對於線性迴歸:
方法一:之前的cross validation中有一種方法是train/test split,如今挪到model_selection庫中,randomly partition the data into training and test sets, by default, 25 percent of the data is assigned to the test set。這種方法只能獲得一次劃分結果的評估結果,不許確。dom
// from sklearn.model_selection import train_test_split
// X_train,X_test,y_train,y_test=train_test_split(X,y)
// model=LinearRegression()
// model.fit(X,y)
// model.score(X_test,y_test)性能
方法二:用model_selection庫中的cross_val_score
// from sklearn.model_selection import cross_val_score
// model=LinearRegression()
// scores=cross_val_score(model,X,y,cv=5)ci
//cv=ShuffleSplit(n_splits=3,test_size=0.3,random_state=0)
//cross_val_score(model, X,y, cv=cv)it
對於邏輯迴歸:
邏輯迴歸用於處理分類問題,線性迴歸求解how far it was from the decision boundary(求距離)的評估方式明顯不適合分類問題。
The most common metrics are accuracy, precision, recall, F1 measure, true negatives, false positives and false negatives
一、計算confusion matrix
Confusion matrix 由 true positives, true negatives, false positives以及 false negatives組成。
// confusion_matrix=confusion_matrix(y_test, y_pred)
二、accuracy: measures a fraction of the classifier's predictions that are correct.
// accuracy_score(y_true,y_pred)
LogisticRegression.score() 默認使用accuracy
三、precision: 好比說咱們預測得了cancer中實際確實得病的百分比
// classifier=LogisticRegression()
// classifier.fit(X_train,y_train)
// precisions= cross_val_score(classifier, X_train,y_train,cv=5,scoring='precision')
四、recall: 好比說實際得了cancer,被咱們預測出來的百分比
// recalls= cross_val_score(classifier,X_train,y_train,cv=5,scoring='recall')
五、precision和recall之間是一個trade-off的關係,用F1score來表徵性能,F1score越高越好
// fls=cross_val_score(classifier, X_train, y_train, cv=5,scoring='f1')
六、ROC曲線和AUC的值
ROC曲線的橫座標爲false positive rate(FPR),縱座標爲true positive rate(TPR)
AUC數值=ROC曲線下的面積
// classifier=LogisticRegression()
// classifier.fit(X_train, y_train)
// predictions = classifier.predict_proba(X_test)
// false_positive_rate, recall, thresholds = roc_curve(y_test, predictions[:,1])
// roc_auc=auc(false_positive_rate, recall)io