简体   繁体   English

为什么 sklearn.metrics.confusion_matrix 和 sklearn.metrics.plot_confusion_matrix 的 function 定义不一致?

[英]Why do sklearn.metrics.confusion_matrix and sklearn.metrics.plot_confusion_matrix have inconsistent function defintions?

I am using sklearn and I noticed that the arguments of sklearn.metrics.plot_confusion_matrix and sklearn.metrics.confusion_matrix are inconsistent.我正在使用 sklearn,我注意到sklearn.metrics.plot_confusion_matrixsklearn.metrics.confusion_matrix的 arguments 不一致。 plot_confusion_matrix uses estimator and X to construct y_pred , while confusion_matrix has y_pred as argument directly. plot_confusion_matrix使用estimatorX来构造y_pred ,而confusion_matrix直接将y_pred作为参数。

What may be the reason for this inconsistency?这种不一致的原因可能是什么?

Partial function definitions:部分 function 定义:

  • sklearn.metrics.plot_confusion_matrix(estimator, X, y_true, ...) [where X should be X_test] sklearn.metrics.plot_confusion_matrix(estimator, X, y_true, ...) [其中 X 应该是 X_test]
  • sklearn.metrics.confusion_matrix(y_true, y_pred, ...)

Sources:资料来源:

Yes, you are right that there isn't a consistent API design for this but there is an on going discussion for this issue here .是的,你是对的,没有一致的 API 设计,但这里有一个关于这个问题的持续讨论。

One quick work around is ConfusionMatrixDisplay .一种快速的解决方法是ConfusionMatrixDisplay

example:例子:

from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

X, y = make_classification(random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y)

clf = make_pipeline(StandardScaler(), LogisticRegression(random_state=0))
clf.fit(X_train, y_train)

from sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay

y_pred = clf.predict(X_test)
cm = confusion_matrix(y_test, y_pred)

cm_display = ConfusionMatrixDisplay(cm, [0,1]).plot()

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

相关问题 在 sklearn.metrics.plot_confusion_matrix 中抑制科学记数法 - Suppress scientific notation in sklearn.metrics.plot_confusion_matrix sklearn.metrics.confusion_matrix - TypeError:'numpy.ndarray'对象不可调用 - sklearn.metrics.confusion_matrix - TypeError: 'numpy.ndarray' object is not callable 如何让sklearn.metrics.confusion_matrix()始终返回TP,TN,FP,FN? - How to make sklearn.metrics.confusion_matrix() to always return TP, TN, FP, FN? Keras model.fit日志和Sklearn.metrics.confusion_matrix报告的验证准确性指标彼此不匹配 - Validation accuracy metrics reported by Keras model.fit log and Sklearn.metrics.confusion_matrix don't match each other Sklearn.metrics.classification_report 混淆矩阵问题? - Sklearn.metrics.classification_report Confusion Matrix Problem? sklearn plot 带标签的混淆矩阵 - sklearn plot confusion matrix with labels 在sklearn指标confusion_matrix中包括零命中的行和列 - Include rows and columns with zero hits in sklearn metrics confusion_matrix 导入错误:无法从“sklearn.metrics”导入名称“plot_confusion_matrix” - ImportError: cannot import name 'plot_confusion_matrix' from 'sklearn.metrics' 为什么我的 sklearn.metrics chaos_matrix output 看起来是转置的? - Why does my sklearn.metrics confusion_matrix output look transposed? 混淆矩阵sklearn错误? - Confusion matrix sklearn bug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM