Add train classification report
This commit is contained in:
@@ -102,6 +102,7 @@ namespace platform {
|
||||
auto edges = torch::zeros({ nResults }, torch::kFloat64);
|
||||
auto num_states = torch::zeros({ nResults }, torch::kFloat64);
|
||||
json confusion_matrices = json::array();
|
||||
json confusion_matrices_train = json::array();
|
||||
std::vector<std::string> notes;
|
||||
Timer train_timer, test_timer;
|
||||
int item = 0;
|
||||
@@ -150,8 +151,12 @@ namespace platform {
|
||||
train_time[item] = train_timer.getDuration();
|
||||
double accuracy_train_value = 0.0;
|
||||
// Score train
|
||||
if (!no_train_score)
|
||||
accuracy_train_value = clf->score(X_train, y_train);
|
||||
if (!no_train_score) {
|
||||
auto y_predict = clf->predict(X_train);
|
||||
Scores scores(y_train, y_predict, states[className].size(), labels);
|
||||
accuracy_train_value = scores.accuracy();
|
||||
confusion_matrices_train.push_back(scores.get_confusion_matrix_json(true));
|
||||
}
|
||||
// Test model
|
||||
if (!quiet)
|
||||
showProgress(nfold + 1, getColor(clf->getStatus()), "c");
|
||||
@@ -183,6 +188,8 @@ namespace platform {
|
||||
partial_result.setNodes(torch::mean(nodes).item<double>()).setLeaves(torch::mean(edges).item<double>()).setDepth(torch::mean(num_states).item<double>());
|
||||
partial_result.setDataset(fileName).setNotes(notes);
|
||||
partial_result.setConfusionMatrices(confusion_matrices);
|
||||
if (!no_train_score)
|
||||
partial_result.setConfusionMatricesTrain(confusion_matrices_train);
|
||||
addResult(partial_result);
|
||||
}
|
||||
}
|
@@ -28,6 +28,7 @@ namespace platform {
|
||||
return *this;
|
||||
}
|
||||
PartialResult& setConfusionMatrices(const json& confusion_matrices) { data["confusion_matrices"] = confusion_matrices; return *this; }
|
||||
PartialResult& setConfusionMatricesTrain(const json& confusion_matrices) { data["confusion_matrices_train"] = confusion_matrices; return *this; }
|
||||
PartialResult& setHyperparameters(const json& hyperparameters) { data["hyperparameters"] = hyperparameters; return *this; }
|
||||
PartialResult& setSamples(int samples) { data["samples"] = samples; return *this; }
|
||||
PartialResult& setFeatures(int features) { data["features"] = features; return *this; }
|
||||
|
@@ -126,24 +126,28 @@ namespace platform {
|
||||
oss << std::setw(dlen) << std::setprecision(ndec) << std::fixed << recall << " ";
|
||||
}
|
||||
oss << std::setw(dlen) << std::setprecision(ndec) << std::fixed << f1_score << " "
|
||||
<< std::setw(dlen) << std::right << support << std::endl;
|
||||
<< std::setw(dlen) << std::right << support;
|
||||
return oss.str();
|
||||
}
|
||||
std::string Scores::classification_report(std::string color)
|
||||
std::vector<std::string> Scores::classification_report(std::string color, std::string title)
|
||||
{
|
||||
std::stringstream oss;
|
||||
std::vector<std::string> report;
|
||||
for (int i = 0; i < num_classes; i++) {
|
||||
label_len = std::max(label_len, (int)labels[i].size());
|
||||
}
|
||||
oss << Colors::GREEN() << "Classification Report" << std::endl;
|
||||
oss << "=====================" << std::endl << color;
|
||||
oss << std::string(label_len, ' ') << " precision recall f1-score support" << std::endl;
|
||||
oss << std::string(label_len, ' ') << " ========= ========= ========= =========" << std::endl;
|
||||
report.push_back("Classification Report using " + title + " dataset");
|
||||
report.push_back("=========================================");
|
||||
oss << std::string(label_len, ' ') << " precision recall f1-score support";
|
||||
report.push_back(oss.str()); oss.str("");
|
||||
oss << std::string(label_len, ' ') << " ========= ========= ========= =========";
|
||||
report.push_back(oss.str()); oss.str("");
|
||||
for (int i = 0; i < num_classes; i++) {
|
||||
oss << classification_report_line(labels[i], precision(i), recall(i), f1_score(i), confusion_matrix[i].sum().item<int>());
|
||||
report.push_back(classification_report_line(labels[i], precision(i), recall(i), f1_score(i), confusion_matrix[i].sum().item<int>()));
|
||||
}
|
||||
oss << std::endl;
|
||||
report.push_back(" ");
|
||||
oss << classification_report_line("accuracy", 0, 0, accuracy(), total);
|
||||
report.push_back(oss.str()); oss.str("");
|
||||
float precision_avg = 0;
|
||||
float recall_avg = 0;
|
||||
float precision_wavg = 0;
|
||||
@@ -159,10 +163,11 @@ namespace platform {
|
||||
recall_wavg /= total;
|
||||
precision_avg /= num_classes;
|
||||
recall_avg /= num_classes;
|
||||
oss << classification_report_line("macro avg", precision_avg, recall_avg, f1_macro(), total);
|
||||
oss << classification_report_line("weighted avg", precision_wavg, recall_wavg, f1_weighted(), total);
|
||||
oss << std::endl << Colors::GREEN() << "Confusion Matrix" << std::endl;
|
||||
oss << "================" << std::endl << color;
|
||||
report.push_back(classification_report_line("macro avg", precision_avg, recall_avg, f1_macro(), total));
|
||||
report.push_back(classification_report_line("weighted avg", precision_wavg, recall_wavg, f1_weighted(), total));
|
||||
report.push_back("");
|
||||
report.push_back("Confusion Matrix");
|
||||
report.push_back("================");
|
||||
auto number = total > 1000 ? 4 : 3;
|
||||
for (int i = 0; i < num_classes; i++) {
|
||||
oss << std::right << std::setw(label_len) << labels[i] << " ";
|
||||
@@ -171,10 +176,9 @@ namespace platform {
|
||||
oss << std::setw(number) << confusion_matrix[i][j].item<int>() << " ";
|
||||
if (i == j) oss << color;
|
||||
}
|
||||
oss << std::endl;
|
||||
report.push_back(oss.str()); oss.str("");
|
||||
}
|
||||
oss << Colors::RESET();
|
||||
return oss.str();
|
||||
return report;
|
||||
}
|
||||
json Scores::get_confusion_matrix_json(bool labels_as_keys)
|
||||
{
|
||||
|
@@ -17,7 +17,7 @@ namespace platform {
|
||||
float precision(int num_class);
|
||||
float recall(int num_class);
|
||||
torch::Tensor get_confusion_matrix() { return confusion_matrix; }
|
||||
std::string classification_report(std::string color = "");
|
||||
std::vector<std::string> classification_report(std::string color = "", std::string title = "");
|
||||
json get_confusion_matrix_json(bool labels_as_keys = false);
|
||||
void aggregate(const Scores& a);
|
||||
private:
|
||||
@@ -30,7 +30,7 @@ namespace platform {
|
||||
int total;
|
||||
std::vector<std::string> labels;
|
||||
torch::Tensor confusion_matrix; // Rows ar actual, columns are predicted
|
||||
int label_len = 12;
|
||||
int label_len = 16;
|
||||
int dlen = 9;
|
||||
int ndec = 7;
|
||||
};
|
||||
|
Reference in New Issue
Block a user