📄 evaluation.java
字号:
* @param instance the test instance to be classified
* @exception Exception if model could not be evaluated
* successfully
*/
public void evaluateModelOnce(double prediction,
Instance instance) throws Exception {
if (m_ClassIsNominal) {
updateStatsForClassifier(makeDistribution(prediction),
instance);
} else {
updateStatsForPredictor(prediction, instance);
}
}
/**
* Wraps a static classifier in enough source to test using the weka
* class libraries.
*
* @param classifier a Sourcable Classifier
* @param className the name to give to the source code class
* @return the source for a static classifier that can be tested with
* weka libraries.
*/
protected static String wekaStaticWrapper(Sourcable classifier,
String className)
throws Exception {
//String className = "StaticClassifier";
String staticClassifier = classifier.toSource(className);
return "package weka.classifiers;\n"
+"import weka.core.Attribute;\n"
+"import weka.core.Instance;\n"
+"import weka.core.Instances;\n"
+"import weka.classifiers.Classifier;\n\n"
+"public class WekaWrapper extends Classifier {\n\n"
+" public void buildClassifier(Instances i) throws Exception {\n"
+" }\n\n"
+" public double classifyInstance(Instance i) throws Exception {\n\n"
+" Object [] s = new Object [i.numAttributes()];\n"
+" for (int j = 0; j < s.length; j++) {\n"
+" if (!i.isMissing(j)) {\n"
+" if (i.attribute(j).type() == Attribute.NOMINAL) {\n"
+" s[j] = i.attribute(j).value((int) i.value(j));\n"
+" } else if (i.attribute(j).type() == Attribute.NUMERIC) {\n"
+" s[j] = new Double(i.value(j));\n"
+" }\n"
+" }\n"
+" }\n"
+" return " + className + ".classify(s);\n"
+" }\n\n"
+"}\n\n"
+staticClassifier; // The static classifer class
}
/**
* Gets the number of test instances that had a known class value
* (actually the sum of the weights of test instances with known
* class value).
*
* @return the number of test instances with known class
*/
public final double numInstances() {
return m_WithClass;
}
/**
* Gets the number of instances incorrectly classified (that is, for
* which an incorrect prediction was made). (Actually the sum of the weights
* of these instances)
*
* @return the number of incorrectly classified instances
*/
public final double incorrect() {
return m_Incorrect;
}
/**
* Gets the percentage of instances incorrectly classified (that is, for
* which an incorrect prediction was made).
*
* @return the percent of incorrectly classified instances
* (between 0 and 100)
*/
public final double pctIncorrect() {
return 100 * m_Incorrect / m_WithClass;
}
/**
* Gets the total cost, that is, the cost of each prediction times the
* weight of the instance, summed over all instances.
*
* @return the total cost
*/
public final double totalCost() {
return m_TotalCost;
}
/**
* Gets the average cost, that is, total cost of misclassifications
* (incorrect plus unclassified) over the total number of instances.
*
* @return the average cost.
*/
public final double avgCost() {
return m_TotalCost / m_WithClass;
}
/**
* Gets the number of instances correctly classified (that is, for
* which a correct prediction was made). (Actually the sum of the weights
* of these instances)
*
* @return the number of correctly classified instances
*/
public final double correct() {
return m_Correct;
}
/**
* Gets the percentage of instances correctly classified (that is, for
* which a correct prediction was made).
*
* @return the percent of correctly classified instances (between 0 and 100)
*/
public final double pctCorrect() {
return 100 * m_Correct / m_WithClass;
}
/**
* Gets the number of instances not classified (that is, for
* which no prediction was made by the classifier). (Actually the sum
* of the weights of these instances)
*
* @return the number of unclassified instances
*/
public final double unclassified() {
return m_Unclassified;
}
/**
* Gets the percentage of instances not classified (that is, for
* which no prediction was made by the classifier).
*
* @return the percent of unclassified instances (between 0 and 100)
*/
public final double pctUnclassified() {
return 100 * m_Unclassified / m_WithClass;
}
/**
* Returns the estimated error rate or the root mean squared error
* (if the class is numeric). If a cost matrix was given this
* error rate gives the average cost.
*
* @return the estimated error rate (between 0 and 1, or between 0 and
* maximum cost)
*/
public final double errorRate() {
if (!m_ClassIsNominal) {
return Math.sqrt(m_SumSqrErr / m_WithClass);
}
if (m_CostMatrix == null) {
return m_Incorrect / m_WithClass;
} else {
return avgCost();
}
}
/**
* Returns value of kappa statistic if class is nominal.
*
* @return the value of the kappa statistic
*/
public final double kappa() {
double[] sumRows = new double[m_ConfusionMatrix.length];
double[] sumColumns = new double[m_ConfusionMatrix.length];
double sumOfWeights = 0;
for (int i = 0; i < m_ConfusionMatrix.length; i++) {
for (int j = 0; j < m_ConfusionMatrix.length; j++) {
sumRows[i] += m_ConfusionMatrix[i][j];
sumColumns[j] += m_ConfusionMatrix[i][j];
sumOfWeights += m_ConfusionMatrix[i][j];
}
}
double correct = 0, chanceAgreement = 0;
for (int i = 0; i < m_ConfusionMatrix.length; i++) {
chanceAgreement += (sumRows[i] * sumColumns[i]);
correct += m_ConfusionMatrix[i][i];
}
chanceAgreement /= (sumOfWeights * sumOfWeights);
correct /= sumOfWeights;
if (chanceAgreement < 1) {
return (correct - chanceAgreement) / (1 - chanceAgreement);
} else {
return 1;
}
}
/**
* Returns the correlation coefficient if the class is numeric.
*
* @return the correlation coefficient
* @exception Exception if class is not numeric
*/
public final double correlationCoefficient() throws Exception {
if (m_ClassIsNominal) {
throw
new Exception("Can't compute correlation coefficient: " +
"class is nominal!");
}
double correlation = 0;
double varActual =
m_SumSqrClass - m_SumClass * m_SumClass / m_WithClass;
double varPredicted =
m_SumSqrPredicted - m_SumPredicted * m_SumPredicted /
m_WithClass;
double varProd =
m_SumClassPredicted - m_SumClass * m_SumPredicted / m_WithClass;
if (Utils.smOrEq(varActual * varPredicted, 0.0)) {
correlation = 0.0;
} else {
correlation = varProd / Math.sqrt(varActual * varPredicted);
}
return correlation;
}
/**
* Returns the mean absolute error. Refers to the error of the
* predicted values for numeric classes, and the error of the
* predicted probability distribution for nominal classes.
*
* @return the mean absolute error
*/
public final double meanAbsoluteError() {
return m_SumAbsErr / m_WithClass;
}
/**
* Returns the mean absolute error of the prior.
*
* @return the mean absolute error
*/
public final double meanPriorAbsoluteError() {
return m_SumPriorAbsErr / m_WithClass;
}
/**
* Returns the relative absolute error.
*
* @return the relative absolute error
* @exception Exception if it can't be computed
*/
public final double relativeAbsoluteError() throws Exception {
return 100 * meanAbsoluteError() / meanPriorAbsoluteError();
}
/**
* Returns the root mean squared error.
*
* @return the root mean squared error
*/
public final double rootMeanSquaredError() {
return Math.sqrt(m_SumSqrErr / m_WithClass);
}
/**
* Returns the root mean prior squared error.
*
* @return the root mean prior squared error
*/
public final double rootMeanPriorSquaredError() {
return Math.sqrt(m_SumPriorSqrErr / m_WithClass);
}
/**
* Returns the root relative squared error if the class is numeric.
*
* @return the root relative squared error
*/
public final double rootRelativeSquaredError() {
return 100.0 * rootMeanSquaredError() /
rootMeanPriorSquaredError();
}
/**
* Calculate the entropy of the prior distribution
*
* @return the entropy of the prior distribution
* @exception Exception if the class is not nominal
*/
public final double priorEntropy() throws Exception {
if (!m_ClassIsNominal) {
throw
new Exception("Can't compute entropy of class prior: " +
"class numeric!");
}
double entropy = 0;
for(int i = 0; i < m_NumClasses; i++) {
entropy -= m_ClassPriors[i] / m_ClassPriorsSum
* Utils.log2(m_ClassPriors[i] / m_ClassPriorsSum);
}
return entropy;
}
/**
* Return the total Kononenko & Bratko Information score in bits
*
* @return the K&B information score
* @exception Exception if the class is not nominal
*/
public final double KBInformation() throws Exception {
if (!m_ClassIsNominal) {
throw
new Exception("Can't compute K&B Info score: " +
"class numeric!");
}
return m_SumKBInfo;
}
/**
* Return the Kononenko & Bratko Information score in bits per
* instance.
*
* @return the K&B information score
* @exception Exception if the class is not nominal
*/
public final double KBMeanInformation() throws Exception {
if (!m_ClassIsNominal) {
throw
new Exception("Can't compute K&B Info score: "
+ "class numeric!");
}
return m_SumKBInfo / m_WithClass;
}
/**
* Return the Kononenko & Bratko Relative Information score
*
* @return the K&B relative information score
* @exception Exception if the class is not nominal
*/
public final double KBRelativeInformation() throws Exception {
if (!m_ClassIsNominal) {
throw
new Exception("Can't compute K&B Info score: " +
"class numeric!");
}
return 100.0 * KBInformation() / priorEntropy();
}
/**
* Returns the total entropy for the null model
*
* @return the total null model entropy
*/
public final double SFPriorEntropy() {
return m_SumPriorEntropy;
}
/**
* Returns the entropy per instance for the null model
*
* @return the null model entropy per instance
*/
public final double SFMeanPriorEntropy() {
return m_SumPriorEntropy / m_WithClass;
}
/**
* Returns the total entropy for the scheme
*
* @return the total scheme entropy
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -