Python 如何使用pandas_ml生成包含2个类的混淆矩阵?
我试图用plotting制作一个混淆矩阵,pandas_ml似乎有一个函数,但它不适用于两个类。有什么秘密的方法可以让它工作吗Python 如何使用pandas_ml生成包含2个类的混淆矩阵?,python,Python,我试图用plotting制作一个混淆矩阵,pandas_ml似乎有一个函数,但它不适用于两个类。有什么秘密的方法可以让它工作吗 from pandas_ml import ConfusionMatrix ytrue = ['ham', 'ham', 'spam'] ypred = ['ham', 'spam', 'spam'] cm = ConfusionMatrix(ytrue, ypred) cm 导致 Predicted False True __all__ Actual
from pandas_ml import ConfusionMatrix
ytrue = ['ham', 'ham', 'spam']
ypred = ['ham', 'spam', 'spam']
cm = ConfusionMatrix(ytrue, ypred)
cm
导致
Predicted False True __all__
Actual
False 0 0 0
True 0 0 0
__all__ 0 0 0
Predicted ham spam third __all__
Actual
ham 1 1 0 2
spam 0 1 0 1
third 0 0 1 1
__all__ 1 2 1 4
这:
导致
Predicted False True __all__
Actual
False 0 0 0
True 0 0 0
__all__ 0 0 0
Predicted ham spam third __all__
Actual
ham 1 1 0 2
spam 0 1 0 1
third 0 0 1 1
__all__ 1 2 1 4
不,当我在python3.6中通过spyder3运行它时,我得到了这个
from pandas_ml import ConfusionMatrix
ytrue = ['ham', 'ham', 'spam']
ypred = ['ham', 'spam', 'spam']
cm = ConfusionMatrix(ytrue, ypred)
cm
Out[1]:
Predicted ham spam __all__
Actual
ham 1 1 2
spam 0 1 1
__all__ 1 2 3
IN[2]: cm.print_stats()
OUT[2]:
population: 3
P: 1
N: 2
PositiveTest: 2
NegativeTest: 1
TP: 1
TN: 1
FP: 1
FN: 0
TPR: 1.0
TNR: 0.5
PPV: 0.5
NPV: 1.0
FPR: 0.5
FDR: 0.5
FNR: 0.0
ACC: 0.666666666667
F1_score: 0.666666666667
MCC: 0.5
informedness: 0.5
markedness: 0.5
prevalence: 0.333333333333
LRP: 2.0
LRN: 0.0
DOR: inf
FOR: 0.0
cm.TP
Out[3]: 1
cm.TN
Out[4]: 1
cm.FP
Out[5]: 1
cm.FN
Out[6]: 0
也许已经太晚了,但我也有同样的问题。当您有两个类时,pandas_ml中的ConfusionMatrix要求您的输入为布尔值。只需将“spam”/“ham”转换为True/False,就可以了
from pandas_ml import ConfusionMatrix
ytrue = np.array(['ham', 'ham', 'spam'])
ytrue = np.array(['ham', 'ham', 'spam'])
ypred = np.array(['ham', 'spam', 'spam'])
cm = ConfusionMatrix(np.where(ytrue == 'spam', True, False), np.where(ypred == 'spam', True, False))
cm
解决这个问题的方法是创建两个名为pandas的系列,并使用pandas.crosstab()。甚至不要使用熊猫肉:
import pandas as pd
ytrue = pd.Series(['ham', 'ham', 'spam'], name='actual')
ypred = pd.Series(['ham', 'spam', 'spam'], name='predictions')
pd.crosstab(ytrue, ypred, margins=True)
输出将是
predictions ham spam All
actual
ham 1 1 2
spam 0 1 1
All 1 2 3
我在Python3.5和3.6两个版本中都试过了,今天最新版本的pandas和pandas_-ml。熊猫和熊猫的版本是什么?也许旧版本能用。我会说“它在我的机器上工作”的答案不是很好,因此否决票。pandas是0.20.1,而as_ml是0.5.0,那么这个呢:
来自sklearn.metrics导入混淆矩阵
。它也应该产生同样的结果<代码>数组([[1,1],[0,1]])。谢谢。这似乎很老套,而且像是坏习惯。pandas_ml需要为此请求拉取。