1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
from sklearn.model_selection import cross_val_score
cross_val_score(estimator,X,y=None,cv=None) from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import cross_val_score
k_range = [2, 4, 5, 10] cv_scores = []
for k in k_range: knn = KNeighborsClassifier(n_neighbors=k) scores = cross_val_score(knn, X_train_scaled, y_train, cv=3)
cv_score = np.mean(scores) print('k={},验证集上的准确率={:.3f}'.format(k, cv_score)) cv_scores.append(cv_score) sklearn.model_selection.validation_curve(estimator, X, y, param_name, param_range, cv=None, scoring=None) 参数: - estimator: 训练器 - X: 训练集上选择的特征 - y:训练集上的要预测值 - param_name :变化的参数的名称 - param_range : 参数变化的范围 - cv:交叉验证的折数 - scoring:采用的模型评价标准 from sklearn.model_selection import validation_curve train_scores, test_scores = validation_curve(SVC(kernel='linear'), X_train_scaled, y_train, param_name='C', param_range=c_range, cv=5, scoring='accuracy')
|