21 #ifndef otbConfusionMatrixMeasurements_hxx
22 #define otbConfusionMatrixMeasurements_hxx
28 template <
class TConfusionMatrix,
class TLabel>
31 m_OverallAccuracy(0.0),
32 m_FalseNegativeValue(0.0),
33 m_TrueNegativeValue(0.0),
34 m_FalsePositiveValue(0.0),
35 m_TruePositiveValue(0.0),
45 template <
class TConfusionMatrix,
class TLabel>
48 m_NumberOfClasses = m_ConfusionMatrix.Rows();
54 m_FalseNegativeValues.Fill(0);
55 m_FalsePositiveValues.Fill(0);
56 m_TruePositiveValues.Fill(0);
57 m_TrueNegativeValues.Fill(0);
59 const double epsilon = 0.0000000001;
60 double luckyRate = 0.;
61 this->m_NumberOfSamples = 0;
62 this->m_OverallAccuracy = 0.;
63 for (
unsigned int i = 0; i < m_NumberOfClasses; ++i)
65 double sum_along_row_i = 0.;
66 double sum_along_column_i = 0.;
67 for (
unsigned int j = 0; j < m_NumberOfClasses; ++j)
69 sum_along_row_i += m_ConfusionMatrix(i, j);
70 sum_along_column_i += m_ConfusionMatrix(j, i);
71 this->m_NumberOfSamples += m_ConfusionMatrix(i, j);
75 this->m_FalseNegativeValues[i] += m_ConfusionMatrix(i, j);
76 this->m_FalsePositiveValues[i] += m_ConfusionMatrix(j, i);
81 this->m_OverallAccuracy += m_ConfusionMatrix(i, i);
82 this->m_TruePositiveValues[i] = m_ConfusionMatrix(i, i);
85 luckyRate += sum_along_row_i * sum_along_column_i;
88 this->m_TrueNegativeValues.Fill(m_NumberOfSamples);
89 this->m_TrueNegativeValues -= this->m_FalseNegativeValues + this->m_FalsePositiveValues + this->m_TruePositiveValues;
91 if (m_NumberOfClasses == 2)
93 this->m_TruePositiveValue = m_ConfusionMatrix(0, 0);
94 this->m_FalseNegativeValue = m_ConfusionMatrix(0, 1);
95 this->m_FalsePositiveValue = m_ConfusionMatrix(1, 0);
96 this->m_TrueNegativeValue = m_ConfusionMatrix(1, 1);
113 m_Precisions.Fill(0.);
117 if (m_NumberOfSamples > 0)
119 this->m_OverallAccuracy /=
static_cast<double>(m_NumberOfSamples);
120 luckyRate /= std::pow(m_NumberOfSamples, 2.0);
122 if (std::abs(1 - luckyRate) > epsilon)
124 m_KappaIndex = (m_OverallAccuracy - luckyRate) / (1 - luckyRate);
128 this->m_KappaIndex = 1.;
133 this->m_OverallAccuracy = 0.;
134 this->m_KappaIndex = 0.;
139 for (
unsigned int i = 0; i < m_NumberOfClasses; ++i)
141 if (std::abs(this->m_TruePositiveValues[i] + this->m_FalsePositiveValues[i]) > epsilon)
143 this->m_Precisions[i] = this->m_TruePositiveValues[i] / (this->m_TruePositiveValues[i] + this->m_FalsePositiveValues[i]);
146 if (std::abs(this->m_TruePositiveValues[i] + this->m_FalseNegativeValues[i]) > epsilon)
148 this->m_Recalls[i] = this->m_TruePositiveValues[i] / (this->m_TruePositiveValues[i] + this->m_FalseNegativeValues[i]);
151 if (std::abs(this->m_Recalls[i] + this->m_Precisions[i]) > epsilon)
153 this->m_FScores[i] = 2 * this->m_Recalls[i] * this->m_Precisions[i] / (this->m_Recalls[i] + this->m_Precisions[i]);
158 if (m_NumberOfClasses == 2)
160 if (std::abs(this->m_TruePositiveValue + this->m_FalsePositiveValue) > epsilon)
162 this->m_Precision = this->m_TruePositiveValue / (this->m_TruePositiveValue + this->m_FalsePositiveValue);
164 if (std::abs(this->m_TruePositiveValue + this->m_FalseNegativeValue) > epsilon)
166 this->m_Recall = this->m_TruePositiveValue / (this->m_TruePositiveValue + this->m_FalseNegativeValue);
168 if (std::abs(this->m_Recall + this->m_Precision) > epsilon)
170 this->m_FScore = 2 * this->m_Recall * this->m_Precision / (this->m_Recall + this->m_Precision);