1、交叉验证cross validation
为了让被评估的模型更加准确可信
将训练数据分为训练集和验证集,分几等份就是几折验证
2、网格搜索grid search
超参数:很多参数需要手动指定
每组超参数都采用交叉验证来进行评估
代码示例
from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.datasets import load_iris # 查看数据集 iris = load_iris() # 训练集测试集拆分 X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.33, random_state=42) # 交叉验证 knn = KNeighborsClassifier() params = { "n_neighbors": [3, 5, 10] } gscv = GridSearchCV(knn, params, cv=2) gscv.fit(X_train, y_train) print(gscv.score(X_test, y_test)) print(gscv.best_score_) print(gscv.best_index_) print(gscv.best_estimator_) print(gscv.best_params_) print(gscv.cv_results_) """ 0.98 0.96 0 KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=3, p=2, weights='uniform') {'n_neighbors': 3} {'mean_fit_time': array([0.00049746, 0.00029266, 0.00028002]), 'std_fit_time': array([3.34978104e-05, 5.96046448e-07, 2.98023224e-06]), 'mean_score_time': array([0.00222301, 0.00057685, 0.00059712]), 'std_score_time': array([1.29294395e-03, 2.98023224e-06, 1.70469284e-05]), 'param_n_neighbors': masked_array(data=[3, 5, 10], mask=[False, False, False], fill_value='?', dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 10}], 'split0_test_score': array([0.94117647, 0.94117647, 0.94117647]), 'split1_test_score': array([0.97959184, 0.93877551, 0.95918367]), 'mean_test_score': array([0.96, 0.94, 0.95]), 'std_test_score': array([0.01920384, 0.00120024, 0.0090018 ]), 'rank_test_score': array([1, 3, 2], dtype=int32), 'split0_train_score': array([0.97959184, 0.95918367, 0.95918367]), 'split1_train_score': array([0.92156863, 0.94117647, 0.96078431]), 'mean_train_score': array([0.95058023, 0.95018007, 0.95998399]), 'std_train_score': array([0.0290116 , 0.0090036 , 0.00080032])} """