在这个示例中,我们首先生成了示例数据,包括真实标签和预测标签。然后,我们使用sklearn.metrics模块中的confusion_matrix函数计算混淆矩阵。接下来,我们使用matplotlib.pyplot模块中的imshow函数绘制混淆矩阵,通过设置cmap参数为Blues来指定颜色映射。最后,我们添加标题、轴标签和颜色条,并显示图表。通过使用不同的颜色映射,我
import matplotlib.pyplot as plt import numpy as np def plot_confusion_matrix(cm, labels, title='Confusion Matrix'): plt.imshow(cm, interpolation='nearest', cmap='Blues') plt.title(title) plt.colorbar() xlocations = np.array(range(len(labels))) plt.xticks(xlocations, labels, rotation=90...
步骤1:导入必要的库 在整个过程中,我们需要使用matplotlib库来进行可视化操作,因此需要先导入该库。 importmatplotlib.pyplotasplt 1. 步骤2:创建混淆矩阵 在这一步,我们需要先定义混淆矩阵的数据,然后使用matplotlib中的imshow函数将其可视化出来。 confusion_matrix_data=[[10,2,0],[3,15,1],[0,2,8]]plt.im...
importmatplotlib.pyplotasplt# 统计TP、TN、FP和FNTP=cm[1,1]TN=cm[0,0]FP=cm[0,1]FN=cm[1,0]# 绘制饼状图labels=['True Positive','True Negative','False Positive','False Negative']sizes=[TP,TN,FP,FN]colors=['gold','lightcoral','lightskyblue','lightgreen']plt.figure(figsize=(8,6...
confusion matrix 二. 代码实现 安装需要的绘图包 #命令行运行以下命令安装 pip install sklearn matplotlib numpy 运行以下代码绘图,类别和路径,读取文件请根据情况自行改动 from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import numpy as np import json classes = ['01','02','...
importmatplotlib.pyplotasplt importnumpy fromsklearnimportmetrics actual = numpy.random.binomial(1,.9,size =1000) predicted =numpy.random.binomial(1,.9,size =1000) confusion_matrix =metrics.confusion_matrix(actual, predicted) cm_display =metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matr...
from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import seaborn as sns # 生成模拟数据集 X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X,...
使用python绘制混淆矩阵(confusion_matrix) Summary 涉及到分类问题,我们经常需要通过可视化混淆矩阵来分析实验结果进而得出调参思路,本文介绍如何利用python绘制混淆矩阵(confusion_matrix),本文只提供代码,给出必要注释。 Code # -*-coding:utf-8-*-fromsklearn.metricsimportconfusion_matriximportmatplotlib.pyplotas...
from sklearn.metrics import confusion_matrix import seaborn as sns import matplotlib.pyplot as plt # 假设我们有以下真实标签和预测标签 y_true = [0, 1, 0, 1, 1, 0, 0, 1] y_pred = [0, 1, 1, 1, 0, 0, 1, 1] # 创建混淆矩阵 cm = confusion_matrix(y_true, y_pred) # 可视化...
importseaborn as snsfromsklearn.metricsimportconfusion_matriximportmatplotlib.pyplot as plt 导入需要的包,如果有一些包没有,pip一下就可以了。 sns.set() f,ax=plt.subplots() y_true= [0,0,1,2,1,2,0,2,2,0,1,1] y_pred= [1,0,1,2,1,0,0,2,2,0,1,1] ...