五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Matplotlib繪制混淆矩陣的實現(xiàn)

發(fā)布日期:2022-04-04 09:52 | 文章來源:源碼中國

對于機器學習多分類模型來說,其評價指標除了精度之外,常用的還有混淆矩陣和分類報告,下面來展示一下如何繪制混淆矩陣,這在論文中經(jīng)常會用到。

代碼如下:

import itertools
import matplotlib.pyplot as plt
import numpy as np
# 繪制混淆矩陣
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
 """
 - cm : 計算出的混淆矩陣的值
 - classes : 混淆矩陣中每一行每一列對應(yīng)的列
 - normalize : True:顯示百分比, False:顯示個數(shù)
 """
 if normalize:
  cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  print("顯示百分比:")
  np.set_printoptions(formatter={'float': '{: 0.2f}'.format})
  print(cm)
 else:
  print('顯示具體數(shù)字:')
  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)
 # matplotlib版本問題,如果不加下面這行代碼,則繪制的混淆矩陣上下只能顯示一半,有的版本的matplotlib不需要下面的代碼,分別試一下即可
 plt.ylim(len(classes) - 0.5, -0.5)
 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")
 plt.tight_layout()
 plt.ylabel('True label')
 plt.xlabel('Predicted label')
 plt.show()

測試數(shù)據(jù):

cnf_matrix = np.array([[8707, 64, 731, 164, 45],
 [1821, 5530, 79, 0, 28],
 [266, 167, 1982, 4, 2],
 [691, 0, 107, 1930, 26],
 [30, 0, 111, 17, 42]])
attack_types = ['Normal', 'DoS', 'Probe', 'R2L', 'U2R']

第一種情況:顯示百分比

plot_confusion_matrix(cnf_matrix, classes=attack_types, normalize=True, title='Normalized confusion matrix')

效果:


第二種情況:顯示數(shù)字

plot_confusion_matrix(cnf_matrix, classes=attack_types, normalize=False, title='Normalized confusion matrix')

效果:


到此這篇關(guān)于Matplotlib繪制混淆矩陣的實現(xiàn)的文章就介紹到這了,更多相關(guān)Matplotlib 混淆矩陣內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

香港服務(wù)器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部