Pandas矩阵混淆,具有绘图功能(matplotlib、seaborn...)
项目描述
pydata_confusion
工作正在进行中 - 使用风险自担
用法
混淆矩阵
导入ConfusionMatrix
from pandas_confusion import ConfusionMatrix
定义实际值(y_actu)和预测值(y_pred)
y_actu = ['rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit', 'rabbit', 'cat', 'dog', 'rabbit'] y_pred = ['cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'cat', 'rabbit', 'rabbit']
让我们定义一个(非二进制)混淆矩阵
confusion_matrix = ConfusionMatrix(y_actu, y_pred) print("Confusion matrix:\n%s" % confusion_matrix)
您可以看到它
Predicted cat dog rabbit __all__ Actual cat 3 0 0 3 dog 0 1 2 3 rabbit 2 1 3 6 __all__ 5 2 5 12
混淆矩阵的matplotlib绘图
在IPython笔记本中,将此行作为第一个单元添加
%matplotlib inline
您可以使用以下方式绘制混淆矩阵
import matplotlib.pyplot as plt confusion_matrix.plot()
如果您不是使用内联模式,则需要使用来显示混淆矩阵图。
plt.show()
confusion_matrix
规范化混淆矩阵的matplotlib绘图
confusion_matrix.plot(normalized=True) plt.show()
confusion_matrix_norm
二进制混淆矩阵
导入BinaryConfusionMatrix和Backend
from pandas_confusion import BinaryConfusionMatrix, Backend
定义实际值(y_actu)和预测值(y_pred)
y_actu = [ True, True, False, False, False, True, False, True, True, False, True, False, False, False, False, False, True, False, True, True, True, True, False, False, False, True, False, True, False, False, False, False, True, True, False, False, False, True, True, True, True, False, False, False, False, True, False, False, False, False, False, False, False, False, False, True, True, False, True, False, True, True, True, False, False, True, False, True, False, False, True, False, False, False, False, False, False, False, False, True, False, True, True, True, True, False, False, True, False, True, True, False, True, False, True, False, False, True, True, False, False, True, True, False, False, False, False, False, False, True, True, False] y_pred = [False, False, False, False, False, True, False, False, True, False, True, False, False, False, False, False, False, False, True, True, True, True, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, True, False, False, False, False, True, False, False, False, False, False, False, False, False, True, False, False, True, False, False, False, False, True, False, True, True, False, False, False, True, False, False, True, True, False, False, True, True, False, False, False, False, False, False, True, False, False]
让我们定义一个二进制混淆矩阵
binary_confusion_matrix = BinaryConfusionMatrix(y_actu, y_pred) print("Binary confusion matrix:\n%s" % binary_confusion_matrix)
它以漂亮的标签显示为Pandas DataFrame
Binary confusion matrix: Predicted False True __all__ Actual False 67 0 67 True 21 24 45 __all__ 88 24 112
您可以得到有用的属性,如真阳性(TP)、真阴性(TN)…
print binary_confusion_matrix.TP
二进制混淆矩阵的matplotlib绘图
binary_confusion_matrix.plot() plt.show()
binary_confusion_matrix
Matplotlib 绘制的归一化二元混淆矩阵
binary_confusion_matrix.plot(normalized=True) plt.show()
binary_confusion_matrix_norm
Seaborn 绘制的二元混淆矩阵(待办)
from pandas_confusion import Backend binary_confusion_matrix.plot(backend=Backend.Seaborn)
混淆矩阵和类别统计
混淆矩阵的整体统计和类别统计可以轻松显示。
y_true = [600, 200, 200, 200, 200, 200, 200, 200, 500, 500, 500, 200, 200, 200, 200, 200, 200, 200, 200, 200] y_pred = [100, 200, 200, 100, 100, 200, 200, 200, 100, 200, 500, 100, 100, 100, 100, 100, 100, 100, 500, 200] cm = ConfusionMatrix(y_true, y_pred) cm.print_stats()
您应该得到
Confusion Matrix: Classes 100 200 500 600 __all__ Actual 100 0 0 0 0 0 200 9 6 1 0 16 500 1 1 1 0 3 600 1 0 0 0 1 __all__ 11 7 2 0 20 Overall Statistics: Accuracy: 0.35 95% CI: (0.1539092047845412, 0.59218853453282805) No Information Rate: ToDo P-Value [Acc > NIR]: 0.978585644357 Kappa: 0.0780141843972 Mcnemar's Test P-Value: ToDo Class Statistics: Classes 100 200 500 600 Population 20 20 20 20 Condition positive 0 16 3 1 Condition negative 20 4 17 19 Test outcome positive 11 7 2 0 Test outcome negative 9 13 18 20 TP: True Positive 0 6 1 0 TN: True Negative 9 3 16 19 FP: False Positive 11 1 1 0 FN: False Negative 0 10 2 1 TPR: Sensivity NaN 0.375 0.3333333 0 TNR=SPC: Specificity 0.45 0.75 0.9411765 1 PPV: Pos Pred Value = Precision 0 0.8571429 0.5 NaN NPV: Neg Pred Value 1 0.2307692 0.8888889 0.95 FPR: False-out 0.55 0.25 0.05882353 0 FDR: False Discovery Rate 1 0.1428571 0.5 NaN FNR: Miss Rate NaN 0.625 0.6666667 1 ACC: Accuracy 0.45 0.45 0.85 0.95 F1 score 0 0.5217391 0.4 0 MCC: Matthews correlation coefficient NaN 0.1048285 0.326732 NaN Informedness NaN 0.125 0.2745098 0 Markedness 0 0.08791209 0.3888889 NaN Prevalence 0 0.8 0.15 0.05 LR+: Positive likelihood ratio NaN 1.5 5.666667 NaN LR-: Negative likelihood ratio NaN 0.8333333 0.7083333 1 DOR: Diagnostic odds ratio NaN 1.8 8 NaN FOR: False omission rate 0 0.7692308 0.1111111 0.05
统计信息也可以通过以下方式作为 OrderedDict 提供
cm.stats()
安装
$ conda install pandas scikit-learn scipy $ pip install pandas_confusion
开发
您可以帮助开发这个库。
问题
您可以使用以下链接提交问题:https://github.com/scls19fr/pandas_confusion/issues
克隆
您可以使用以下方式克隆存储库以尝试自己修复问题:
$ git clone https://github.com/scls19fr/pandas_confusion.git
运行单元测试
运行所有单元测试
$ nosetests -s -v
运行特定的测试
$ nosetests -s -v tests/test_pandas_confusion.py:test_pandas_confusion_normalized
安装开发版本
$ python setup.py install
或
$ sudo pip install git+git://github.com/scls19fr/pandas_confusion.git
协作
复制存储库
创建一个分支以修复特定问题
提交拉取请求
完成
持续集成(Travis)
将混淆矩阵转换为二元混淆矩阵
Python 包
单元测试(nose)
修复缺失列和缺失行
整体统计:准确率,95% CI,P-值 [Acc > NIR],Kappa
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定要选择哪一个,请了解更多关于 安装包 的信息。
源代码分发
pandas_confusion-0.0.6.tar.gz (13.9 kB 查看哈希值)
构建分发
关闭
哈希值 for pandas_confusion-0.0.6-py2.py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5b5e03191fc7207a74175436f85b8402480be6b7b627429a034767e5ba9f5206 |
|
MD5 | be7ab7c1b7e2e0aa0a9b81edeb5e3239 |
|
BLAKE2b-256 | 008283c2d82e76280d4f27e2bc56cd27e2fce759f77fb505ecf681a580965ba4 |