C ++混淆矩阵错误

c++ confusion matrix error

我正在尝试根据基于 OpenCV SVM 训练和预测函数的一组 SVM 预测构建一个混淆矩阵,如下所示:

void testClassifier(CvSVM & SVM, 
    std::vector<std::vector<float>> & test_samples,
    std::vector<float> & test_labels,
    const CvSVMParams & params,
    double & tempAcc)
{
    cv::Mat matSamp;
    int response;
    int cnt = 0;
    //std::vector<int> labels = { 0, 1, 2, 3, 4, 5, 6 };
    std::vector<int> exCount = { 0, 0, 0, 0, 0, 0, 0 };
    std::vector<std::vector<double>> confMat = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };


    //test classifier
    for (unsigned int t = 0; t < test_samples.size(); t++)
    {
        matSamp = cv::Mat(test_samples[t].size(), 1, CV_32FC1, test_samples[t].data());
        response = SVM.predict(matSamp);

        if (response == test_labels[t])
        {
            cnt++;
        }

        exCount[test_labels[t]] += 1;
        //cout << test_labels[t] << " " << exCount[test_labels[t]] << endl;
        confMat[test_labels[t]][response] += 1.0;
        //cout << confMat[test_labels[t]][response] << endl;
    }

    cout << "Parameters used: " << endl;
    tempAcc = (double)cnt * 100 / test_samples.size();
    cout << "  C:               " << params.C << endl;
    cout << "  Gamma:           " << params.gamma << endl;
    cout << "you hit " << tempAcc << "% accuracy" << endl << endl;
    cout << confMat[0][0] / exCount[0] * 100 << " " << confMat[0][1] / exCount[1] * 100 << " " << confMat[0][2] / exCount[2] * 100 << " " << confMat[0][3] / exCount[3] * 100 << " " << confMat[0][4] / exCount[4] * 100 << " " << confMat[0][5] / exCount[5] * 100 << " " << confMat[0][6] / exCount[6] * 100 << endl;
    cout << confMat[1][0] / exCount[0] * 100 << " " << confMat[1][1] / exCount[1] * 100 << " " << confMat[1][2] / exCount[2] * 100 << " " << confMat[1][3] / exCount[3] * 100 << " " << confMat[1][4] / exCount[4] * 100 << " " << confMat[1][5] / exCount[5] * 100 << " " << confMat[1][6] / exCount[6] * 100 << endl;
    cout << confMat[2][0] / exCount[0] * 100 << " " << confMat[2][1] / exCount[1] * 100 << " " << confMat[2][2] / exCount[2] * 100 << " " << confMat[2][3] / exCount[3] * 100 << " " << confMat[2][4] / exCount[4] * 100 << " " << confMat[2][5] / exCount[5] * 100 << " " << confMat[2][6] / exCount[6] * 100 << endl;
    cout << confMat[3][0] / exCount[0] * 100 << " " << confMat[3][1] / exCount[1] * 100 << " " << confMat[3][2] / exCount[2] * 100 << " " << confMat[3][3] / exCount[3] * 100 << " " << confMat[3][4] / exCount[4] * 100 << " " << confMat[3][5] / exCount[5] * 100 << " " << confMat[3][6] / exCount[6] * 100 << endl;
    cout << confMat[4][0] / exCount[0] * 100 << " " << confMat[4][1] / exCount[1] * 100 << " " << confMat[4][2] / exCount[2] * 100 << " " << confMat[4][3] / exCount[3] * 100 << " " << confMat[4][4] / exCount[4] * 100 << " " << confMat[4][5] / exCount[5] * 100 << " " << confMat[4][6] / exCount[6] * 100 << endl;
    cout << confMat[5][0] / exCount[0] * 100 << " " << confMat[5][1] / exCount[1] * 100 << " " << confMat[5][2] / exCount[2] * 100 << " " << confMat[5][3] / exCount[3] * 100 << " " << confMat[5][4] / exCount[4] * 100 << " " << confMat[5][5] / exCount[5] * 100 << " " << confMat[5][6] / exCount[6] * 100 << endl;
    cout << confMat[6][0] / exCount[0] * 100 << " " << confMat[6][1] / exCount[1] * 100 << " " << confMat[6][2] / exCount[2] * 100 << " " << confMat[6][3] / exCount[3] * 100 << " " << confMat[6][4] / exCount[4] * 100 << " " << confMat[6][5] / exCount[5] * 100 << " " << confMat[6][6] / exCount[6] * 100 << endl;
    cout << "enter for next matrix" << endl;/**/
    cin.get();
}

但是当输出矩阵条目时,有些行的总和大于 100%,有些小于。我敢肯定它很简单,但我已经盯着它看了很多年了,却一无所知。有什么想法吗?


更新

我不确定为什么,但我使用的多个 cout 影响了 return 值。

问题已通过将调试 cout 包含在 for 循环中解决,如下所示:

for (unsigned int m = 0; m < 7; m++)
{
    for (unsigned int n = 0; n < 7; n++)
    {
        confMat[m][n] = confMat[m][n] * 100 / exCount[m];
        cout << confMat[m][n] << " ";
    }
    cout << endl;
}

有人知道为什么吗?

我不确定为什么,但我使用的多个 cout 影响了 return 值。

通过在 for 循环中包含调试 cout,如下所示:

for (unsigned int m = 0; m < 7; m++)
{
    for (unsigned int n = 0; n < 7; n++)
    {
        confMat[m][n] = confMat[m][n] * 100 / exCount[m];
        cout << confMat[m][n] << " ";
    }
    cout << endl;
}

对问题进行了排序。知道为什么吗?

看来我的第一个猜测是对的。在原来你这样规范化:

confMat[i][m] / exCount[m] * 100

在正确的代码中,您可以像这样规范化:

confMat[m][i] / exCount[m] * 100

根据 exCount 计算的是每行总计还是每列总计,您只能通过上述其中一行得到正确答案。