机器学习:模型选择与调优交叉验证和网格搜索

网友投稿 202 2022-11-08

机器学习:模型选择与调优交叉验证和网格搜索

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])} """

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:四个嵌入式CPU标准面临的共同挑战
下一篇:机器学习:sklearn训练结果的保存和加载
相关文章

 发表评论

暂时没有评论,来抢沙发吧~