from sklearn import metrics
1.accuracy_score(y_true, y_pred, normalize=True, sample_weight=None)
參數(shù)分別為y實(shí)際類別、預(yù)測(cè)類別、返回值要求(True返回正確的樣本占比,false返回的是正確分類的樣本數(shù)量)
eg:
>>> import numpy as np
>>> from sklearn.metrics import accuracy_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> accuracy_score(y_true, y_pred)
0.5
>>> accuracy_score(y_true, y_pred, normalize=False)
2.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2)
參數(shù):真是類別,預(yù)測(cè)類別,目標(biāo)類別名稱
eg:

3.confusion_matrix(y_true, y_pred, labels=None, sample_weight=None)
輸出為混淆矩陣
eg:

太多了,寫(xiě)3個(gè)常用的吧,具體參考help(metrics)
文末驚喜在此:
純手工Python混淆矩陣作圖代碼案例
defcm_plot(y,yp):#參數(shù)為實(shí)際分類和預(yù)測(cè)分類
fromsklearn.metricsimportconfusion_matrix
#導(dǎo)入混淆矩陣函數(shù)
cm = confusion_matrix(y,yp)
#輸出為混淆矩陣
importmatplotlib.pyplotasplt
#導(dǎo)入作圖函數(shù)
plt.matshow(cm,cmap=plt.cm.Greens)
# 畫(huà)混淆矩陣圖,配色風(fēng)格使用cm.Greens
plt.colorbar()
# 顏色標(biāo)簽
forxinrange(len(cm)):
foryinrange(len(cm)):
plt.annotate(cm[x,y],xy=(x,y),horizontalalignment='center',verticalalignment='center')
#annotate主要在圖形中添加注釋
# 第一個(gè)參數(shù)添加注釋
# 第一個(gè)參數(shù)是注釋的內(nèi)容
# xy設(shè)置箭頭尖的坐標(biāo)
#horizontalalignment水平對(duì)齊
#verticalalignment垂直對(duì)齊
#其余常用參數(shù)如下:
# xytext設(shè)置注釋內(nèi)容顯示的起始位置
# arrowprops 用來(lái)設(shè)置箭頭
# facecolor 設(shè)置箭頭的顏色
# headlength 箭頭的頭的長(zhǎng)度
# headwidth 箭頭的寬度
# width 箭身的寬度
plt.ylabel('True label')# 坐標(biāo)軸標(biāo)簽
plt.xlabel('Predicted label')# 坐標(biāo)軸標(biāo)簽
returnplt
#函數(shù)調(diào)用
cm_plot(train[:,3],tree.predict(train[:,:3])).show()
輸出結(jié)果圖:
