【问题标题】:c++ confusion matrix errorc++混淆矩阵错误
【发布时间】:2015-09-18 10:04:47
【问题描述】:

我正在尝试从一组基于 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;
}

有人知道为什么吗?

【问题讨论】:

  • 还没有完全理解代码,但我会尝试的第一件事是将confMat[i][x] 标准化为exCount[i]
  • 这就是最终 cout 声明的内容。我想将预测矩阵作为一个单独的实体 confMat。 exCount 也是每种类型/标签的出现总数,因此直到循环外才完成,exCount[i] 可能不是标准化的全部总数。

标签: c++ opencv confusion-matrix dlib


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 因此,例如使用 confMat[1][2],我实际上是在访问第三个向量的第二个元素,而不是相反.. confMat[1] 不会第二个向量呢?
  • @rfho_bdss 好吧...我总是(真的总是)将行与列混淆,但由于只有两种可能性,因此很容易修复
  • 下次我会记住这一点,非常感谢。
【解决方案2】:

我不知道为什么,但是我使用的多个 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;
}

对问题进行了分类。知道为什么吗?

【讨论】:

  • 请删除此答案。我编辑了您的问题以包含此文本。
  • 刷新您的浏览器。请参阅您问题中的更新部分。
  • 啊,我明白了!我的解决方案在这里有什么问题?
  • 你解决了它,然后问了一个问题。您需要将其分开。
猜你喜欢
  • 2019-10-15
  • 2020-03-09
  • 2018-10-02
  • 2020-07-25
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 2015-12-17
相关资源
最近更新 更多