Example usage for weka.core Instance isMissing

List of usage examples for weka.core Instance isMissing

Introduction

In this page you can find the example usage for weka.core Instance isMissing.

Prototype

public boolean isMissing(Attribute att);

Source Link

Document

Tests if a specific value is "missing".

Usage

From source file:milk.visualize.MIPlot2D.java

License:Open Source License

/**
 * Renders this component//ww  w .  ja v  a 2s . c o m
 * @param gx the graphics context
 */
public void paintComponent(Graphics gx) {

    //if(!isEnabled())
    //    return;

    super.paintComponent(gx);

    if (plotExemplars != null) {
        gx.setColor(m_axisColour);
        // Draw the axis name
        String xname = plotExemplars.attribute(m_xIndex).name(),
                yname = plotExemplars.attribute(m_yIndex).name();
        gx.drawString(yname, m_XaxisStart + m_labelMetrics.stringWidth("M"),
                m_YaxisStart + m_labelMetrics.getAscent() / 2 + m_tickSize);
        gx.drawString(xname, m_XaxisEnd - m_labelMetrics.stringWidth(yname) + m_tickSize,
                (int) (m_YaxisEnd - m_labelMetrics.getAscent() / 2));

        // Draw points
        Attribute classAtt = plotExemplars.classAttribute();
        for (int j = 0; j < m_plots.size(); j++) {
            PlotData2D temp_plot = (PlotData2D) (m_plots.elementAt(j));
            Instances instances = temp_plot.getPlotInstances();

            StringTokenizer st = new StringTokenizer(
                    instances.firstInstance().stringValue(plotExemplars.idIndex()), "_");

            //////////////////// TLD stuff /////////////////////////////////
            /*
            double[] mu = new double[plotExemplars.numAttributes()],
                sgm = new double[plotExemplars.numAttributes()];
            st.nextToken(); // Squeeze first element
            int p=0;
            while(p<mu.length){
                if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex()))
               p++;
                if(p<mu.length){
               mu[p] = Double.parseDouble(st.nextToken());
               sgm[p] = Double.parseDouble(st.nextToken());
               p++;
                }
            }
            Instance ins = instances.firstInstance();
            gx.setColor((Color)m_colorList.elementAt((int)ins.classValue()));
            double mux=mu[m_xIndex], muy=mu[m_yIndex],
                sgmx=sgm[m_xIndex], sgmy=sgm[m_yIndex];
            double xs = convertToPanelX(mux-3*sgmx), xe = convertToPanelX(mux+3*sgmx),
                xleng = Math.abs(xe-xs);
            double ys = convertToPanelY(muy+3*sgmy), ye = convertToPanelY(muy-3*sgmy),
                yleng = Math.abs(ye-ys);
            // Draw oval
            gx.drawOval((int)xs,(int)ys,(int)xleng,(int)yleng);
            // Draw a dot
            gx.fillOval((int)convertToPanelX(mux)-2, (int)convertToPanelY(muy)-2, 4, 4);
            */
            //////////////////// TLD stuff /////////////////////////////////

            //////////////////// instance-based stuff /////////////////////////////////
            /*
              double[] core = new double[plotExemplars.numAttributes()],
                range=new double[plotExemplars.numAttributes()];
            st.nextToken(); // Squeeze first element
            int p=0;
            while(p<range.length){
                if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex()))
               p++;
                if(p<range.length)
               range[p++] = Double.parseDouble(st.nextToken());
            }
                    
            p=0;
            while(st.hasMoreTokens()){
                if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex()))
               p++;
                core[p++] = Double.parseDouble(st.nextToken());
            }
                    
            Instance ins = instances.firstInstance();
            gx.setColor((Color)m_colorList.elementAt((int)ins.classValue()));
            double rgx=range[m_xIndex], rgy=range[m_yIndex];
            double x1 = convertToPanelX(core[m_xIndex]-rgx/2),
                y1 = convertToPanelY(core[m_yIndex]-rgy/2),
                x2 = convertToPanelX(core[m_xIndex]+rgx/2),
                y2 = convertToPanelY(core[m_yIndex]+rgy/2),
                x = convertToPanelX(core[m_xIndex]),
                y = convertToPanelY(core[m_yIndex]);
                    
            // Draw a rectangle
            gx.drawLine((int)x1, (int)y1, (int)x2, (int)y1);
            gx.drawLine((int)x1, (int)y1, (int)x1, (int)y2);
            gx.drawLine((int)x2, (int)y1, (int)x2, (int)y2);
            gx.drawLine((int)x1, (int)y2, (int)x2, (int)y2);
                    
            // Draw a dot
            gx.fillOval((int)x-3, (int)y-3, 6, 6);
                    
            // Draw string
            StringBuffer text =new StringBuffer(temp_plot.getPlotName()+":"+instances.numInstances());      
            gx.drawString(text.toString(), (int)x1, (int)y2+m_labelMetrics.getHeight());
            */
            //////////////////// instance-based stuff /////////////////////////////////

            //////////////////// normal graph /////////////////////////////////

            // Paint numbers
            for (int i = 0; i < instances.numInstances(); i++) {
                Instance ins = instances.instance(i);
                if (!ins.isMissing(m_xIndex) && !ins.isMissing(m_yIndex)) {
                    if (classAtt.isNominal())
                        gx.setColor((Color) m_colorList.elementAt((int) ins.classValue()));
                    else {
                        double r = (ins.classValue() - m_minC) / (m_maxC - m_minC);
                        r = (r * 240) + 15;
                        gx.setColor(new Color((int) r, 150, (int) (255 - r)));
                    }

                    double x = convertToPanelX(ins.value(m_xIndex));
                    double y = convertToPanelY(ins.value(m_yIndex));

                    String id = temp_plot.getPlotName();
                    gx.drawString(id, (int) (x - m_labelMetrics.stringWidth(id) / 2),
                            (int) (y + m_labelMetrics.getHeight() / 2));
                }
            }

            //////////////////// normal graph /////////////////////////////////   
        }
    }

    //////////////////// TLD stuff /////////////////////////////////
    // Draw two Guassian contour with 3 stdDev
    // (-1, -1) with stdDev 1, 2
    // (1, 1) with stdDev 2, 1
    /*gx.setColor(Color.black);
    double mu=-1.5, sigmx, sigmy; // class 0
    if(m_xIndex == 1)
        sigmx = 1;          
    else
        sigmx = 2;
    if(m_yIndex == 1)
        sigmy = 1;          
    else
        sigmy = 2;
            
    double x1 = convertToPanelX(mu-3*sigmx), x2 = convertToPanelX(mu+3*sigmx),
        xlen = Math.abs(x2-x1);
    double y1 = convertToPanelY(mu+3*sigmy), y2 = convertToPanelY(mu-3*sigmy),
        ylen = Math.abs(y2-y1);
    // Draw heavy oval
    gx.drawOval((int)x1,(int)y1,(int)xlen,(int)ylen);
    gx.drawOval((int)x1-1,(int)y1-1,(int)xlen+2,(int)ylen+2);
    gx.drawOval((int)x1+1,(int)y1+1,(int)xlen-2,(int)ylen-2);
    // Draw a dot
    gx.fillOval((int)convertToPanelX(mu)-3, (int)convertToPanelY(mu)-3, 6, 6);
            
    mu=1.5; // class 1
    if(m_xIndex == 1)
        sigmx = 1;          
    else
        sigmx = 2;
    if(m_yIndex == 1)
        sigmy = 1;          
    else
        sigmy = 2;
            
    x1 = convertToPanelX(mu-3*sigmx);
    x2 = convertToPanelX(mu+3*sigmx);
    xlen = Math.abs(x2-x1);
    y1 = convertToPanelY(mu+3*sigmy);
    y2 = convertToPanelY(mu-3*sigmy);
    ylen = Math.abs(y2-y1);
    // Draw heavy oval
    gx.drawOval((int)x1,(int)y1,(int)xlen,(int)ylen);
    gx.drawOval((int)x1-1,(int)y1-1,(int)xlen+2,(int)ylen+2);
    gx.drawOval((int)x1+1,(int)y1+1,(int)xlen-2,(int)ylen-2);
    // Draw a dot
    gx.fillOval((int)convertToPanelX(mu)-3, (int)convertToPanelY(mu)-3, 6, 6);
    */
    //////////////////// TLD stuff /////////////////////////////////

    //////////////////// instance-based stuff /////////////////////////////////
    /*
    // Paint a log-odds line: 1*x0+2*x1=0
    double xstart, xend, ystart, yend, xCoeff, yCoeff;
    if(m_xIndex == 1)
        xCoeff = 1;   
    else
        xCoeff = 2;   
    if(m_yIndex == 1)
        yCoeff = 1;   
    else
        yCoeff = 2;   
            
    xstart = m_minX;
    ystart = -xstart*xCoeff/yCoeff;
    if(ystart > m_maxY){
        ystart = m_maxY;
        xstart = -ystart*yCoeff/xCoeff;
    }   
    yend = m_minY;
    xend = -yend*yCoeff/xCoeff;
    if(xend > m_maxX){
        xend = m_maxX;
        yend = -xend*xCoeff/yCoeff;
    }
            
    // Draw a heavy line
    gx.setColor(Color.black);
    gx.drawLine((int)convertToPanelX(xstart), (int)convertToPanelY(ystart),
           (int)convertToPanelX(xend), (int)convertToPanelY(yend));
    gx.drawLine((int)convertToPanelX(xstart)+1, (int)convertToPanelY(ystart)+1,
           (int)convertToPanelX(xend)+1, (int)convertToPanelY(yend)+1);
    gx.drawLine((int)convertToPanelX(xstart)-1, (int)convertToPanelY(ystart)-1,
           (int)convertToPanelX(xend)-1, (int)convertToPanelY(yend)-1);
    */
    //////////////////// instance-based stuff /////////////////////////////////
}

From source file:moa.classifiers.bayes.NaiveBayes.java

License:Open Source License

public static double[] doNaiveBayesPrediction(Instance inst, DoubleVector observedClassDistribution,
        AutoExpandVector<AttributeClassObserver> attributeObservers) {
    double[] votes = new double[observedClassDistribution.numValues()];
    double observedClassSum = observedClassDistribution.sumOfValues();
    for (int classIndex = 0; classIndex < votes.length; classIndex++) {
        votes[classIndex] = observedClassDistribution.getValue(classIndex) / observedClassSum;
        for (int attIndex = 0; attIndex < inst.numAttributes() - 1; attIndex++) {
            int instAttIndex = modelAttIndexToInstanceAttIndex(attIndex, inst);
            AttributeClassObserver obs = attributeObservers.get(attIndex);
            if ((obs != null) && !inst.isMissing(instAttIndex)) {
                votes[classIndex] *= obs.probabilityOfAttributeValueGivenClass(inst.value(instAttIndex),
                        classIndex);//from   w  w  w .j ava 2s .  c o m
            }
        }
    }
    // TODO: need logic to prevent underflow?
    return votes;
}

From source file:moa.classifiers.bayes.NaiveBayes.java

License:Open Source License

public static double[] doNaiveBayesPredictionLog(Instance inst, DoubleVector observedClassDistribution,
        AutoExpandVector<AttributeClassObserver> observers,
        AutoExpandVector<AttributeClassObserver> observers2) {
    AttributeClassObserver obs;/*w w  w .j a v a2s.c o m*/
    double[] votes = new double[observedClassDistribution.numValues()];
    double observedClassSum = observedClassDistribution.sumOfValues();
    for (int classIndex = 0; classIndex < votes.length; classIndex++) {
        votes[classIndex] = Math.log10(observedClassDistribution.getValue(classIndex) / observedClassSum);
        for (int attIndex = 0; attIndex < inst.numAttributes() - 1; attIndex++) {
            int instAttIndex = modelAttIndexToInstanceAttIndex(attIndex, inst);
            if (inst.attribute(instAttIndex).isNominal()) {
                obs = observers.get(attIndex);
            } else {
                obs = observers2.get(attIndex);
            }

            if ((obs != null) && !inst.isMissing(instAttIndex)) {
                votes[classIndex] += Math
                        .log10(obs.probabilityOfAttributeValueGivenClass(inst.value(instAttIndex), classIndex));

            }
        }
    }
    return votes;

}

From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance the new training instance to include in the model
 *///from  ww w  .j  a v a  2 s  .  c o m
@Override
public void trainOnInstanceImpl(Instance inst) {
    if (this.reset == true) {
        this.m_numClasses = inst.numClasses();
        double laplace = this.laplaceCorrectionOption.getValue();
        int numAttributes = inst.numAttributes();

        m_probOfClass = new double[m_numClasses];
        Arrays.fill(m_probOfClass, laplace);

        m_classTotals = new double[m_numClasses];
        Arrays.fill(m_classTotals, laplace * numAttributes);

        m_wordTotalForClass = new DoubleVector[m_numClasses];
        for (int i = 0; i < m_numClasses; i++) {
            //Arrays.fill(wordTotal, laplace);
            m_wordTotalForClass[i] = new DoubleVector();
        }
        this.reset = false;
    }
    // Update classifier
    int classIndex = inst.classIndex();
    int classValue = (int) inst.value(classIndex);

    double w = inst.weight();
    m_probOfClass[classValue] += w;

    m_classTotals[classValue] += w * totalSize(inst);
    double total = m_classTotals[classValue];

    for (int i = 0; i < inst.numValues(); i++) {
        int index = inst.index(i);
        if (index != classIndex && !inst.isMissing(i)) {
            //m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i);
            double laplaceCorrection = 0.0;
            if (m_wordTotalForClass[classValue].getValue(index) == 0) {
                laplaceCorrection = this.laplaceCorrectionOption.getValue();
            }
            m_wordTotalForClass[classValue].addToValue(index, w * inst.valueSparse(i) + laplaceCorrection);
        }
    }
}

From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance./*from w  ww.  j a va 2 s . co  m*/
 *
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance instance) {
    if (this.reset == true) {
        return new double[2];
    }
    double[] probOfClassGivenDoc = new double[m_numClasses];
    double totalSize = totalSize(instance);

    for (int i = 0; i < m_numClasses; i++) {
        probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]);
    }

    for (int i = 0; i < instance.numValues(); i++) {

        int index = instance.index(i);
        if (index == instance.classIndex() || instance.isMissing(i)) {
            continue;
        }

        double wordCount = instance.valueSparse(i);
        for (int c = 0; c < m_numClasses; c++) {
            double value = m_wordTotalForClass[c].getValue(index);
            probOfClassGivenDoc[c] += wordCount
                    * Math.log(value == 0 ? this.laplaceCorrectionOption.getValue() : value);
        }
    }

    return Utils.logs2probs(probOfClassGivenDoc);
}

From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java

License:Open Source License

public double totalSize(Instance instance) {
    int classIndex = instance.classIndex();
    double total = 0.0;
    for (int i = 0; i < instance.numValues(); i++) {
        int index = instance.index(i);
        if (index == classIndex || instance.isMissing(i)) {
            continue;
        }//from   www .  ja v a2  s  .c o  m
        double count = instance.valueSparse(i);
        if (count >= 0) {
            total += count;
        } else {
            //throw new Exception("Numeric attribute value is not >= 0. " + i + " " + index + " " +
            //          instance.valueSparse(i) + " " + " " + instance);
        }
    }
    return total;
}

From source file:moa.classifiers.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance    the new training instance to include in the model
 *///from   w  w  w  . ja v  a  2  s. c  o  m
@Override
public void trainOnInstanceImpl(Instance inst) {
    if (this.reset == true) {
        this.m_numClasses = inst.numClasses();
        double laplace = this.laplaceCorrectionOption.getValue();
        int numAttributes = inst.numAttributes();

        m_probOfClass = new double[m_numClasses];
        Arrays.fill(m_probOfClass, laplace);

        m_classTotals = new double[m_numClasses];
        Arrays.fill(m_classTotals, laplace * numAttributes);

        m_wordTotalForClass = new double[numAttributes][m_numClasses];
        for (double[] wordTotal : m_wordTotalForClass) {
            Arrays.fill(wordTotal, laplace);
        }
        this.reset = false;
    }
    // Update classifier
    int classIndex = inst.classIndex();
    int classValue = (int) inst.value(classIndex);

    double w = inst.weight();
    m_probOfClass[classValue] += w;

    m_classTotals[classValue] += w * totalSize(inst);
    double total = m_classTotals[classValue];

    for (int i = 0; i < inst.numValues(); i++) {
        int index = inst.index(i);
        if (index != classIndex && !inst.isMissing(i)) {
            m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i);
        }
    }
}

From source file:moa.classifiers.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//from w  w  w  . j av  a 2 s  .  c  o  m
 *
 * @param instance    the instance to be classified
 * @return       predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance instance) {
    if (this.reset == true) {
        return new double[2];
    }
    double[] probOfClassGivenDoc = new double[m_numClasses];
    double totalSize = totalSize(instance);

    for (int i = 0; i < m_numClasses; i++) {
        probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]);
    }

    for (int i = 0; i < instance.numValues(); i++) {

        int index = instance.index(i);
        if (index == instance.classIndex() || instance.isMissing(i)) {
            continue;
        }

        double wordCount = instance.valueSparse(i);
        for (int c = 0; c < m_numClasses; c++) {
            probOfClassGivenDoc[c] += wordCount * Math.log(m_wordTotalForClass[index][c]);
        }
    }

    return Utils.logs2probs(probOfClassGivenDoc);
}

From source file:moa.classifiers.rules.core.conditionaltests.NumericAttributeBinaryRulePredicate.java

License:Apache License

@Override
public int branchForInstance(Instance inst) {
    int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1;
    if (inst.isMissing(instAttIndex)) {
        return -1;
    }// w  ww .j ava  2  s.c o  m
    double v = inst.value(instAttIndex);
    int ret = 0;
    switch (this.operator) {
    case 0:
        ret = (v == this.attValue) ? 0 : 1;
        break;
    case 1:
        ret = (v <= this.attValue) ? 0 : 1;
        break;
    case 2:
        ret = (v > this.attValue) ? 0 : 1;
    }
    return ret;
}

From source file:moa.classifiers.rules.RuleClassifier.java

License:Apache License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int countRuleFiredTrue = 0;
    boolean ruleFired = false;
    this.instance = inst;
    this.numAttributes = instance.numAttributes() - 1;
    this.numClass = instance.numClasses();
    this.numInstance = numInstance + 1;
    int conta1 = 0;
    for (int j = 0; j < ruleSet.size(); j++) {
        if (this.ruleSet.get(j).ruleEvaluate(inst) == true) {
            countRuleFiredTrue = countRuleFiredTrue + 1;

            double anomaly = 0.0;
            if (this.Supervised.isSet()) {
                anomaly = computeAnomalySupervised(this.ruleSet.get(j), j, inst); // compute anomaly (Supervised method)
            } else if (this.Unsupervised.isSet()) {
                anomaly = computeAnomalyUnsupervised(this.ruleSet.get(j), j, inst); // compute anomaly (Unsupervised method)
            }/*from   www. j a  v a 2 s. c o  m*/

            if (anomaly >= this.anomalyProbabilityThresholdOption.getValue()) {
                conta1 = conta1 + 1;
            }
            //   System.out.print(numInstance+";"+anomaly+"\n");
            try {

                File dir = new File("SeaAnomaliesUnsupervised.txt");
                FileWriter fileWriter = new FileWriter(dir, true);
                PrintWriter printWriter = new PrintWriter(fileWriter);
                printWriter.println(numInstance + ";" + anomaly);
                printWriter.flush();
                printWriter.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
            if ((this.ruleSet.get(j).instancesSeen <= this.anomalyNumInstThresholdOption.getValue())
                    || (anomaly < this.anomalyProbabilityThresholdOption.getValue()
                            && this.anomalyDetectionOption.isSet())
                    || !this.anomalyDetectionOption.isSet()) {
                this.ruleSet.get(j).obserClassDistrib.addToValue((int) inst.classValue(), inst.weight());
                for (int i = 0; i < inst.numAttributes() - 1; i++) {
                    int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst);

                    if (!inst.isMissing(instAttIndex)) {
                        AttributeClassObserver obs = this.ruleSet.get(j).observers.get(i); // Nominal and binary tree.
                        AttributeClassObserver obsGauss = this.ruleSet.get(j).observersGauss.get(i); // Gaussian.
                        if (obs == null) {
                            obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver()
                                    : newNumericClassObserver();
                            this.ruleSet.get(j).observers.set(i, obs);
                        }
                        if (obsGauss == null) {
                            obsGauss = inst.attribute(instAttIndex).isNumeric() ? newNumericClassObserver2()
                                    : null;
                            this.ruleSet.get(j).observersGauss.set(i, obsGauss);
                        }
                        obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(),
                                inst.weight());
                        if (inst.attribute(instAttIndex).isNumeric()) {
                            obsGauss.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(),
                                    inst.weight());
                        }
                    }
                }
                expandeRule(this.ruleSet.get(j), inst, j); // This function expands the rule
            }
            if (this.orderedRulesOption.isSet()) { // Ordered rules
                break;
            }
        }
    }
    if (countRuleFiredTrue > 0) {
        ruleFired = true;
    } else {
        ruleFired = false;
    }
    if (ruleFired == false) { //If none of the rules cover the example update sufficient statistics of the default rule
        this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight());
        for (int i = 0; i < inst.numAttributes() - 1; i++) {
            int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst);
            if (!inst.isMissing(instAttIndex)) {
                AttributeClassObserver obs = this.attributeObservers.get(i);
                AttributeClassObserver obsGauss = this.attributeObserversGauss.get(i);
                if (obs == null) {
                    obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver()
                            : newNumericClassObserver();
                    this.attributeObservers.set(i, obs);
                }
                if (obsGauss == null) {
                    obsGauss = inst.attribute(instAttIndex).isNumeric() ? newNumericClassObserver2() : null;
                    this.attributeObserversGauss.set(i, obsGauss);
                }
                obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight());
                if (inst.attribute(instAttIndex).isNumeric()) {
                    obsGauss.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(),
                            inst.weight());
                }
            }
        }
        createRule(inst); //This function creates a rule
    }
}