分類模型的可信度評估


1.分類模型中的預測准確率

############################# 分類模型中的預測准確率 #######################################
#導入數據集生成工具
from sklearn.datasets import make_blobs
#導入numpy
import numpy as np
#導入畫圖工具
import matplotlib.pyplot as plt
#生成樣本數為200,分類為2,標准差為5的數據集
X,y = make_blobs(n_samples=200,random_state=1,centers=2,cluster_std=5)
#繪制散點圖
plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.cool,edgecolor='k')
#顯示圖像
plt.show()

#導入數據集拆分工具
from sklearn.model_selection import train_test_split
#導入貝葉斯模型
from sklearn.naive_bayes import GaussianNB
#將數據集拆分為訓練集與測試集
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=68)
#訓練高斯貝葉斯模型
gnb = GaussianNB()
gnb.fit(X_train,y_train)
#獲得高斯貝葉斯的分類准確概率
predict_proba = gnb.predict_proba(X_test)
#打印結果
print('預測准確率形態:{}'.format(predict_proba.shape))
預測准確率形態:(50, 2)
#打印准確率的前5個
print(predict_proba[:5])
[[0.98849996 0.01150004]
 [0.0495985  0.9504015 ]
 [0.01648034 0.98351966]
 [0.8168274  0.1831726 ]
 [0.00282471 0.99717529]]
#設定橫縱軸的范圍
x_min,x_max = X[:, 0].min() - .5,X[:, 0].max() + .5
y_min,y_max = X[:, 1].min() - .5,X[:, 1].max() + .5

xx,yy = np.meshgrid(np.arange(x_min,x_max, 0.2),np.arange(y_min,y_max, 0.2))
Z = gnb.predict_proba(np.c_[xx.ravel(),yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
#繪制等高線
plt.contourf(xx,yy,Z,cmap=plt.cm.summer,alpha=.8)
#繪制散點圖
plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.cool,edgecolor='k')
plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.cool,edgecolor='k',alpha=0.6)
#設置橫縱軸范圍
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
#設置橫縱軸單位
plt.xticks(())
plt.yticks(())
#顯示圖像
plt.show()

  • 圖中的圓點代表的是測試集中的樣本數據,青色區域代表第一個分類,紅色區域代表第二個分類,在兩個區域的中間,有一部分漸變色的區域,處於這個區域中的數據點便是模型覺得"模棱兩可"的點

2.分類模型中的決定系數

#導入SVC模型
from sklearn.svm import SVC
#使用訓練集訓練模型
svc = SVC(gamma='auto').fit(X_train,y_train)
#獲得SVC的決定系數
dec_func = svc.decision_function(X_test)
#打印決定系數中的前5個
print(dec_func[:5])
[ 0.02082432  0.87852242  1.01696254 -0.30356558  0.95924836]
Z = svc.decision_function(np.c_[xx.ravel(),yy.ravel()])
Z = Z.reshape(xx.shape)
#繪制等高線
plt.contourf(xx,yy,Z,cmap=plt.cm.summer,alpha=.8)
#繪制散點圖
plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.cool,edgecolor='k')
plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.cool,edgecolor='k',alpha=0.6)
#設置橫縱軸范圍
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
#設置圖題
plt.title('SVC decision_function')
#設置橫縱軸單位
plt.xticks(())
plt.yticks(())
#顯示圖像
plt.show()

  • 分類同樣是用青色和紅色的區域來表示,如每個數據點所處的區域青色越明顯,則說明模型越確定這個數據點屬於分類1,反之屬於分類2,那些處於漸變色區域的點,也是模型覺得"模棱兩可"的點.
#在scikit-learn中,使用網格搜索GridSearchCV類時,如果要改變評分方式,只需修改scoring參數即可.
#如對隨機森林分類進行評分,可以直接這樣修改
#修改scoring參數為roc_auc
grid = GirdSearchCV(RandomForestClassifier(),param_grid = param_grid,scoring = 'roc_auc')

 

總結 : 

  SVC(支持向量機)的decision_function和GaussianNB(朴素貝葉斯)的predict_proba有相似之處,也有很大的差異.兩者都可以使用多元分類任務.

  我們使用交叉驗證法.網格搜索法,分類模型的可信度評估,這些方法都可以幫助我們對模型進行評估並找到相對較優的參數.

  還有.score方法給模型評分:對於分類模型來說,默認情況下.score給出的評分是模型分類的准確率(accuracy)

                對於回歸模型來說,默認情況下.score給出的評分回歸分析中的R平方的分數.

  其他對模型評分的方法 : 精度(Precision),召回率(Recall),f1分數(f1-score),ROC(Receiver Operating Characteristic Curve),AUC(Area Under Curve)

 

文章引自 : 《深入淺出python機器學習》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM