Example usage for weka.core Instance dataset

List of usage examples for weka.core Instance dataset

Introduction

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

Prototype

public Instances dataset();

Source Link

Document

Returns the dataset this instance has access to.

Usage

From source file:moa.evaluation.WindowClassificationPerformanceEvaluator.java

License:Open Source License

@Override
public void addResult(Instance inst, double[] classVotes) {
    double weight = inst.weight();
    int trueClass = (int) inst.classValue();
    if (weight > 0.0) {
        if (TotalweightObserved == 0) {
            reset(inst.dataset().numClasses());
        }/*from  w  w w  .  jav a2s . c  o  m*/
        this.TotalweightObserved += weight;
        this.weightObserved.add(weight);
        int predictedClass = Utils.maxIndex(classVotes);
        if (predictedClass == trueClass) {
            this.weightCorrect.add(weight);
        } else {
            this.weightCorrect.add(0);
        }
        //Add Kappa statistic information
        for (int i = 0; i < this.numClasses; i++) {
            this.rowKappa[i].add(i == predictedClass ? weight : 0);
            this.columnKappa[i].add(i == trueClass ? weight : 0);
        }
        if (this.lastSeenClass == trueClass) {
            this.weightCorrectNoChangeClassifier.add(weight);
        } else {
            this.weightCorrectNoChangeClassifier.add(0);
        }
        this.classAccuracy[trueClass].add(predictedClass == trueClass ? weight : 0.0);
        this.lastSeenClass = trueClass;
    }
}

From source file:moa.evaluation.WindowClassificationPerformanceEvaluator2.java

License:Open Source License

@Override
public void addResult(Instance inst, double[] classVotes) {
    double weight = inst.weight();
    int trueClass = (int) inst.classValue();
    if (weight > 0.0) {
        if (TotalweightObserved == 0) {
            reset(inst.dataset().numClasses());
        }/*w w w. j  a  v  a2  s.c o m*/
        this.TotalweightObserved += weight;
        this.weightObserved.add(weight);
        int predictedClass = Utils.maxIndex(classVotes);
        if (predictedClass == trueClass) {
            this.weightCorrect.add(weight);
        } else {
            this.weightCorrect.add(0);
        }
        //Add Kappa statistic information
        for (int i = 0; i < this.numClasses; i++) {
            this.rowKappa[i].add(i == predictedClass ? weight : 0);
            this.columnKappa[i].add(i == trueClass ? weight : 0);
        }

    }
}

From source file:moa.evaluation.WindowRegressionPerformanceEvaluator.java

License:Open Source License

@Override
public void addResult(Instance inst, double[] prediction) {
    double weight = inst.weight();
    if (weight > 0.0) {
        if (TotalweightObserved == 0) {
            reset(inst.dataset().numClasses());
        }//  ww  w.ja  v a  2 s  . com
        this.TotalweightObserved += weight;
        this.weightObserved.add(weight);

        if (prediction.length > 0) {
            this.squareError.add((inst.classValue() - prediction[0]) * (inst.classValue() - prediction[0]));
            this.averageError.add(Math.abs(inst.classValue() - prediction[0]));
        }
        //System.out.println(inst.classValue()+", "+prediction[0]);
    }
}

From source file:moa.gui.visualization.DataPoint.java

License:Apache License

public DataPoint(Instance nextInstance, Integer timestamp) {
    super(nextInstance);
    this.setDataset(nextInstance.dataset());
    this.timestamp = timestamp;
    measure_values = new HashMap<String, String>();

    Attribute classLabel = dataset().classAttribute();
    noiseLabel = classLabel.indexOfValue("noise"); // -1 returned if there is no noise
}

From source file:mulan.classifier.meta.thresholding.MetaLabeler.java

License:Open Source License

private int countTrueLabels(Instance instance) {
    int numTrueLabels = 0;
    for (int i = 0; i < numLabels; i++) {
        int labelIndice = labelIndices[i];
        if (instance.dataset().attribute(labelIndice).value((int) instance.value(labelIndice)).equals("1")) {
            numTrueLabels++;/*from  ww  w.  j  a v a2  s. co  m*/
        }
    }
    return numTrueLabels;
}

From source file:mulan.classifier.transformation.IncludeLabelsClassifier.java

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    double[] confidences = new double[numLabels];
    boolean[] bipartition = new boolean[numLabels];

    Instance newInstance = pt6Trans.transformInstance(instance);
    //calculate confidences
    //debug(instance.toString());
    for (int i = 0; i < numLabels; i++) {
        newInstance.setDataset(transformed);
        newInstance.setValue(newInstance.numAttributes() - 2,
                instance.dataset().attribute(labelIndices[i]).name());
        //debug(newInstance.toString());
        double[] temp = baseClassifier.distributionForInstance(newInstance);
        //debug(temp.toString());
        confidences[i] = temp[transformed.classAttribute().indexOfValue("1")];
        //debug("" + confidences[i]);
        bipartition[i] = temp[transformed.classAttribute().indexOfValue("1")] >= temp[transformed
                .classAttribute().indexOfValue("0")] ? true : false;
        //debug("" + bipartition[i]);
    }/*from   w w w .jav a 2  s. co  m*/

    MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences);
    return mlo;
}

