国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > Python基础教程 > 如何实现python绘制混淆矩阵?

如何实现python绘制混淆矩阵?

头像

2020-11-04 11:10:389091浏览 · 0收藏 · 0评论

大家从python基础到如今的入门,想必都对python有一定基础,今天小编给大家带来一个关于python的高阶内容——绘制混淆矩阵,一起来看下吧~

介绍:

混淆矩阵通过表示正确/不正确标签的计数来表示模型在表格格式中的准确性。

计算/绘制混淆矩阵:

以下是计算混淆矩阵的过程。

您需要一个包含预期结果值的测试数据集或验证数据集。

  • 对测试数据集中的每一行进行预测。

  • 从预期的结果和预测计数:

  • 每个类的正确预测数量。

  • 每个类的错误预测数量,由预测的类组织。

然后将这些数字组织成表格或矩阵,如下所示:

  • Expected down the side:矩阵的每一行都对应一个预测的类。

  • Predicted across the top:矩阵的每一列对应于一个实际的类。

然后将正确和不正确分类的计数填入表格中。

Reading混淆矩阵:

一个类的正确预测的总数进入该类值的预期行,以及该类值的预测列。

以同样的方式,一个类别的不正确预测总数进入该类别值的预期行,以及该类别值的预测列。

对角元素表示预测标签等于真实标签的点的数量,而非对角线元素是分类器错误标记的元素?;煜卣蟮亩越窍咧翟礁咴胶?,表明许多正确的预测。

Python绘制混淆矩阵 :

import itertools
 
import numpy as np
 
import matplotlib.pyplot as plt
 
from sklearn import svm, datasets
 
from sklearn.model_selection import train_test_split
 
from sklearn.metrics import confusion_matrix
 
# import some data to play with
 
iris = datasets.load_iris()
 
X = iris.data
 
y = iris.target
 
class_names = iris.target_names
 
# Split the data into a training set and a test set
 
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
 
# Run classifier, using a model that is too regularized (C too low) to see
 
# the impact on the results
 
classifier = svm.SVC(kernel='linear', C=0.01)
 
y_pred = classifier.fit(X_train, y_train).predict(X_test)
 
def plot_confusion_matrix(cm, classes,
 
normalize=False,
 
title='Confusion matrix',
 
cmap=plt.cm.Blues):
 
"""
 
This function prints and plots the confusion matrix.
 
Normalization can be applied by setting `normalize=True`.
 
"""
 
if normalize:
 
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
 
print("Normalized confusion matrix")
 
else:
 
print('Confusion matrix, without normalization')
 
print(cm)
 
plt.imshow(cm, interpolation='nearest', cmap=cmap)
 
plt.title(title)
 
plt.colorbar()
 
tick_marks = np.arange(len(classes))
 
plt.xticks(tick_marks, classes, rotation=45)
 
plt.yticks(tick_marks, classes)
 
fmt = '.2f' if normalize else 'd'
 
thresh = cm.max() / 2.
 
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
 
plt.text(j, i, format(cm[i, j], fmt),
 
horizontalalignment="center",
 
color="white" if cm[i, j] > thresh else "black")
 
color="white" if cm[i, j] > thresh else "black")
 
plt.tight_layout()
 
plt.ylabel('True label')
 
plt.xlabel('Predicted label')
 
# Compute confusion matrix
 
cnf_matrix = confusion_matrix(y_test, y_pred)
 
np.set_printoptions(precision=2)
 
# Plot non-normalized confusion matrix
 
plt.figure()
 
plot_confusion_matrix(cnf_matrix, classes=class_names,
 
title='Confusion matrix, without normalization')
 
# Plot normalized confusion matrix
 
plt.figure()
 
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
 
title='Normalized confusion matrix')
 
plt.show()

Confusion matrix, without normalization
 
[[13 0 0]
 
[ 0 10 6]
 
[ 0 0 9]]
 
Normalized confusion matrix
 
[[ 1. 0. 0. ]
 
[ 0. 0.62 0.38]
 
[ 0. 0. 1. ]]

好了,大家可以消化学习下哦~如需了解更多python实用知识,点击进入PyThon学习网教学中心。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

男按摩师舌头伸进去了SPA漫画| 按摩师把我添高潮了6次| 男朋友摸完小花园小肚子有点痛 | 男人手伸进我内衣揉我胸到爽| 开会桌下把荫蒂添的好多水| 《睡眠指导1-6集》| 人野兽马狗猪大全| 教练车内含乳挺进她漫画| 26UUU.MOBI| 《日本公与媳5》公妇伦| 欧美大妈LOGO大全及价格图| 暴躁少女10-16岁| 臣卜扌桑扌圭鬲蘑菇| 绘色千佳| 引诱亲女乱第24部分阅读| 妈妈让我戴上避孕套的下一句是啥| 年轻的母亲4| 中文字幕精品无码亚洲字幕一区| 恋爱之瘾| 成品网站免费直播有哪些平台推荐| 暴躁少女免费看高清电视剧| 日韩毛片| 女人做爰高潮全过| 姐姐片多多免费高清电视剧叶玉卿 | 美女和帅哥一起努力生产豆浆| 免费观看B站刺激战场2023| 《漂亮的女邻居5》HD| 海棠书屋免费无弹窗全文阅读| 我的老师中韩双字| 我老公跟我一晚上用很多卫生纸| 天堂草原高清在线观看电视剧 | 女生频道| 动浸在线观看完整版影视| BL 电影| 肉多到炸的年下小狗(双南)| 成全二人世界免费观看电视剧| 人狗大战2免费观看正版| 欧美叔叔别撩我免费观看电视剧| 无人区6高清免费追剧软件介绍| 《暴雨回不了家去上司家》| 玉女心经2:阴阳合欢李丽珍