2

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

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;
}

有人知道为什么吗?

4

2 回答 2

3

似乎我的第一个猜测是正确的。在原始文件中,您可以这样标准化:

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

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

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

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

于 2015-07-01T13:04:03.167 回答
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;
}

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

于 2015-07-01T12:14:19.260 回答