Example usage for weka.core Instance weight

List of usage examples for weka.core Instance weight

Introduction

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

Prototype

public double weight();

Source Link

Document

Returns the instance's weight.

Usage

From source file:moa.evaluation.ClassificationWithNovelClassPerformanceEvaluator.java

License:Open Source License

/**
 * //w  ww .  j av  a  2s.co m
 * Note that for novel class testing, an addition class value is added to the known classes. T
 * This extra "Label" represents a prediction of "Novel Class". This approach allows for
 * algorithms that do not have novel class prediction capabilities to still function,
 * as this method first bounds checks to see if the prediction array includes the added label
 * 
 * @param inst instance under test
 * @param classVotes prediction table for this instance
 */
@Override
public void addResult(Instance inst, double[] classVotes) {
    if (header == null) {
        header = AbstractNovelClassClassifier.augmentInstances(inst.dataset());
        this.novelClassLabel = header.classAttribute()
                .indexOfValue(AbstractNovelClassClassifier.NOVEL_LABEL_STR);
        this.outlierLabel = header.classAttribute()
                .indexOfValue(AbstractNovelClassClassifier.OUTLIER_LABEL_STR);
        this.rowKappa = new double[header.numClasses()];
        Arrays.fill(this.rowKappa, 0.0);
        this.columnKappa = new double[header.numClasses()];
        Arrays.fill(this.columnKappa, 0.0);
        this.knownTrueLabels = new int[header.numClasses()];
        Arrays.fill(knownTrueLabels, 0);
        this.observedLabels = new int[header.numClasses()];
        Arrays.fill(observedLabels, 0);
    }

    final int trueClass = (int) inst.classValue();
    if (classVotes == null) {
        this.knownTrueLabels[trueClass]++;
        return;
    }
    final double[] labelsOnlyVotes = Arrays.copyOf(classVotes, inst.numClasses());
    if (labelsOnlyVotes.length > this.novelClassLabel) {
        labelsOnlyVotes[novelClassLabel] = 0;
    }
    if (labelsOnlyVotes.length > this.outlierLabel) {
        labelsOnlyVotes[outlierLabel] = 0;
    }
    final double totalVoteQty = weka.core.Utils.sum(labelsOnlyVotes);
    final int predictedClass = weka.core.Utils.maxIndex(labelsOnlyVotes); // Don't count the special extended indexes for novel and outlier
    final boolean isMarkedOutlier = (weka.core.Utils.maxIndex(classVotes) == this.outlierLabel);

    if (predictedClass < inst.numClasses() && labelsOnlyVotes[predictedClass] > 0.0) { // Only if there is SOME vote (non-zero)
        this.observedLabels[predictedClass]++; // If we predict it, then it can't be novel!
    }
    //final boolean isTrueNovel = !(this.observedLabels[(int)trueClass] > observationsUntilNotNovelOption.getValue());
    boolean predictedNovel = ((classVotes.length > this.novelClassLabel)
            && (classVotes[this.novelClassLabel] > 0));// this.thresholdOfNoveltyOption.getValue()));

    final boolean isVoteOutlier = (totalVoteQty <= (weka.core.Utils.SMALL * 10.0));
    final boolean correctLabelPrediction = (predictedClass == trueClass);
    switch (this.outlierHandlingStrategyOption.getChosenIndex()) {
    case 0: // use anyway
        // keep on trucking... 
        break;
    case 1: // ignore marked
        if (isMarkedOutlier) {
            return;
        }
        break;
    case 2: // ignore no vote
        if (isVoteOutlier) {
            return;
        }
        break;
    case 3: // ignore iff marked AND no vote
        if (isVoteOutlier && isMarkedOutlier) {
            return;
        }
        break;
    case 4: // ignore pure OR marked
        if (isVoteOutlier || isMarkedOutlier) {
            return;
        }
        break;
    case 5: // mark as novel
        predictedNovel = predictedNovel || isMarkedOutlier;
        break;
    default:
        break;
    }
    this.numberOfInstancesSeen++;
    this.weightObserved += inst.weight(); // /!\ IS THIS RIGHT???
    //final boolean isTrueNovel = (this.knownTrueLabels[trueClass] < this.maxUnobservationsUntilNotNovelOption.getValue()) && (this.observedLabels[trueClass] < observationsUntilNotNovelOption.getValue());
    final boolean isTrueNovel = (this.knownTrueLabels[trueClass] < this.maxUnobservationsUntilNotNovelOption
            .getValue());
    // 8x different mutually exclusive options (i.e. 3-bits)
    if ((!predictedNovel) && (!isTrueNovel) && (correctLabelPrediction)) { // Should be most common
        this.novelClassDetectionTrueNegative++;
        this.weightCorrect++;
    }
    if ((predictedNovel) && (isTrueNovel) && (correctLabelPrediction)) { // Rare if ever
        this.novelClassDetectionTruePositive++;
        this.weightCorrect++;
        assert false : "Paradox 1 - true novel, but predicted the right label";
    }
    if ((predictedNovel) && (!isTrueNovel) && (correctLabelPrediction)) { // Error due to overly restrictive models
        this.novelClassDetectionFalsePositive++;
        if (this.goodIsGoodOption.isSet()) {
            this.weightCorrect++;
        }
    }
    if ((!predictedNovel) && (isTrueNovel) && (correctLabelPrediction)) { // Should never happen?  Framework was wrong here, so TN
        this.novelClassDetectionTrueNegative++;
        this.weightCorrect++;
        assert false : "Paradox 2 - true novel, but predicted the right label";
    }
    if ((predictedNovel) && (isTrueNovel) && (!correctLabelPrediction)) { // Should be most common when x is novel
        this.novelClassDetectionTruePositive++;
        this.weightCorrect++;
    }
    if ((predictedNovel) && (!isTrueNovel) && (!correctLabelPrediction)) { // Probably an Outlier case
        this.novelClassDetectionFalsePositive++;
        if (this.outlierHandlingStrategyOption.getChosenIndex() > 0) {
            this.weightCorrect++;
        }
    }
    if ((!predictedNovel) && (isTrueNovel) && (!correctLabelPrediction)) { // NCD failure     FN
        this.novelClassDetectionFalseNegative++;
    }
    if ((!predictedNovel) && (!isTrueNovel) && (!correctLabelPrediction)) { // Correct NCD, but bad h(x) prediction
        this.novelClassDetectionTrueNegative++;
    }

    this.rowKappa[predictedClass]++;
    this.columnKappa[trueClass]++;
    this.knownTrueLabels[trueClass] += inst.weight();

}

From source file:moa.evaluation.EWMAClassificationPerformanceEvaluator.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) {
        this.TotalweightObserved += weight;
        if (Utils.maxIndex(classVotes) == trueClass) {
            this.weightCorrect.add(1);
        } else {//from  ww w . j  a  v  a  2s  . c om
            this.weightCorrect.add(0);
        }
    }
}

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());
        }//  w  w  w.j  av a2s  .co  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());
        }/*from   www  .j a v  a2 s  .  c om*/
        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());
        }// www  .  ja v a2  s .c  o m
        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.streams.filters.AddNoiseFilter.java

License:Open Source License

@Override
public Instance nextInstance() {
    Instance inst = (Instance) this.inputStream.nextInstance().copy();
    for (int i = 0; i < inst.numAttributes(); i++) {
        double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue()
                : this.attNoiseFractionOption.getValue();
        if (inst.attribute(i).isNominal()) {
            DoubleVector obs = (DoubleVector) this.attValObservers.get(i);
            if (obs == null) {
                obs = new DoubleVector();
                this.attValObservers.set(i, obs);
            }// www  .j ava  2s  .  c o  m
            int originalVal = (int) inst.value(i);
            if (!inst.isMissing(i)) {
                obs.addToValue(originalVal, inst.weight());
            }
            if ((this.random.nextDouble() < noiseFrac) && (obs.numNonZeroEntries() > 1)) {
                do {
                    inst.setValue(i, this.random.nextInt(obs.numValues()));
                } while (((int) inst.value(i) == originalVal) || (obs.getValue((int) inst.value(i)) == 0.0));
            }
        } else {
            GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i);
            if (obs == null) {
                obs = new GaussianEstimator();
                this.attValObservers.set(i, obs);
            }
            obs.addObservation(inst.value(i), inst.weight());
            inst.setValue(i, inst.value(i) + this.random.nextGaussian() * obs.getStdDev() * noiseFrac);
        }
    }
    return inst;
}

From source file:moa.tasks.EvaluateNonStationaryDynamicStream.java

License:Open Source License

/**
 *
 *
 * @return instances used for training/*  ww w  . java  2  s .  c o m*/
 */
private int train() {
    this.monitor.setCurrentActivityDescription((this.inWarmupPhase) ? "Warmup Training" : "Online Training");
    int ret = 0;
    while (!this.latentTrainingInstQueue.isEmpty()
            && this.latentTrainingInstQueue.peek().deadline <= this.instancesProcessed) {
        Instance x = this.latentTrainingInstQueue.pop().inst;
        if (x.weight() > 0.0 || this.sendZeroWeightsOption.isSet()) {
            if (!x.classIsMissing()) {
                learner.trainOnInstance(x);
                this.knownLabels[(int) x.classValue()] += x.weight();
                ret++;
            }
        }
    }
    assert this.latentTrainingInstQueue.size() < (this.trainingTimeDelayOption.getValue()
            + 1) : "Cache 'latentTrainingInstQueue' is larger than designed.";
    return ret;
}

From source file:mulan.classifier.lazy.BRkNN.java

License:Open Source License

/**
 * Calculates the confidences of the labels, based on the neighboring
 * instances/*from   ww w .  jav  a  2 s.c  o m*/
 *
 * @param neighbours
 *            the list of nearest neighboring instances
 * @param distances
 *            the distances of the neighbors
 * @return the confidences of the labels
 */
private double[] getConfidences(Instances neighbours, double[] distances) {
    double total = 0, weight;
    double neighborLabels = 0;
    double[] confidences = new double[numLabels];

    // Set up a correction to the estimator
    for (int i = 0; i < numLabels; i++) {
        confidences[i] = 1.0 / Math.max(1, train.numInstances());
    }
    total = (double) numLabels / Math.max(1, train.numInstances());

    for (int i = 0; i < neighbours.numInstances(); i++) {
        // Collect class counts
        Instance current = neighbours.instance(i);
        distances[i] = distances[i] * distances[i];
        distances[i] = Math.sqrt(distances[i] / (train.numAttributes() - numLabels));
        switch (distanceWeighting) {
        case WEIGHT_INVERSE:
            weight = 1.0 / (distances[i] + 0.001); // to avoid division by
            // zero
            break;
        case WEIGHT_SIMILARITY:
            weight = 1.0 - distances[i];
            break;
        default: // WEIGHT_NONE:
            weight = 1.0;
            break;
        }
        weight *= current.weight();

        for (int j = 0; j < numLabels; j++) {
            double value = Double.parseDouble(
                    current.attribute(labelIndices[j]).value((int) current.value(labelIndices[j])));
            if (Utils.eq(value, 1.0)) {
                confidences[j] += weight;
                neighborLabels += weight;
            }
        }
        total += weight;
    }

    avgPredictedLabels = (int) Math.round(neighborLabels / total);
    // Normalise distribution
    if (total > 0) {
        Utils.normalize(confidences, total);
    }
    return confidences;
}

From source file:mulan.classifier.meta.HierarchyBuilder.java

License:Open Source License

/**
 * Creates the hierarchical dataset according to the original multilabel
 * instances object and the constructed label hierarchy
 *
 * @param mlData the original multilabel instances
 * @param metaData the metadata of the constructed label hierarchy
 * @return the produced dataset/*from   w ww. j  a  v  a 2s. c  om*/
 * @throws InvalidDataFormatException 
 */
public static MultiLabelInstances createHierarchicalDataset(MultiLabelInstances mlData, LabelsMetaData metaData)
        throws InvalidDataFormatException {
    Set<String> leafLabels = mlData.getLabelsMetaData().getLabelNames();
    Set<String> metaLabels = metaData.getLabelNames();
    for (String string : leafLabels) {
        metaLabels.remove(string);
    }
    Instances dataSet = mlData.getDataSet();
    int numMetaLabels = metaLabels.size();

    // copy existing attributes
    ArrayList<Attribute> atts = new ArrayList<Attribute>(dataSet.numAttributes() + numMetaLabels);
    for (int i = 0; i < dataSet.numAttributes(); i++) {
        atts.add(dataSet.attribute(i));
    }

    ArrayList<String> labelValues = new ArrayList<String>();
    labelValues.add("0");
    labelValues.add("1");

    // add metalabel attributes
    for (String metaLabel : metaLabels) {
        atts.add(new Attribute(metaLabel, labelValues));
    }

    // initialize dataset
    Instances newDataSet = new Instances("hierarchical", atts, dataSet.numInstances());

    // copy features and labels, set metalabels
    for (int i = 0; i < dataSet.numInstances(); i++) {
        //System.out.println("Constructing instance " + (i+1) + "/"  + dataSet.numInstances());
        // initialize new values
        double[] newValues = new double[newDataSet.numAttributes()];
        Arrays.fill(newValues, 0);

        // copy features and labels
        double[] values = dataSet.instance(i).toDoubleArray();
        System.arraycopy(values, 0, newValues, 0, values.length);

        // set metalabels
        for (String label : leafLabels) {
            Attribute att = dataSet.attribute(label);
            if (att.value((int) dataSet.instance(i).value(att)).equals("1")) {
                //System.out.println(label);
                //System.out.println(Arrays.toString(metaData.getLabelNames().toArray()));
                LabelNode currentNode = metaData.getLabelNode(label);
                // put 1 all the way up to the root, unless you see a 1, in which case stop
                while (currentNode.hasParent()) {
                    currentNode = currentNode.getParent();
                    Attribute currentAtt = newDataSet.attribute(currentNode.getName());
                    // change the following to refer to the array
                    if (newValues[atts.indexOf(currentAtt)] == 1) // no need to go more up
                    {
                        break;
                    } else // put 1
                    {
                        newValues[atts.indexOf(currentAtt)] = 1;
                    }
                }
            }
        }
        Instance instance = dataSet.instance(i);
        newDataSet.add(DataUtils.createInstance(instance, instance.weight(), newValues));
    }
    return new MultiLabelInstances(newDataSet, metaData);
}

From source file:mulan.classifier.meta.HOMER.java

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    Instance transformed = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray());
    for (int i = 0; i < numMetaLabels; i++) {
        transformed.insertAttributeAt(transformed.numAttributes());
    }/*from   ww  w.  ja va2  s.c  o  m*/

    transformed.setDataset(header);
    MultiLabelOutput mlo = hmc.makePrediction(transformed);
    boolean[] oldBipartition = mlo.getBipartition();
    //System.out.println("old:" + Arrays.toString(oldBipartition));
    boolean[] newBipartition = new boolean[numLabels];
    System.arraycopy(oldBipartition, 0, newBipartition, 0, numLabels);
    //System.out.println("new:" + Arrays.toString(newBipartition));
    double[] oldConfidences = mlo.getConfidences();
    double[] newConfidences = new double[numLabels];
    System.arraycopy(oldConfidences, 0, newConfidences, 0, numLabels);
    MultiLabelOutput newMLO = new MultiLabelOutput(newBipartition, newConfidences);
    return newMLO;
}