介绍
小编给大家分享一下实现python绘制混淆矩阵的方法,希望大家阅读完这篇文章后大所收获、下面让我们一起去探讨吧!
<强> 强> <强> 强>
/不正确标签的计数来表示模型在表格格式中的准确性。
<强>/绘制混淆矩阵:强> <强> 强>
<强> 强> <强> 强>
- <李>
李> <李>
李> <李>
李> <李>
李>
<强> 强>
- <李>
李> <李>
李>
<强> 强> <强> 强>
<强> 强> <强> 强>
<强> python绘制混淆矩阵:强> <强> 强>
import itertools , import numpy  as np , import matplotlib.pyplot  as plt , 得到sklearn import 支持向量机,数据集 , 得到sklearn.model_selection import  train_test_split , 得到sklearn.metrics import  confusion_matrix , #,import some  data 用play , 时间=iris datasets.load_iris () , X =iris.data , y =iris.target , class_names =iris.target_names , #,Split 从而,data into a training set 以及a test 集 , X_train, X_test, y_train,, y_test =, train_test_split (X, y,, random_state=0) , #,Run 分类器,using a model that is too regularized (C too 低),用看到的 , #,从而impact 提醒,结果 , 时间=classifier svm.SVC(内核=& # 39;线性# 39;,,C=0.01) , 时间=y_pred classifier.fit (X_train, y_train) .predict (X_test) , def plot_confusion_matrix(厘米,,类, ,=False正常化, , title=& # 39; Confusion 矩阵# 39; , 提出=plt.cm.Blues): , “““ , 却,能够function prints 以及plots 从而confusion 矩阵。 , Normalization 还要be applied  by setting “正常化=True”。 , “““ , if 规范化: , 时间=cm cm.astype(& # 39;浮动# 39;),/,cm.sum(轴=1)(:,,np.newaxis] , print (“Normalized  confusion matrix") , 其他: , 打印(& # 39;Confusion 矩阵,,without 规范化# 39;) , 打印(cm) , plt.imshow (cm,插值=& # 39;最近的# 39;,,提出=提出) , plt.title(标题) , plt.colorbar () , 时间=tick_marks np.arange (len(类)) , plt.xticks (tick_marks,类,旋转=45) , plt.yticks (tick_marks,类) , 时间=fmt & # 39; .2f& # 39;, if normalize else & # 39; d # 39; , 时间=thresh cm.max (),/2。 , for 我,j 拷贝itertools.product(范围(cm.shape[0]),,范围(cm.shape [1])): , plt.text (j,,我,,格式(cm (i, j), fmt), , horizontalalignment=癱enter", , 颜色=皐hite", if 厘米(i, j),祝辞,thresh else “black") , 颜色=皐hite", if 厘米(i, j),祝辞,thresh else “black") , plt.tight_layout () , plt.ylabel (& # 39; True 标签# 39;) , plt.xlabel (& # 39; Predicted 标签# 39;) , #,Compute confusion 矩阵 , 时间=cnf_matrix confusion_matrix (y_test, y_pred) , np.set_printoptions(精度=2) , #,Plot non-normalized  confusion 矩阵 , plt.图() ,=class_names plot_confusion_matrix (cnf_matrix,类, , title=& # 39; Confusion 矩阵,,without 规范化# 39; , #,Plot normalized  confusion 矩阵 , plt.图() ,=class_names plot_confusion_matrix (cnf_matrix,类,,=True正常化, , title=& # 39; Normalized  confusion 矩阵# 39; , plt.show ()
Confusion 矩阵,without 正常化 , [[13,0,0) , (10,0,6] , (0,0,9]] , Normalized confusion 矩阵 , [[0,1只,只0只) , (0.62,0.38,0只) , [,0只0只1只]]
看完了这篇文章,相信你对实现python绘制混淆矩阵的方法有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!