📄 cattestresult.java
字号:
if (computeLogLoss) {
double logLoss =
log_loss(predictedDist, correctCat) * weight;
metrics.totalLogLoss += logLoss;
} else
metrics.totalLogLoss = 0;
}
/** Computes the log-loss of the given distribution and correct
* category. Aborts if the correct category is assigned
* probability 0 (infinite log loss).
* @return
* @param predDist
* @param correctCat
*/
private double log_loss(CatDist predDist,
AugCategory correctCat) {
double predProb = predDist.get_scores()[correctCat.num()];
if (MLJ.approx_equal(predProb, 0.0))
Error.fatalErr("CatTestResult::log_loss: the correct category " +correctCat
+" was assigned probability zero by the predicted distribution "
+predDist );
return -MLJ.log_bin(predProb);
}
/** Return the number of instances in the test InstanceList that were
* correctly categorized.
* @return An integer representing the number of instances that were correctly
* categorized by an inducer during a test run.
*/
public int num_correct() {
return metrics.numCorrect;
}
/** Return the number of instances in the test InstanceList that were
* incorrectly categorized.
* @return An integer representing the number of instances that were incorrectly
* categorized by an inducer during a test run.
*/
public int num_incorrect() {
return num_test_instances() - num_correct();
}
/** Returns the number of test instances appearing in appearing in the
* training data. Initializes flag for each test instance if not already
* done.
* @return The number of test instances also in the training data.
*/
public int num_on_train() {
if ( ! inTrainingSetInitialized )
initializeTrainTable();
return numOnTrain;
}
/** Returns the number of test instances not appearing in appearing in the
* training data. Initializes flag for each test instance if not already
* done.
* @return The number of test instances not in the training data.
*/
public int num_off_train() {
return num_test_instances() - num_on_train();
}
/** Returns the number of instances in the training set. This function
* ignores weights of instances.
* @return The number of training instances.
*/
public int num_train_instances() {
return trainIL.num_instances();
}
/** Returns the number of instances in the testing set. This function
* ignores weights of instances.
* @return The number of test instances.
*/
public int num_test_instances() {
return testIL.num_instances();
}
/** Returns the total weight of instances which were correctly
* classified.
* @return The total weight of correct instances.
*/
public double total_correct_weight() {
return metrics.weightCorrect;
}
/** Returns the total weight in the training list.
* @return The total weight in the training list.
*/
public double total_train_weight() {
return trainIL.total_weight();
}
//Returns various measures of scoring performance. These metrics attempt
//to validate the probability distribution returned by a the Categorizer's
//score() function.
/** Returns the total mean squared error value.
* @return The total mean squared error value.
*/
double total_mean_squared_error() {
return metrics.meanSquaredError;
}
/** Returns the total mean absolute error value.
* @return The total mean absolute error value.
*/
double total_mean_absolute_error() {
return metrics.meanAbsoluteError;
}
/** Computes the estimated standard deviation according to the binomial model,
* which assumes every test instance is a Bernoulli trial, thus
* std-dev=sqrt(acc*(1-acc)/(n-1))
* @return The estimated standard deviation.
* @param error Error value for which deviation measure is requested.
* @param n The number of samples over which the deviation is requested.
*/
static double theoretical_std_dev(double error, double n) {
// StoredReal storedRealWeight = (StoredReal)n;
// mlc.clamp_above(storedRealWeight, (StoredReal)2.0,
// "CatTestResult:: std-dev of less than "
// "2.0 total weight is undefined");
DoubleRef storedRealWeight = new DoubleRef(n);
MLJ.clamp_above(storedRealWeight, (double)2.0,
"CatTestResult:: std-dev of less than "
+"2.0 total weight is undefined");
return Math.sqrt(error*(1-error)/(storedRealWeight.value-1));
}
/* Standardized display for performances (expected to be in percentages)
public BufferedWriter perf_display(BufferedWriter stream) {
stream.write(setprecision(GlobalOptions::printPerfPrecision) + fixed_reals);
return stream;
}
*/
/** Returns the total log loss recorded for this Inducer run.
* @return The total log loss.
*/
public double total_log_loss() {
return metrics.totalLogLoss;
}
/** Determines whether unknown classes are used. or example, some test
* instance was classified as 'unknown' or there are some test instances
* that is of 'unknown' class. Result is to set step and start values
* for use in display_ascii_confusion_matrix and
* display_scatterviz_confusion_matrix.
* @param step The step value of where to begin in the
* confusion matrix.
* @param start The start value of where to begin in the
* confusion matrix.
* @param confusionMatrix The confusion matrix which is being checked
* for unknown values.
*/
static public void check_for_unknown_classes(int step, int start,
double[][] confusionMatrix) {
boolean hasUnknowns = false;
int i;
for (i = 0; i < confusionMatrix.length;
i++)
if (confusionMatrix[i][0] > 0) {
hasUnknowns = true;
break;
}
if (!hasUnknowns)
for (i = 0 + 1;
i < confusionMatrix[0].length; i++)
if (confusionMatrix[0][i] > 0) {
hasUnknowns = true;
break;
}
// set depending variables.
if (hasUnknowns) {
step = 0;
start = Globals.UNKNOWN_CATEGORY_VAL;
}
else {
step = 1;
start = Globals.FIRST_CATEGORY_VAL;
}
}
/** Displays confusion matrices in ascii format for this CatTestResult object.
* @return The String containing the display of the confusion matrix.
* @param stream Stream to which display is shown.
*/
public String display_ascii_confusion_matrix(String stream) {
return display_ascii_confusion_matrix(stream, confusionMatrix, testIL.get_schema());
}
/** Displays confusion matrices in ascii format.
* @return The String containing the display of the confusion matrix.
* @param display The String containing any previous items to be
* included in the display.
* @param confusionMatrix The confusion matrix to be displayed.
* @param schema The Schema of the categories that Instances can be
* classified as.
*/
static public String display_ascii_confusion_matrix(String display,
double[][] confusionMatrix,
Schema schema) {
int numCategories = schema.num_label_values() + 1;
if (
//confusionMatrix.start_row() != Globals.UNKNOWN_CATEGORY_VAL ||
//confusionMatrix.start_col() != Globals.UNKNOWN_CATEGORY_VAL ||
confusionMatrix.length != numCategories ||
confusionMatrix[0].length != numCategories)
Error.fatalErr("CatTestResult::display_ascii_confusion_matrix: the given "
+"confusion matrix, of dimension (0"
+".." +confusionMatrix.length +") x (0"
+".." +confusionMatrix[0].length
+") is not compatible with the given schema, which has "
+numCategories +" label values (including UNKNOWN)");
display = display + "\nDisplaying confusion matrix... " + "\n";
// display = display + push_opts + setprecision(3);
int row, col;
int i;
// int step = 0 + Globals.FIRST_CATEGORY_VAL;
// int start = 0 + Globals.FIRST_CATEGORY_VAL;
int step = 0;
int start = 0;
check_for_unknown_classes(step, start, confusionMatrix);
for (i = start, row = 0 + step;
row < confusionMatrix.length; row++, i++)
if (i == Globals.UNKNOWN_CATEGORY_VAL)
display = display + " (?) ";
else
display = display + " (" + (char)('a'+i- Globals.FIRST_CATEGORY_VAL) + ") ";
display = display + " <-- classified as " + "\n";
for (row = 0 + step; row < confusionMatrix.length; row++)
display = display + "-------- ";
display = display + "\n";
for (i = start, row = 0 + step;
row < confusionMatrix.length /*- Globals.FIRST_CATEGORY_VAL*/; i++, row++) {
for (col = 0 + step;
col < confusionMatrix[0].length /*- Globals.FIRST_CATEGORY_VAL*/; col++) {
// String confStr(confusionMatrix(row, col), 2, 0,
// MString::mlcFixed);
// display = display + " " + confusionMatrix[row][col] + " ";
display = display + MLJ.numberToString(confusionMatrix[row][col],8,2) + " ";
}
AttrValue val = new AttrValue();
schema.nominal_label_info().set_nominal_val(val,i);
// Globals.FIRST_CATEGORY_VAL + i);
if (i == Globals.UNKNOWN_CATEGORY_VAL)
display = display + " (?): unknown class " + "\n";
else
display = display + " (" + (char)('a' + i - Globals.FIRST_CATEGORY_VAL) + "): class "
+ schema.nominal_label_info().attrValue_to_string(val)
+ "\n";
}
/*
display = display + pop_opts;
*/
return display;
}
/** Displays the confusion matrix. The confusion matrix displays for row i
* column j, the number of instances classified as j that should have been
* classified as i.
* @param stream Stream to which display is shown.
* @return String containing the display.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -