交叉验证

对于线性回归:
方法一:之前的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

score算的是r-squared系数,好像score和cross_val_score默认算的就是r-squared系统

// 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=5表示cross_val_score采用的是k-fold cross validation的方法,重复5次交叉验证

实际上,cross_val_score能够用的方法有不少,如kFold, leave-one-out, ShuffleSplit等,举例而言:

//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

做者:dechuan 连接:https://www.jianshu.com/p/a4e94e72a46d 來源:简书 著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。
相关文章
相关标签/搜索