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

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


咨詢
建站咨詢
