8 double accuracy(
int TP,
int TN,
int FP,
int FN,
int numberOfClasses,
int alpha)
10 double numerator = TP + TN + alpha;
11 double nominator = TP + TN + FP + FN + numberOfClasses;
12 return numerator/nominator;
15 double error(
int TP,
int TN,
int FP,
int FN,
int numberOfClasses,
int alpha)
17 double numerator = FP + FN + alpha;
18 double nominator = TP + TN + FP + FN + numberOfClasses;
19 return numerator/nominator;
23 double precision(
int TP,
int FP,
int numberOfClasses,
int alpha)
25 return (
static_cast<double>(TP) + alpha) / (TP + FP + numberOfClasses);
28 double sensitivity(
int TP,
int FN,
int numberOfClasses,
int alpha)
30 return (
static_cast<double>(TP) + alpha) / (TP + FN + numberOfClasses);
33 double fallout(
int FP,
int TN,
int numberOfClasses,
int alpha)
35 return (
static_cast<double>(FP) + alpha) / (FP + TN + numberOfClasses);
38 double specificity(
int TN,
int FP,
int numberOfClasses,
int alpha)
40 return (
static_cast<double>(TN) + alpha)/ (TN + FP + numberOfClasses);
43 double F1(
int TP,
int FP,
int FN,
int numberOfClasses,
int alpha)
45 double P = precision(TP,FP,numberOfClasses,alpha);
46 double R = sensitivity(TP,FN,numberOfClasses,alpha);
47 return (2 * P * R) / (P + R);
50 double FBeta(
int TP,
int FP,
int FN,
double beta,
int numberOfClasses,
int alpha)
52 double P = precision(TP,FP,numberOfClasses,alpha);
53 double R = sensitivity(TP,FN,numberOfClasses,alpha);
54 return ((1 + beta * beta) * P * R) / (beta * beta * P + R);
56 double returnConfusionMatrixResult(
const std::vector<std::vector<uint> >& confusionMatrix, uint size,
57 const std::string& measureUsed,
int alpha)
59 std::vector<double> calculatedMeasures;
60 uint numberOfClasses = confusionMatrix.size();
61 uint TP=0,TN=0,FP=0,FN=0;
62 for(uint i = 0; i < numberOfClasses; i++) {
63 TP += confusionMatrix[i][i];
64 for(uint j = 0; j < confusionMatrix[i].size(); j++) {
66 FP += confusionMatrix[i][j];
69 for(uint j = 0; j < confusionMatrix.size(); j++) {
71 FN += confusionMatrix[j][i];
74 TN = size - TP - FP - FN;
75 if(measureUsed ==
"f1" || measureUsed ==
"f1score" ) {
76 calculatedMeasures.push_back(F1(TP,FP,FN,numberOfClasses,alpha));
78 else if(measureUsed ==
"precision") {
79 calculatedMeasures.push_back(precision(TP,FP,numberOfClasses, alpha));
81 else if(measureUsed ==
"specificity") {
82 calculatedMeasures.push_back(specificity(TN,FP,numberOfClasses,alpha));
84 else if(measureUsed ==
"sensitivity" || measureUsed ==
"recall" || measureUsed ==
"true positive rate") {
85 calculatedMeasures.push_back(sensitivity(TP,FN,numberOfClasses,alpha));
87 else if(measureUsed ==
"fallout" || measureUsed ==
"false positive rate") {
88 calculatedMeasures.push_back(fallout(FP,TN,numberOfClasses,alpha));
90 else if(measureUsed ==
"error") {
91 calculatedMeasures.push_back(error(TP,TN,FP,FN,numberOfClasses,alpha));
94 calculatedMeasures.push_back(accuracy(TP,TN,FP,FN,numberOfClasses,alpha));
98 for(uint i = 0; i < calculatedMeasures.size(); i++) {
99 measure += calculatedMeasures[i];
101 return measure / calculatedMeasures.size();
104 double meanSquareError(
const std::vector<double>& trueOutput,
const std::vector<double>& calculatedOutput)
107 if(trueOutput.size() != calculatedOutput.size()) {
108 std::cerr <<
"Mean square error encountered an error. Calculated output and true output are not of same size.\n";
110 for(uint i = 0; i < trueOutput.size(); i++) {
111 sum += (trueOutput[i] - calculatedOutput[i]) * (trueOutput[i] - calculatedOutput[i]);
113 return sum / trueOutput.size();
116 double rootMeanSquareError(
const std::vector<double>& trueOutput,
const std::vector<double>& calculatedOutput)
119 if(trueOutput.size() != calculatedOutput.size()) {
120 std::cerr <<
"Root mean square error encountered an error. Calculated output and true output are not of same size.\n";
122 for(uint i = 0; i < trueOutput.size(); i++) {
123 sum += (trueOutput[i] - calculatedOutput[i]) * (trueOutput[i] - calculatedOutput[i]);
125 return std::sqrt(sum / trueOutput.size());