Skip to content

Commit

Permalink
Fix for precision, recall and F1 computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown committed Jan 30, 2018
1 parent 4389f0a commit f83f9af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ public void addResult(Example<Instance> example, double[] classVotes) {
for (int i = 0; i < this.numClasses; i++) {
this.rowKappa[i].add(predictedClass == i ? weight : 0);
this.columnKappa[i].add(trueClass == i ? weight : 0);
// for both precision and recall, NaN values are used to 'balance' the number
// of instances seen across classes
if (predictedClass == i) {
precision[i].add(predictedClass == trueClass ? weight : 0.0);
}
} else precision[i].add(Double.NaN);
if (trueClass == i) {
recall[i].add(predictedClass == trueClass ? weight : 0.0);
}
} else recall[i].add(Double.NaN);
}
}
this.weightCorrectNoChangeClassifier.add(this.lastSeenClass == trueClass ? weight : 0);
Expand Down Expand Up @@ -310,8 +312,10 @@ public class BasicEstimator implements Estimator {

@Override
public void add(double value) {
sum += value;
len++;
if(!Double.isNaN(value)) {
sum += value;
len++;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@
import com.yahoo.labs.samoa.instances.InstanceData;
import com.yahoo.labs.samoa.instances.Prediction;

import java.util.LinkedList;

/**
* Classification evaluator that updates evaluation results using a sliding
* window.
*
* @author Albert Bifet (abifet at cs dot waikato dot ac dot nz)
* @version $Revision: 7 $
* @author Jean Paul Barddal (jpbarddal@gmail.com)
* @version $Revision: 8 $
*
*
*/
public class WindowClassificationPerformanceEvaluator extends BasicClassificationPerformanceEvaluator {

Expand All @@ -54,38 +59,34 @@ protected Estimator newEstimator() {

public class WindowEstimator implements Estimator {

protected double[] window;

protected int posWindow;

protected int lenWindow;

protected int SizeWindow;
protected LinkedList<Double> window;

protected int sizeWindow;
protected double sum;
// NaN values are used to 'balance' the number of instances observed across classes (precision and recall only)
protected double qtyNaNs;

public WindowEstimator(int sizeWindow) {
window = new double[sizeWindow];
SizeWindow = sizeWindow;
posWindow = 0;
lenWindow = 0;
sum = 0.0;
qtyNaNs = 0.0;
this.sizeWindow = sizeWindow;
this.window = new LinkedList<>();
}

public void add(double value) {
sum -= window[posWindow];
sum += value;
window[posWindow] = value;
posWindow++;
if (posWindow == SizeWindow) {
posWindow = 0;
}
if (lenWindow < SizeWindow) {
lenWindow++;
window.add(value);
if(!Double.isNaN(value)) sum += value;
else qtyNaNs++;
if(window.size() > sizeWindow){
double forget = window.removeFirst();
if(!Double.isNaN(forget)) sum -= forget;
else qtyNaNs--;
}

}

public double estimation(){
return sum/(double) lenWindow;
return sum / (double) (window.size() - qtyNaNs);
}

}
Expand Down

0 comments on commit f83f9af

Please sign in to comment.