c ++混淆矩阵错误

时间:2015-07-01 09:53:41

标签: c++ opencv confusion-matrix dlib

我正在尝试从一组基于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会影响返回值。

通过在这样的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;
}

任何人都知道为什么?

2 个答案:

答案 0 :(得分:3)

我的第一次猜测似乎是对的。在原文中,您可以像这样标准化:

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

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

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

根据exCount是计算每行还是每列的总数,您只能使用上述其中一行获得正确答案。

答案 1 :(得分:1)

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

将调试couts包含在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;
}

排序问题。谁知道为什么?