📄 confusionmatrix.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.performance;
import edu.udo.cs.yale.tools.math.Averagable;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.operator.OperatorException;
/** A confusion matrix.
*
* @version $Id: ConfusionMatrix.java,v 2.11 2004/08/27 11:57:43 ingomierswa Exp $
*/
public class ConfusionMatrix extends MeasuredPerformance {
private double threshold;
private double thresholdLabel;
private double[][] costMatrix;
private double totalCost;
private int exampleCount;
/** If the (predicted) label exceeds the threshold is considered to be positive, otherwise negative.
* If the classification is correct, the profit is increased by pp or nn, if the label is positive
* and the predicted label is negative, the profit is increased by pn. In the opposite case the profit
* is increased by np. */
public ConfusionMatrix(double threshold, double thresholdLabel, double pp, double pn, double np, double nn) {
this.threshold = threshold;
this.thresholdLabel = thresholdLabel;
this.costMatrix = new double[2][2];
this.costMatrix[0][0] = pp;
this.costMatrix[0][1] = pn;
this.costMatrix[1][0] = np;
this.costMatrix[1][1] = nn;
}
public String getName() {
return "data mining performance";
}
// private ConfusionMatrix(ConfusionMatrix c) {
// this.threshold = c.threshold;
// this.thresholdLabel = c.thresholdLabel;
// this.costMatrix = new double[2][2];
// this.costMatrix[0][0] = c.costMatrix[0][0];
// this.costMatrix[0][1] = c.costMatrix[0][1];
// this.costMatrix[1][0] = c.costMatrix[1][0];
// this.costMatrix[1][1] = c.costMatrix[1][1];
// }
public void startCounting(ExampleSet set) throws OperatorException {
super.startCounting(set);
totalCost = 0.0;
}
public double getFitness() {
return getValue();
}
public double getValue() {
return totalCost / exampleCount;
}
public double getVariance() {
return Double.NaN;
}
public void countExample(Example e) {
int l = e.getLabel() >= thresholdLabel ? 0 : 1;
int p = e.getPredictedLabel() >= threshold ? 0 : 1;
totalCost += costMatrix[l][p];
exampleCount++;
}
public int compareTo(Object o) {
double dif = ((ConfusionMatrix)o).getValue() - getValue();
if (dif < 0) return 1;
if (dif > 0) return -1;
return 0;
}
// public Object clone() {
// return new ConfusionMatrix(this);
// }
protected void cloneAveragable(Averagable avg) {
ConfusionMatrix c = (ConfusionMatrix)avg;
this.threshold = c.threshold;
this.thresholdLabel = c.thresholdLabel;
this.costMatrix = new double[2][2];
this.costMatrix[0][0] = c.costMatrix[0][0];
this.costMatrix[0][1] = c.costMatrix[0][1];
this.costMatrix[1][0] = c.costMatrix[1][0];
this.costMatrix[1][1] = c.costMatrix[1][1];
}
public double getThreshold() { return threshold; }
public String toString() {
return super.toString() + " (threshold="+threshold+")";
}
public void buildAverage(Averagable performance) {
super.buildAverage(performance);
ConfusionMatrix other = (ConfusionMatrix)performance;
this.totalCost += other.totalCost;
this.exampleCount += other.exampleCount;
}
public String getDescription() {
return "A confusion matrix";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -