简体   繁体   English

从 pd.crosstab 中的混淆矩阵中找到准确度

[英]Finding the accuracy from the confusion matrix in pd.crosstab

Using pd.crosstab , I can produce a confusion matrix from my predicted data.使用pd.crosstab ,我可以从我的预测数据中生成一个混淆矩阵。 I used the following line to generate the confusion matrix:我使用以下行来生成混淆矩阵:

pd.crosstab(test_data['class'], test_data['predicted'], margins = True)

Similarly in R, I can generate a confusion matrix using the line below同样在 R 中,我可以使用下面的行生成混淆矩阵

confusion_matrix <- table(truth = data.test$class, prediction = predict(model, data.test[,-46], type = 'class'))

And in R I can find the accuracy of my model using this line在 R 我可以使用这条线找到我的 model 的精度

sum(diag(confusion_matrix)) / sum(confusion_matrix)

In Python, is there an equivalent of sum(diag(confusion_matrix)) / sum(confusion_matrix) to calculate the accuracy from my confusion matrix?在 Python 中,是否有sum(diag(confusion_matrix)) / sum(confusion_matrix)的等价物来计算我的混淆矩阵的准确性?

I will prefer not to use any libraries except pandas (eg Scikit learn).我宁愿不使用除 pandas 以外的任何库(例如 Scikit learn)。

You need to use numpy , first use np.diag on the crosstab product to get sum of the diagonal, and then converting the crosstab product to a numpy array before summing:您需要使用numpy ,首先在交叉表产品上使用np.diag以获得对角线的总和,然后在求和之前将交叉表产品转换为 numpy 数组:

import numpy as np
np.random.seed(123)
test_data = pd.DataFrame({'class':np.random.randint(0,2,10),
                        'predicted':np.random.randint(0,2,10)})

tab = pd.crosstab(test_data['class'], test_data['predicted'])

predicted   0   1
class       
0   4   3
1   0   3

tab = pd.crosstab(test_data['class'], test_data['predicted'])
np.diag(tab).sum() / tab.to_numpy().sum()
0.7

Or hardcode it?还是硬编码? not sure why you want to do this:不知道你为什么要这样做:

(tab.iloc[0,0] + tab.iloc[1,1]) / tab.to_numpy().sum()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM