机器学习知识点(三十六)分类器性能度量指标f1-score
在用python機器學習庫scikit-learn訓練模型時,常用f1-score來度量模型性能,下面回顧和學習下這個指標。
內容概要?
- 模型評估的目的及一般評估流程
- 分類準確率的用處及其限制
- 混淆矩陣(confusion matrix)是如何表示一個分類器的性能
- 混淆矩陣中的度量是如何計算的
- 通過改變分類閾值來調整分類器性能
- ROC曲線的用處
- 曲線下面積(Area Under the Curve, AUC)與分類準確率的不同
1. 回顧?
模型評估可以用于在不同的模型類型、調節參數、特征組合中選擇適合的模型,所以我們需要一個模型評估的流程來估計訓練得到的模型對于非樣本數據的泛化能力,并且還需要恰當的模型評估度量手段來衡量模型的性能表現。
對于模型評估流程而言,之前介紹了K折交叉驗證的方法,針對模型評估度量方法,回歸問題可以采用平均絕對誤差(Mean Absolute Error)、均方誤差(Mean Squared Error)、均方根誤差(Root Mean Squared Error),而分類問題可以采用分類準確率和這篇文章中介紹的度量方法。
2. 分類準確率(Classification accuracy)?
這里我們使用Pima Indians Diabetes dataset,其中包含健康數據和糖尿病狀態數據,一共有768個病人的數據。
In?[1]: # read the data into a Pandas DataFrame import pandas as pd url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data' col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label'] pima = pd.read_csv(url, header=None, names=col_names) In?[2]: # print the first 5 rows of data pima.head() Out[2]:| 6 | 148 | 72 | 35 | 0 | 33.6 | 0.627 | 50 | 1 |
| 1 | 85 | 66 | 29 | 0 | 26.6 | 0.351 | 31 | 0 |
| 8 | 183 | 64 | 0 | 0 | 23.3 | 0.672 | 32 | 1 |
| 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | 21 | 0 |
| 0 | 137 | 40 | 35 | 168 | 43.1 | 2.288 | 33 | 1 |
上面表格中的label一列,1表示該病人有糖尿病,0表示該病人沒有糖尿病
In?[3]: # define X and y feature_cols = ['pregnant', 'insulin', 'bmi', 'age'] X = pima[feature_cols] y = pima.label In?[4]: # split X and y into training and testing sets from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) In?[5]: # train a logistic regression model on the training set from sklearn.linear_model import LogisticRegression logreg = LogisticRegression() logreg.fit(X_train, y_train) Out[5]: LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,intercept_scaling=1, max_iter=100, multi_class='ovr',penalty='l2', random_state=None, solver='liblinear', tol=0.0001,verbose=0) In?[6]: # make class predictions for the testing set y_pred_class = logreg.predict(X_test) In?[7]: # calculate accuracy from sklearn import metrics print metrics.accuracy_score(y_test, y_pred_class) 0.692708333333分類準確率分數是指所有分類正確的百分比。
空準確率(null accuracy)是指當模型總是預測比例較高的類別,那么其正確的比例是多少
In?[8]: # examine the class distribution of the testing set (using a Pandas Series method) y_test.value_counts() Out[8]: 0 130 1 62 dtype: int64 In?[9]: # calculate the percentage of ones y_test.mean() Out[9]: 0.32291666666666669 In?[10]: # calculate the percentage of zeros 1 - y_test.mean() Out[10]: 0.67708333333333326 In?[11]: # calculate null accuracy(for binary classification problems coded as 0/1) max(y_test.mean(), 1-y_test.mean()) Out[11]: 0.67708333333333326我們看到空準確率是68%,而分類準確率是69%,這說明該分類準確率并不是很好的模型度量方法,分類準確率的一個缺點是其不能表現任何有關測試數據的潛在分布。
In?[12]: # calculate null accuracy (for multi-class classification problems) y_test.value_counts().head(1) / len(y_test) Out[12]: 0 0.677083 dtype: float64比較真實和預測的類別響應值:
In?[13]: # print the first 25 true and predicted responses print "True:", y_test.values[0:25] print "Pred:", y_pred_class[0:25] True: [1 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 0] Pred: [0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]從上面真實值和預測值的比較中可以看出,當正確的類別是0時,預測的類別基本都是0;當正確的類別是1時,預測的類別大都不是1。換句話說,該訓練的模型大都在比例較高的那項類別的預測中預測正確,而在另外一中類別的預測中預測失敗,而我們沒法從分類準確率這項指標中發現這個問題。
分類準確率這一衡量分類器的標準比較容易理解,但是它不能告訴你響應值的潛在分布,并且它也不能告訴你分類器犯錯的類型。接下來介紹的混淆矩陣可以識別這個問題。
3. 混淆矩陣?
In?[14]: # IMPORTANT: first argument is true values, second argument is predicted values print metrics.confusion_matrix(y_test, y_pred_class) [[118 12][ 47 15]]
- 真陽性(True Positive,TP):指被分類器正確分類的正例數據
- 真陰性(True Negative,TN):指被分類器正確分類的負例數據
- 假陽性(False Positive,FP):被錯誤地標記為正例數據的負例數據
- 假陰性(False Negative,FN):被錯誤地標記為負例數據的正例數據
4. 基于混淆矩陣的評估度量?
準確率、識別率(Classification Accuracy):分類器正確分類的比例
In?[16]: print (TP+TN) / float(TP+TN+FN+FP) print metrics.accuracy_score(y_test, y_pred_class) 0.692708333333 0.692708333333錯誤率、誤分類率(Classification Error):分類器誤分類的比例
In?[17]: print (FP+FN) / float(TP+TN+FN+FP) print 1-metrics.accuracy_score(y_test, y_pred_class) 0.307291666667 0.307291666667考慮類不平衡問題,其中感興趣的主類是稀少的。即數據集的分布反映負類顯著地占多數,而正類占少數。故面對這種問題,需要其他的度量,評估分類器正確地識別正例數據的情況和正確地識別負例數據的情況。
靈敏性(Sensitivity),也稱為真正例識別率、召回率(Recall):正確識別的正例數據在實際正例數據中的百分比
In?[18]: print TP / float(TP+FN) recall = metrics.recall_score(y_test, y_pred_class) print metrics.recall_score(y_test, y_pred_class) 0.241935483871 0.241935483871特效性(Specificity),也稱為真負例率:正確識別的負例數據在實際負例數據中的百分比
In?[19]: print TN / float(TN+FP) 0.907692307692假陽率(False Positive Rate):實際值是負例數據,預測錯誤的百分比
In?[20]: print FP / float(TN+FP) specificity = TN / float(TN+FP) print 1 - specificity 0.0923076923077 0.0923076923077精度(Precision):看做精確性的度量,即標記為正類的數據實際為正例的百分比
In?[21]: print TP / float(TP+FP) precision = metrics.precision_score(y_test, y_pred_class) print precision 0.555555555556 0.555555555556F度量(又稱為F1分數或F分數),是使用精度和召回率的方法組合到一個度量上
F=2?precision?recallprecision+recall??F=2?precision?recallprecision+recall F?β?=(1+β?2?)?precision?recallβ?2??precision+recall??Fβ=(1+β2)?precision?recallβ2?precision+recall
F?F度量是精度和召回率的調和均值,它賦予精度和召回率相等的權重。
F?β??Fβ度量是精度和召回率的加權度量,它賦予召回率權重是賦予精度的β?β倍。
In?[22]: print (2*precision*recall) / (precision+recall) print metrics.f1_score(y_test, y_pred_class) 0.337078651685 0.337078651685總結
混淆矩陣賦予一個分類器性能表現更全面的認識,同時它通過計算各種分類度量,指導你進行模型選擇。
使用什么度量取決于具體的業務要求:
- 垃圾郵件過濾器:優先優化精度或者特效性,因為該應用對假陽性(非垃圾郵件被放進垃圾郵件箱)的要求高于對假陰性(垃圾郵件被放進正常的收件箱)的要求
- 欺詐交易檢測器:優先優化靈敏度,因為該應用對假陰性(欺詐行為未被檢測)的要求高于假陽性(正常交易被認為是欺詐)的要求
5. 調整分類的閾值?
In?[23]: # print the first 10 predicted responses logreg.predict(X_test)[0:10] Out[23]: array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1], dtype=int64) In?[24]: y_test.values[0:10] Out[24]: array([1, 0, 0, 1, 0, 0, 1, 1, 0, 0], dtype=int64) In?[25]: # print the first 10 predicted probabilities of class membership logreg.predict_proba(X_test)[0:10, :] Out[25]: array([[ 0.63247571, 0.36752429],[ 0.71643656, 0.28356344],[ 0.71104114, 0.28895886],[ 0.5858938 , 0.4141062 ],[ 0.84103973, 0.15896027],[ 0.82934844, 0.17065156],[ 0.50110974, 0.49889026],[ 0.48658459, 0.51341541],[ 0.72321388, 0.27678612],[ 0.32810562, 0.67189438]])上面的輸出中,第一列顯示的是預測值為0的百分比,第二列顯示的是預測值為1的百分比。
In?[26]: # print the first 10 predicted probabilities for class 1 logreg.predict_proba(X_test)[0:10, 1] Out[26]: array([ 0.36752429, 0.28356344, 0.28895886, 0.4141062 , 0.15896027,0.17065156, 0.49889026, 0.51341541, 0.27678612, 0.67189438])我們看到,預測為1的和實際的類別號差別很大,所以這里有50%作為分類的閾值顯然不太合理。于是我們將所有預測類別為1的百分比數據用直方圖的方式形象地表示出來,然后嘗試重新設置閾值。
In?[27]: # store the predicted probabilities for class 1 y_pred_prob = logreg.predict_proba(X_test)[:, 1] In?[28]: # allow plots to appear in the notebook %matplotlib inline import matplotlib.pyplot as plt In?[29]: # histogram of predicted probabilities plt.hist(y_pred_prob, bins=8) plt.xlim(0, 1) plt.title('Histogram of predicted probabilities') plt.xlabel('Predicted probability of diabetes') plt.ylabel('Frequency') Out[29]: <matplotlib.text.Text at 0x76853b0>我們發現在20%-30%之間的數高達45%,故以50%作為分類閾值時,只有很少的一部分數據會被認為是類別為1的情況。我們可以將閾值調小,以改變分類器的靈敏度和特效性。
In?[30]: # predict diabetes if the predicted probability is greater than 0.3 from sklearn.preprocessing import binarize y_pred_class = binarize(y_pred_prob, 0.3)[0] In?[31]: # print the first 10 predicted probabilities y_pred_prob[0:10] Out[31]: array([ 0.36752429, 0.28356344, 0.28895886, 0.4141062 , 0.15896027,0.17065156, 0.49889026, 0.51341541, 0.27678612, 0.67189438]) In?[32]: # print the first 10 predicted classes with the lower threshold y_pred_class[0:10] Out[32]: array([ 1., 0., 0., 1., 0., 0., 1., 1., 0., 1.]) In?[33]: y_test.values[0:10] Out[33]: array([1, 0, 0, 1, 0, 0, 1, 1, 0, 0], dtype=int64)從上面兩組數據對比來看,效果確實改善不少
In?[34]: # previous confusion matrix (default threshold of 0.5) print confusion [[118 12][ 47 15]] In?[35]: # new confusion matrix (threshold of 0.3) print metrics.confusion_matrix(y_test, y_pred_class) [[80 50][16 46]] In?[36]: # sensitivity has increased (used to be 0.24) print 46 / float(46 + 16) print metrics.recall_score(y_test, y_pred_class) 0.741935483871 0.741935483871 In?[37]: # specificity has decreased (used to be 0.91) print 80 / float(80 + 50) 0.615384615385總結:
- 0.5作為閾值時默認的情況
- 調節閾值可以改變靈敏性和特效性
- 靈敏性和特效性是一對相反作用的指標
- 該閾值的調節是作為改善分類性能的最后一步,應更多去關注分類器的選擇或構建更好的分類器
6. ROC曲線和AUC?
ROC曲線指受試者工作特征曲線/接收器操作特性(receiver operating characteristic,ROC)曲線, 是反映靈敏性和特效性連續變量的綜合指標,是用構圖法揭示敏感性和特異性的相互關系,它通過將連續變量設定出多個不同的臨界值,從而計算出一系列敏感性和特異性。
ROC曲線是根據一系列不同的二分類方式(分界值或決定閾),以真正例率(也就是靈敏度)(True Positive Rate,TPR)為縱坐標,假正例率(1-特效性)(False Positive Rate,FPR)為橫坐標繪制的曲線。
ROC觀察模型正確地識別正例的比例與模型錯誤地把負例數據識別成正例的比例之間的權衡。TPR的增加以FPR的增加為代價。ROC曲線下的面積是模型準確率的度量。
In?[38]: # IMPORTANT: first argument is true values, second argument is predicted probabilities fpr, tpr, thresholds = metrics.roc_curve(y_test, y_pred_prob) plt.plot(fpr, tpr) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.0]) plt.title('ROC curve for diabetes classifier') plt.xlabel('False Positive Rate (1 - Specificity)') plt.ylabel('True Positive Rate (Sensitivity)') plt.grid(True)ROC曲線上的每一個點對應于一個threshold,對于一個分類器,每個threshold下會有一個TPR和FPR。 比如Threshold最大時,TP=FP=0,對應于原點;Threshold最小時,TN=FN=0,對應于右上角的點(1,1)
正如上面所述,TPR的增加以FPR的增加為代價,所以ROC曲線可以幫助我們選擇一個可以平衡靈敏性和特效性的閾值。通過ROC曲線我們沒法看到響應閾值的對應關系,所以我們用下面的函數來查看。
In?[39]: # define a function that accepts a threshold and prints sensitivity and specificity def evaluate_threshold(threshold):print 'Sensitivity:', tpr[thresholds > threshold][-1]print 'Specificity:', 1 - fpr[thresholds > threshold][-1] In?[40]: evaluate_threshold(0.5) Sensitivity: 0.241935483871 Specificity: 0.907692307692 In?[41]: evaluate_threshold(0.3) Sensitivity: 0.741935483871 Specificity: 0.615384615385AUC(Area Under Curve)被定義為ROC曲線下的面積,也可以認為是ROC曲線下面積占單位面積的比例,顯然這個面積的數值不會大于1。又由于ROC曲線一般都處于y=x這條直線的上方,所以AUC的取值范圍在0.5和1之間。
對應AUC更大的分類器效果更好。所以AUC是衡量分類器性能的一個很好的度量,并且它不像分類準確率那樣,在類別比例差別很大的情況下,依然是很好的度量手段。在欺詐交易檢測中,由于欺詐案例是很小的一部分,這時分類準確率就不再是一個良好的度量,而可以使用AUC來度量。
In?[42]: # IMPORTANT: first argument is true values, second argument is predicted probabilities print metrics.roc_auc_score(y_test, y_pred_prob) 0.724565756824 In?[43]: # calculate cross-validated AUC from sklearn.cross_validation import cross_val_score cross_val_score(logreg, X, y, cv=10, scoring='roc_auc').mean() Out[43]: 0.73782336182336183參考資料?
- scikit-learn documentation:?Model evaluation
- ROC曲線-閾值評價標準
總結
以上是生活随笔為你收集整理的机器学习知识点(三十六)分类器性能度量指标f1-score的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【正一专栏】识时务者为俊杰——致敬杜兰特
- 下一篇: 【正一专栏】希望才是深深让人绝望的东西-