plot_confusion_matrix 没有估算器
plot_confusion_matrix without estimator
我正在尝试使用 plot_confusion_matrix,
from sklearn.metrics import confusion_matrix
y_true = [1, 1, 0, 1]
y_pred = [1, 1, 0, 0]
confusion_matrix(y_true, y_pred)
输出:
array([[1, 0],
[1, 2]])
现在,同时使用以下内容;使用 'classes' 或不使用 'classes'
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(y_true, y_pred, classes=[0,1], title='Confusion matrix, without normalization')
或
plot_confusion_matrix(y_true, y_pred, title='Confusion matrix, without normalization')
除了里面的数字,我希望得到类似这样的输出,
绘制简单的图表,应该不需要估算器。
使用 mlxtend.plotting、
from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
binary1 = np.array([[4, 1],
[1, 2]])
fig, ax = plot_confusion_matrix(conf_mat=binary1)
plt.show()
它提供相同的输出。
基于this
它需要一个分类器,
disp = plot_confusion_matrix(classifier, X_test, y_test,
display_labels=class_names,
cmap=plt.cm.Blues,
normalize=normalize)
我可以不用分类器绘制它吗?
plot_confusion_matrix
需要经过训练的分类器。如果您查看 source code,它所做的是执行预测,为您生成 y_pred
:
y_pred = estimator.predict(X)
cm = confusion_matrix(y_true, y_pred, sample_weight=sample_weight,
labels=labels, normalize=normalize)
因此,为了在不指定 分类器 的情况下绘制混淆矩阵,您必须使用其他工具,或者自己动手。
一个简单的选择是使用 seaborn:
import seaborn as sns
cm = confusion_matrix(y_true, y_pred)
f = sns.heatmap(cm, annot=True)
因为 plot_confusion_matrix 要求参数 'estimator' 不是 None,所以答案是:不,你不能。但是你可以用其他方式绘制你的混淆矩阵,例如看到这个答案:How can I plot a confusion matrix?
我来晚了一点,但我认为其他人可能会从我的回答中受益。
正如其他人提到的那样,使用 plot_confusion_matrix
不是没有分类器的选项,但仍然可以使用 sklearn 在没有分类器的情况下获得类似的混淆矩阵。下面的函数正是这样做的。
def confusion_ma(y_true, y_pred, class_names):
cm = confusion_matrix(y_true, y_pred, normalize='true')
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)
disp.plot(cmap=plt.cm.Blues)
return plt.show()
confusion_matrix
函数returns一个简单的ndarry矩阵。通过将其与预测标签一起传递给 ConfusionMatrixDisplay
函数,可以获得一个外观相似的矩阵。在定义中,我添加了要显示的 class_names
而不是 0 和 1,选择规范化输出并指定颜色图 - 根据您的需要进行相应更改。
我在 Jupyter notebook 运行 Amazon SageMaker 中的 conda_python3 内核中测试了以下“身份分类器”。原因是 SageMaker 的转换作业是异步的,因此不允许在 plot_confusion_matrix
的参数中使用分类器,y_pred
必须在 之前计算 调用函数。
IC = type('IdentityClassifier', (), {"predict": lambda i : i, "_estimator_type": "classifier"})
plot_confusion_matrix(IC, y_pred, y_test, normalize='true', values_format='.2%');
因此,虽然 plot_confusion_matrix
确实需要一个估算器,但如果此解决方案适合您的用例,您不一定必须使用 IMO 的其他工具。
simplified POC from the notebook
我解决了使用自定义 classifier 的问题;您可以构建任何自定义 classifier 并将其作为 class:
传递给 plot_confusion 矩阵
class MyModelPredict(object):
def __init__(self, model):
self._estimator_type = 'classifier'
def predict(self, X):
return your_custom_prediction
model = MyModelPredict()
plot_confusion_matrix(model, X, y_true)
我正在尝试使用 plot_confusion_matrix,
from sklearn.metrics import confusion_matrix
y_true = [1, 1, 0, 1]
y_pred = [1, 1, 0, 0]
confusion_matrix(y_true, y_pred)
输出:
array([[1, 0],
[1, 2]])
现在,同时使用以下内容;使用 'classes' 或不使用 'classes'
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(y_true, y_pred, classes=[0,1], title='Confusion matrix, without normalization')
或
plot_confusion_matrix(y_true, y_pred, title='Confusion matrix, without normalization')
除了里面的数字,我希望得到类似这样的输出,
绘制简单的图表,应该不需要估算器。
使用 mlxtend.plotting、
from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
binary1 = np.array([[4, 1],
[1, 2]])
fig, ax = plot_confusion_matrix(conf_mat=binary1)
plt.show()
它提供相同的输出。
基于this
它需要一个分类器,
disp = plot_confusion_matrix(classifier, X_test, y_test,
display_labels=class_names,
cmap=plt.cm.Blues,
normalize=normalize)
我可以不用分类器绘制它吗?
plot_confusion_matrix
需要经过训练的分类器。如果您查看 source code,它所做的是执行预测,为您生成 y_pred
:
y_pred = estimator.predict(X)
cm = confusion_matrix(y_true, y_pred, sample_weight=sample_weight,
labels=labels, normalize=normalize)
因此,为了在不指定 分类器 的情况下绘制混淆矩阵,您必须使用其他工具,或者自己动手。 一个简单的选择是使用 seaborn:
import seaborn as sns
cm = confusion_matrix(y_true, y_pred)
f = sns.heatmap(cm, annot=True)
因为 plot_confusion_matrix 要求参数 'estimator' 不是 None,所以答案是:不,你不能。但是你可以用其他方式绘制你的混淆矩阵,例如看到这个答案:How can I plot a confusion matrix?
我来晚了一点,但我认为其他人可能会从我的回答中受益。
正如其他人提到的那样,使用 plot_confusion_matrix
不是没有分类器的选项,但仍然可以使用 sklearn 在没有分类器的情况下获得类似的混淆矩阵。下面的函数正是这样做的。
def confusion_ma(y_true, y_pred, class_names):
cm = confusion_matrix(y_true, y_pred, normalize='true')
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)
disp.plot(cmap=plt.cm.Blues)
return plt.show()
confusion_matrix
函数returns一个简单的ndarry矩阵。通过将其与预测标签一起传递给 ConfusionMatrixDisplay
函数,可以获得一个外观相似的矩阵。在定义中,我添加了要显示的 class_names
而不是 0 和 1,选择规范化输出并指定颜色图 - 根据您的需要进行相应更改。
我在 Jupyter notebook 运行 Amazon SageMaker 中的 conda_python3 内核中测试了以下“身份分类器”。原因是 SageMaker 的转换作业是异步的,因此不允许在 plot_confusion_matrix
的参数中使用分类器,y_pred
必须在 之前计算 调用函数。
IC = type('IdentityClassifier', (), {"predict": lambda i : i, "_estimator_type": "classifier"})
plot_confusion_matrix(IC, y_pred, y_test, normalize='true', values_format='.2%');
因此,虽然 plot_confusion_matrix
确实需要一个估算器,但如果此解决方案适合您的用例,您不一定必须使用 IMO 的其他工具。
simplified POC from the notebook
我解决了使用自定义 classifier 的问题;您可以构建任何自定义 classifier 并将其作为 class:
传递给 plot_confusion 矩阵class MyModelPredict(object):
def __init__(self, model):
self._estimator_type = 'classifier'
def predict(self, X):
return your_custom_prediction
model = MyModelPredict()
plot_confusion_matrix(model, X, y_true)