From source file:mulan.regressor.transformation.RegressorChainSimple.java

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    double[] scores = new double[numLabels];

    // create a new temporary instance so that the passed instance is not altered
    Instances dataset = instance.dataset();
    Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray());

    for (int counter = 0; counter < numLabels; counter++) {
        dataset.setClassIndex(chain[counter]);
        tempInstance.setDataset(dataset);
        // find the appropriate position for that score in the scores array
        // i.e. which is the corresponding target
        int pos = 0;
        for (int i = 0; i < numLabels; i++) {
            if (chain[counter] == labelIndices[i]) {
                pos = i;// w  ww . j  a v a  2s.  co m
                break;
            }
        }
        scores[pos] = chainRegressors[counter].classifyInstance(tempInstance);
        tempInstance.setValue(chain[counter], scores[pos]);
    }

    MultiLabelOutput mlo = new MultiLabelOutput(scores, true);
    return mlo;
}

From source file:mulan.regressor.transformation.SingleTargetRegressor.java

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    double[] scores = new double[numLabels];
    Instances dataset = instance.dataset();

    for (int counter = 0; counter < numLabels; counter++) {
        dataset.setClassIndex(labelIndices[counter]);
        instance.setDataset(dataset);/*from   w  w  w . java 2  s. co m*/
        scores[counter] = stRegressors[counter].classifyInstance(instance);
    }

    MultiLabelOutput mlo = new MultiLabelOutput(scores, true);

    return mlo;
}

From source file:mulan.transformations.regression.ChainTransformation.java

License:Open Source License

/**
 * Transforms a single Instance in the same way as
 * {@link #transformInstance(Instance, int[], int)} transforms an Instances object.
 * /*from  w ww  . java  2 s.  c o m*/
 * @param instance the input instance
 * @param chain a chain (permutation) of the indices of the target attributes
 * @param numTargetsToKeep the number of target attributes from the beginning of the chain that
 *            should be kept, 1&lt;=numTargetsToKeep&lt;=numOfTargets
 * @return the transformed Instance object. The input object is not modified.
 * @throws Exception Potential exception thrown. To be handled in an upper level.
 */
public static Instance transformInstance(Instance instance, int[] chain, int numTargetsToKeep)
        throws Exception {
    int numOfTargets = chain.length;
    // Indices of attributes to remove
    int[] indicesToRemove = new int[numOfTargets - numTargetsToKeep];
    for (int i = 0; i < numOfTargets - numTargetsToKeep; i++) {
        indicesToRemove[i] = chain[numTargetsToKeep + i];
    }
    Remove remove = new Remove();
    remove.setAttributeIndicesArray(indicesToRemove);
    remove.setInputFormat(instance.dataset());
    remove.input(instance);
    remove.batchFinished();
    Instance transformed = remove.output();

    return transformed;
}

From source file:net.sf.bddbddb.OrderClassifier.java

License:LGPL

public double importance(weka.core.Attribute attribute, String attrValue) {//, String classValue){

    int count = 0;
    int goodCount = 0, badCount = 0;
    List newInstances = new LinkedList();

    for (Iterator it = orders.iterator(); it.hasNext();) {
        Instance instance = (Instance) it.next();
        if (//!instance.stringValue(instance.classIndex()).equals(classValue) ||
        !instance.stringValue(attribute).equals(attrValue))
            continue;

        if (goodClusters.contains(instance.stringValue(instance.classIndex())))
            ++goodCount;//w  w  w  .  j a  v a 2s  .  c om
        else
            ++badCount;

        Instance newInstance = new Instance(instance);
        newInstance.setDataset(instance.dataset());
        newInstances.add(newInstance);
    }
    goodCount *= attrOptions.size() - 1;
    badCount *= attrOptions.size() - 1;
    for (Iterator it = newInstances.iterator(); it.hasNext();) {
        Instance instance = (Instance) it.next();
        /*      if(//!instance.stringValue(instance.classIndex()).equals(classValue) || 
         !instance.stringValue(attribute).equals(attrValue)) continue;
         */

        String classValue = instance.stringValue(instance.classIndex());
        FastVector newOptions = new FastVector();
        newOptions.appendElements(attrOptions);
        newOptions.removeElementAt(newOptions.indexOf(instance.stringValue(attribute)));
        //int index = Math.abs(LearnedOrder.randomNumGen.nextInt()) % newOptions.size();
        int index = 0;
        while (index < newOptions.size()) {
            instance.setValue(attribute, attrOptions.indexOf(newOptions.elementAt(index)));
            String value = classify(instance);
            if (goodClusters.contains(classValue)) {
                if (goodClusters.contains(value))
                    --goodCount;
            } else if (!goodClusters.contains(classValue)) {
                if (!goodClusters.contains(value))
                    --badCount;
            }
            ++index;
        }
        //if(value.equals(classValue)) --count;
    }

    count = goodCount - badCount;
    count /= attrOptions.size() - 1;

    double importance = ((double) count) / newInstances.size();
    if (Double.isNaN(importance))
        return 0;
    return importance;
}