📄 evaluation.java
字号:
}
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
* @throws 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 (varActual * varPredicted <= 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() {
if (m_NoPriors)
return Double.NaN;
return m_SumPriorAbsErr / m_WithClass;
}
/**
* Returns the relative absolute error.
*
* @return the relative absolute error
* @throws Exception if it can't be computed
*/
public final double relativeAbsoluteError() throws Exception {
if (m_NoPriors)
return Double.NaN;
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() {
if (m_NoPriors)
return Double.NaN;
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() {
if (m_NoPriors)
return Double.NaN;
return 100.0 * rootMeanSquaredError() /
rootMeanPriorSquaredError();
}
/**
* Calculate the entropy of the prior distribution
*
* @return the entropy of the prior distribution
* @throws 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!");
}
if (m_NoPriors)
return Double.NaN;
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
* @throws 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!");
}
if (m_NoPriors)
return Double.NaN;
return m_SumKBInfo;
}
/**
* Return the Kononenko & Bratko Information score in bits per
* instance.
*
* @return the K&B information score
* @throws 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!");
}
if (m_NoPriors)
return Double.NaN;
return m_SumKBInfo / m_WithClass;
}
/**
* Return the Kononenko & Bratko Relative Information score
*
* @return the K&B relative information score
* @throws 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!");
}
if (m_NoPriors)
return Double.NaN;
return 100.0 * KBInformation() / priorEntropy();
}
/**
* Returns the total entropy for the null model
*
* @return the total null model entropy
*/
public final double SFPriorEntropy() {
if (m_NoPriors)
return Double.NaN;
return m_SumPriorEntropy;
}
/**
* Returns the entropy per instance for the null model
*
* @return the null model entropy per instance
*/
public final double SFMeanPriorEntropy() {
if (m_NoPriors)
return Double.NaN;
return m_SumPriorEntropy / m_WithClass;
}
/**
* Returns the total entropy for the scheme
*
* @return the total scheme entropy
*/
public final double SFSchemeEntropy() {
if (m_NoPriors)
return Double.NaN;
return m_SumSchemeEntropy;
}
/**
* Returns the entropy per instance for the scheme
*
* @return the scheme entropy per instance
*/
public final double SFMeanSchemeEntropy() {
if (m_NoPriors)
return Double.NaN;
return m_SumSchemeEntropy / m_WithClass;
}
/**
* Returns the total SF, which is the null model entropy minus
* the scheme entropy.
*
* @return the total SF
*/
public final double SFEntropyGain() {
if (m_NoPriors)
return Double.NaN;
return m_SumPriorEntropy - m_SumSchemeEntropy;
}
/**
* Returns the SF per instance, which is the null model entropy
* minus the scheme entropy, per instance.
*
* @return the SF per instance
*/
public final double SFMeanEntropyGain() {
if (m_NoPriors)
return Double.NaN;
return (m_SumPriorEntropy - m_SumSchemeEntropy) / m_WithClass;
}
/**
* Output the cumulative margin distribution as a string suitable
* for input for gnuplot or similar package.
*
* @return the cumulative margin distribution
* @throws Exception if the class attribute is nominal
*/
public String toCumulativeMarginDistributionString() throws Exception {
if (!m_ClassIsNominal) {
throw new Exception("Class must be nominal for margin distributions");
}
String result = "";
double cumulativeCount = 0;
double margin;
for(int i = 0; i <= k_MarginResolution; i++) {
if (m_MarginCounts[i] != 0) {
cumulativeCount += m_MarginCounts[i];
margin = (double)i * 2.0 / k_MarginResolution - 1.0;
result = result + Utils.doubleToString(margin, 7, 3) + ' '
+ Utils.doubleToString(cumulativeCount * 100
/ m_WithClass, 7, 3) + '\n';
} else if (i == 0) {
result = Utils.doubleToString(-1.0, 7, 3) + ' '
+ Utils.doubleToString(0, 7, 3) + '\n';
}
}
return result;
}
/**
* Calls toSummaryString() with no title and no complexity stats
*
* @return a summary description of the classifier evaluation
*/
public String toSummaryString() {
return toSummaryString("", false);
}
/**
* Calls toSummaryString() with a default title.
*
* @param printComplexityStatistics if true, complexity statistics are
* returned as well
* @return the summary string
*/
public String toSummaryString(boolean printComplexityStatistics) {
return toSummaryString("=== Summary ===\n", printComplexityStatistics);
}
/**
* Outputs the performance statistics in summary form. Lists
* number (and percentage) of instances classified correctly,
* incorrectly and unclassified. Outputs the total number of
* instances classified, and the number of instances (if any)
* that had no class value provided.
*
* @param title the title for the statistics
* @param printComplexityStatistics if true, complexity statistics are
* returned as well
* @return the summary as a String
*/
public String toSummaryString(String title,
boolean printComplexityStatistics) {
StringBuffer text = new StringBuffer();
text.append(title + "\n");
try {
if (m_WithClass > 0) {
if (m_ClassIsNominal) {
text.append("Correctly Classified Instances ");
text.append(Utils.doubleToString(correct(), 12, 4) + " " +
Utils.doubleToString(pctCorrect(),
12, 4) + " %\n");
text.append("Incorrectly Classified Instances ");
text.append(Utils.doubleToString(incorrect(), 12, 4) + " " +
Utils.doubleToString(pctIncorrect(),
12, 4) + " %\n");
text.append("Kappa statistic ");
text.append(Utils.doubleToString(kappa(), 12, 4) + "\n");
if (m_CostMatrix != null) {
text.append("Total Cost ");
text.append(Utils.doubleToString(totalCost(), 12, 4) + "\n");
text.append("Average Cost ");
text.append(Utils.doubleToString(avgCost(), 12, 4) + "\n");
}
if (printComplexityStatistics) {
text.append("K&B Relative Info Score ");
text.append(Utils.doubleToString(KBRelativeInformation(), 12, 4)
+ " %\n");
text.append("K&B Information Score ");
text.append(Utils.doubleToString(KBInformation(), 12, 4)
+ " bits");
text.append(Utils.doubleToString(KBMeanInformation(), 12, 4)
+ " bits/instance\n");
}
} else {
text.append("Correlation coefficient ");
text.append(Utils.doubleToString(correlationCoefficient(), 12 , 4) +
"\n");
}
if (printComplexityStatistics) {
text.append("Class complexity | order 0 ");
text.append(Utils.doubleToString(SFPriorEntropy(), 12, 4)
+ " bits");
text.append(Utils.doubleToString(SFMeanPriorEntropy(), 12, 4)
+ " bits/instance\n");
text.append("Class complexity | scheme ");
text.append(Utils.doubleToString(SFSchemeEntropy(), 12, 4)
+ " bits");
text.append(Utils.doubleToString(SFMeanSchemeEntropy(), 12, 4)
+ " bits/instance\n");
text.append("Complexity improvement (Sf) ");
text.append(Utils.doubleToString(SFEntropyGain(), 12, 4) + " bits");
text.append(Utils.doubleToString(SFMeanEntropyGain(), 12, 4)
+ " bits/instance\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -