List of usage examples for weka.core Instance classValue
public double classValue();
From source file:moa.evaluation.BasicClassificationPerformanceEvaluator.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 (this.weightObserved == 0) { reset(inst.dataset().numClasses()); }// w w w. j a v a2s. co m this.weightObserved += weight; int predictedClass = Utils.maxIndex(classVotes); if (predictedClass == trueClass) { this.weightCorrect += weight; } this.rowKappa[predictedClass] += weight; this.columnKappa[trueClass] += weight; } if (this.lastSeenClass == trueClass) { this.weightCorrectNoChangeClassifier += weight; } this.lastSeenClass = trueClass; }
From source file:moa.evaluation.BasicClassificationScoringEvaluator.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 (this.weightObserved == 0) { reset(inst.dataset().numClasses()); }/* w ww .j a va 2 s. co m*/ this.weightObserved += weight; //MSE Calculus int predictedClass = Utils.maxIndex(classVotes); if (predictedClass == trueClass) { this.weightCorrect += weight; } double[] normalized = normalize(classVotes); double vote = 0; if (normalized.length > 0) { vote = trueClass < normalized.length ? normalized[trueClass] : 0; } if (Double.compare(vote, Double.NaN) == 0) { int countNaN = 0; for (int i = 0; i < classVotes.length; ++i) { if (Double.compare(normalized[i], Double.NaN) == 0) { countNaN++; } } vote = 1; if (countNaN > 1 && classVotes.length > 1) { vote = 1.0 / countNaN; } } this.mse += 1 - vote; this.saw++; this.rowKappa[predictedClass] += weight; this.columnKappa[trueClass] += weight; } }
From source file:moa.evaluation.BasicRegressionPerformanceEvaluator.java
License:Open Source License
@Override public void addResult(Instance inst, double[] prediction) { if (inst.weight() > 0.0) { this.weightObserved += inst.weight(); if (prediction.length > 0) { this.squareError += (inst.classValue() - prediction[0]) * (inst.classValue() - prediction[0]); this.averageError += Math.abs(inst.classValue() - prediction[0]); }// w w w .j av a 2 s . co m } }
From source file:moa.evaluation.ClassificationWithNovelClassPerformanceEvaluator.java
License:Open Source License
/** * //from w w w. j a v a 2s . com * 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 . java 2 s . co m*/ 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 . jav a 2 s. com*/ 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 w ww .ja va 2 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()); }/* w w w . java2s.co 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.generators.multilabel.MetaMultilabelGenerator.java
License:Open Source License
private Instance getNextWithBinary(int i) { int lim = 1000; if (queue[i].size() <= 0) { int c = -1; while (lim-- > 0) { Instance tinst = this.m_BinaryGenerator.nextInstance(); c = (int) Math.round(tinst.classValue()); if (i == c) { return tinst; } else if (queue[c].size() < 100) { queue[c].add(tinst);// w w w . j av a2 s .c om } } System.err.println( "[Overflow] The binary stream is too skewed, could not get an example of class " + i + ""); System.exit(1); return null; } else { return queue[i].remove(); } }
From source file:moa.tasks.EvaluateConceptDrift.java
License:Open Source License
@Override protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) { ChangeDetectorLearner learner = (ChangeDetectorLearner) getPreparedClassOption(this.learnerOption); ConceptDriftGenerator stream = (ConceptDriftGenerator) getPreparedClassOption(this.streamOption); this.setEventsList(stream.getEventsList()); ClassificationPerformanceEvaluator evaluator = (ClassificationPerformanceEvaluator) getPreparedClassOption( this.evaluatorOption); LearningCurve learningCurve = new LearningCurve("learning evaluation instances"); learner.setModelContext(stream.getHeader()); int maxInstances = this.instanceLimitOption.getValue(); long instancesProcessed = 0; int maxSeconds = this.timeLimitOption.getValue(); int secondsElapsed = 0; monitor.setCurrentActivity("Evaluating learner...", -1.0); File dumpFile = this.dumpFileOption.getFile(); PrintStream immediateResultStream = null; if (dumpFile != null) { try {//from ww w .ja v a 2 s . co m if (dumpFile.exists()) { immediateResultStream = new PrintStream(new FileOutputStream(dumpFile, true), true); } else { immediateResultStream = new PrintStream(new FileOutputStream(dumpFile), true); } } catch (Exception ex) { throw new RuntimeException("Unable to open immediate result file: " + dumpFile, ex); } } //File for output predictions /* File outputPredictionFile = this.outputPredictionFileOption.getFile(); PrintStream outputPredictionResultStream = null; if (outputPredictionFile != null) { try { if (outputPredictionFile.exists()) { outputPredictionResultStream = new PrintStream( new FileOutputStream(outputPredictionFile, true), true); } else { outputPredictionResultStream = new PrintStream( new FileOutputStream(outputPredictionFile), true); } } catch (Exception ex) { throw new RuntimeException( "Unable to open prediction result file: " + outputPredictionFile, ex); } }*/ boolean firstDump = true; boolean preciseCPUTiming = TimingUtils.enablePreciseTiming(); long evaluateStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread(); long lastEvaluateStartTime = evaluateStartTime; double RAMHours = 0.0; while (stream.hasMoreInstances() && ((maxInstances < 0) || (instancesProcessed < maxInstances)) && ((maxSeconds < 0) || (secondsElapsed < maxSeconds))) { Instance trainInst = stream.nextInstance(); Instance testInst = (Instance) trainInst.copy(); int trueClass = (int) trainInst.classValue(); //testInst.setClassMissing(); double[] prediction = learner.getVotesForInstance(testInst); if (prediction[0] == 1) { //Change detected this.getEventsList().add(new ClusterEvent(this, instancesProcessed, "Detected Change", "Drift")); } // Output prediction /* if (outputPredictionFile != null) { outputPredictionResultStream.println(Utils.maxIndex(prediction) + "," + trueClass); }*/ //evaluator.addClassificationAttempt(trueClass, prediction, testInst.weight()); evaluator.addResult(testInst, prediction); learner.trainOnInstance(trainInst); instancesProcessed++; if (instancesProcessed % this.sampleFrequencyOption.getValue() == 0 || stream.hasMoreInstances() == false) { long evaluateTime = TimingUtils.getNanoCPUTimeOfCurrentThread(); double time = TimingUtils.nanoTimeToSeconds(evaluateTime - evaluateStartTime); double timeIncrement = TimingUtils.nanoTimeToSeconds(evaluateTime - lastEvaluateStartTime); double RAMHoursIncrement = learner.measureByteSize() / (1024.0 * 1024.0 * 1024.0); //GBs RAMHoursIncrement *= (timeIncrement / 3600.0); //Hours RAMHours += RAMHoursIncrement; lastEvaluateStartTime = evaluateTime; learningCurve.insertEntry(new LearningEvaluation( new Measurement[] { new Measurement("learning evaluation instances", instancesProcessed), new Measurement("evaluation time (" + (preciseCPUTiming ? "cpu " : "") + "seconds)", time), new Measurement("model cost (RAM-Hours)", RAMHours) }, evaluator, learner)); if (immediateResultStream != null) { if (firstDump) { immediateResultStream.println(learningCurve.headerToString()); firstDump = false; } immediateResultStream.println(learningCurve.entryToString(learningCurve.numEntries() - 1)); immediateResultStream.flush(); } } if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) { if (monitor.taskShouldAbort()) { return null; } long estimatedRemainingInstances = stream.estimatedRemainingInstances(); if (maxInstances > 0) { long maxRemaining = maxInstances - instancesProcessed; if ((estimatedRemainingInstances < 0) || (maxRemaining < estimatedRemainingInstances)) { estimatedRemainingInstances = maxRemaining; } } monitor.setCurrentActivityFractionComplete(estimatedRemainingInstances < 0 ? -1.0 : (double) instancesProcessed / (double) (instancesProcessed + estimatedRemainingInstances)); if (monitor.resultPreviewRequested()) { monitor.setLatestResultPreview(learningCurve.copy()); } secondsElapsed = (int) TimingUtils .nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread() - evaluateStartTime); } } if (immediateResultStream != null) { immediateResultStream.close(); } /* if (outputPredictionResultStream != null) { outputPredictionResultStream.close(); }*/ return learningCurve; }