新聞中心
大家從python基礎(chǔ)到如今的入門,想必都對(duì)python有一定基礎(chǔ),今天小編給大家?guī)硪粋€(gè)關(guān)于python的高階內(nèi)容——繪制混淆矩陣,一起來看下吧~

創(chuàng)新互聯(lián)主營(yíng)靜海網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),靜海h5小程序設(shè)計(jì)搭建,靜海網(wǎng)站營(yíng)銷推廣歡迎靜海等地區(qū)企業(yè)咨詢
介紹:
混淆矩陣通過表示正確/不正確標(biāo)簽的計(jì)數(shù)來表示模型在表格格式中的準(zhǔn)確性。
計(jì)算/繪制混淆矩陣:
以下是計(jì)算混淆矩陣的過程。
您需要一個(gè)包含預(yù)期結(jié)果值的測(cè)試數(shù)據(jù)集或驗(yàn)證數(shù)據(jù)集。
-
對(duì)測(cè)試數(shù)據(jù)集中的每一行進(jìn)行預(yù)測(cè)。
-
從預(yù)期的結(jié)果和預(yù)測(cè)計(jì)數(shù):
-
每個(gè)類的正確預(yù)測(cè)數(shù)量。
-
每個(gè)類的錯(cuò)誤預(yù)測(cè)數(shù)量,由預(yù)測(cè)的類組織。
然后將這些數(shù)字組織成表格或矩陣,如下所示:
-
Expected down the side:矩陣的每一行都對(duì)應(yīng)一個(gè)預(yù)測(cè)的類。
-
Predicted across the top:矩陣的每一列對(duì)應(yīng)于一個(gè)實(shí)際的類。
然后將正確和不正確分類的計(jì)數(shù)填入表格中。
Reading混淆矩陣:
一個(gè)類的正確預(yù)測(cè)的總數(shù)進(jìn)入該類值的預(yù)期行,以及該類值的預(yù)測(cè)列。
以同樣的方式,一個(gè)類別的不正確預(yù)測(cè)總數(shù)進(jìn)入該類別值的預(yù)期行,以及該類別值的預(yù)測(cè)列。
對(duì)角元素表示預(yù)測(cè)標(biāo)簽等于真實(shí)標(biāo)簽的點(diǎn)的數(shù)量,而非對(duì)角線元素是分類器錯(cuò)誤標(biāo)記的元素。混淆矩陣的對(duì)角線值越高越好,表明許多正確的預(yù)測(cè)。
用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. ]]
好了,大家可以消化學(xué)習(xí)下哦~如需了解更多python實(shí)用知識(shí),點(diǎn)擊進(jìn)入PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:如何實(shí)現(xiàn)python繪制混淆矩陣?
網(wǎng)頁地址:http://m.fisionsoft.com.cn/article/cosgpcd.html


咨詢
建站咨詢
