混淆矩陣 confusion matrix

2021-10-05 11:12:39 字數 3195 閱讀 4592

我們以乙個二分類問題舉例說明:

x:x1,x2…x100

y_真實:1,0,0,0,1,1,1…(假設60個1,40個0)

y^_**:0,1,0,1…(**70個1,30個0)

假設我們**中的70個正例中只有50個是真正例(即**的真結果和實際情況一樣),

假正例=70-50=20(故名思意,即**的正例和實際情況不一樣)

查準率:即我們想要知道**結果中**中真正例占**結果正例的比例

p=a/a+c

查全率:即我們想知道**結果中真正例占真實情況正例的比例

r=a/a+b

需要注意的是,查準率和查全率是一對矛盾的度量!

即p變大,r就變小,反之p變小,r變大,即pr成反比。

證明:因為a+b=60是乙個固定的值,不會改變

此時:p1=5/7=0.714, r1=5/6=0.833

第二次**情況:

可見,p1>p2,r1**實現:

import pandas as pd

from sklearn.preprocessing import labelencoder

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import standardscaler

from sklearn.pipeline import pipeline

from sklearn.svm import svc

import numpy as np

from sklearn.metrics import confusion_matrix

import matplotlib.pyplot as plt

from sklearn.metrics import precision_score, recall_score, f1_score

# 匯入資料

file = pd.read_csv('',

header=none)

df = file

x = df.loc[:, 2:].values #讀取檔案中所有行,從第二列開始的資料

# print(x.shape)

y = df.loc[:, 1].values #讀取檔案中所有行,從第一列的資料,即讀取真實值

# print(y)

le = labelencoder()

y = le.fit_transform(y) #類標整數化,輸出0/1

# print(y)

# 劃分訓練集合測試集

#x_train,x_test, y_train, y_test =sklearn.model_selection.train_test_split(train_data,train_target,test_size=0.4, random_state=0,stratify=y_train)

# train_data:所要劃分的樣本特徵集

# train_target:所要劃分的樣本結果

# test_size:測試集樣本佔比,如果是整數的話就是樣本的數量

# random_state:是隨機數的種子。

# 隨機數種子:其實就是該組隨機數的編號,在需要重複試驗的時候,保證得到一組一樣的隨機數。

# 比如你每次都填1,其他引數一樣的情況下你得到的隨機數組是一樣的。但填0或不填,每次都會不一樣。

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=1)

# 建立pipeline

# pipelines類只用來變換(transform)觀測資料(x)

# pipeline使用一系列 (key, value) 鍵值對來構建,其中 key 是你給這個步驟起的名字, value 是乙個評估器物件

# pipeline可以把多個評估器鏈結成乙個。這個是很有用的,因為處理資料的步驟一般都是固定的。例如特徵選擇、標準化和分類

#管道中的所有評估器,除了最後乙個評估器,管道的所有評估器必須是轉換器。(例如,必須有 transform 方法). 最後乙個評估器的型別不限**換器、分類器等等)

pipe_svc = pipeline([('scl', standardscaler()), ('clf', svc(random_state=1))])

pipe_svc.fit(x_train, y_train)

y_pred = pipe_svc.predict(x_test)

# 混淆矩陣並視覺化

confmat = confusion_matrix(y_true=y_test, y_pred=y_pred) # 輸出混淆矩陣

print(confmat)

fig, ax = plt.subplots(figsize=(2.5, 2.5))

ax.matshow(confmat, cmap=plt.cm.blues, alpha=0.3)

for i in range(confmat.shape[0]):

for j in range(confmat.shape[1]):

ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center')

plt.xlabel('predicted label')

plt.ylabel('true label')

plt.show()

# 召回率、準確率、f1

print('precision:%.3f' % precision_score(y_true=y_test, y_pred=y_pred))

print('recall:%.3f' % recall_score(y_true=y_test, y_pred=y_pred))

print('f1:%.3f' % f1_score(y_true=y_test, y_pred=y_pred))

混淆矩陣(confusion matrix)

乙個完美的分類模型是,將實際上是good的例項 成good,將bad的例項 稱bad。對於實際應用中的分類模型,可能 錯誤例項型別,因此我們需要知道到底 對了多少例項,錯了多少例項。混淆矩陣就是將這些資訊放在乙個表中,便於直觀的觀測和分析。在分類問題中,的情形存在如下四種 1.good good t...

混淆矩陣 Confusion Matrix

混淆矩陣是除了roc曲線和auc之外的另乙個判斷分類好壞程度的方法。以下有幾個概念需要先說明 tp true positive 真實為0,也為0 fn false negative 真實為0,為1 fp false positive 真實為1,為0 tn true negative 真實為1,也為1...

初學混淆矩陣

在中,混淆矩陣 confusion matrix 是視覺化工具,一般也叫做匹配矩陣。混淆矩陣的每一列代表了 類別,每一行代表了資料的真實歸屬類別,每一列的總數表示 為該類別的資料的數目 即通過演算法被分為該類的數目 每一行的資料總數表示該類別的資料例項的數目 如下表,第一行第一列中的43表示有43個...