分類模型——Logistics Regression

葉青婧發表於2019-02-16

Logistics regression

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(x_train, y_train)  

準確率與召回率

準確率:scikit-learn提供了accuracy_score來計算:LogisticRegression.score()
準確率是分類器預測正確性的比例,但是並不能分辨出假陽性錯誤和假陰性錯誤
精確率是指分類器預測出的垃圾簡訊中真的是垃圾簡訊的比例,P=TP/(TP+FP)
召回率在醫學上也叫做靈敏度,在本例中知所有真的垃圾簡訊被分類器正確找出來的比例,R=TP/(TP+FN)

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score  
predictions = clf.predict(x_test)  
print(`準確率:`, accuracy_score(y_test, predictions))  
print(`精確率:`, precision_score(y_test, predictions))  
print(`召回率:`, recall_score(y_test, predictions))  
print(`F1-Score:`, f1_score(y_test, predictions))  

from sklearn.metrics import classification_report, accuracy_score, confusion_matrix  
predictions = clf.predict(x_test)  
print(`準確率:`, accuracy_score(y_test, predictions))  
print(`混淆矩陣:`, confusion_matrix(y_test, predictions))  
print(`分類報告:`, classification_report(y_test, predictions))  

ROC AUC

ROC曲線(Receiver Operating Characteristic,ROC curve)可以用來視覺化分類器的效果。和準確率不同,ROC曲線對分類比例不平衡的資料集不敏感,ROC曲線顯示的是對超過限定閾值的所有預測結果的分類器效果。ROC曲線畫的是分類器的召回率與誤警率(fall-out)的曲線。誤警率也稱假陽性率,是所有陰性樣本中分類器識別為陽性的樣本所佔比例:
F=FP/(TN+FP) AUC是ROC曲線下方的面積,它把ROC曲線變成一個值,表示分類器隨機預測的效果.

from sklearn.metrics import roc_curve, auc  
predictions = clf.predict_proba(x_test)  
false_positive_rate, recall, thresholds = roc_curve(y_test, predictions[:, 1])  
roc_auc = auc(false_positive_rate, recall)  
plt.title(`Receiver Operating Characteristic`)  
plt.plot(false_positive_rate, recall, `b`, label=`AUC = %0.2f` % roc_auc)  
plt.legend(loc=`lower right`)  
plt.plot([0, 1], [0, 1], `r--`)  
plt.xlim([0.0, 1.0])  
plt.ylim([0.0, 1.0])  
plt.ylabel(`Recall`)  
plt.xlabel(`Fall-out`)  
plt.show() 

模型原理

http://blog.csdn.net/sergeyca…
http://blog.csdn.net/zjuPeco/…

相關文章