GridSearchCV.best_score_ 得分设置为 'accuracy' 和 CV 时的含义
GridSearchCV.best_score_ meaning when scoring set to 'accuracy' and CV
我试图在著名的威斯康星癌症数据集(569 个样本,31 个特征 + 目标)上找到应用于乳腺癌样本分类的最佳模型神经网络模型。我正在使用 sklearn 0.18.1。到目前为止我没有使用规范化。等我解决这个问题再补充。
# some init code omitted
X_train, X_test, y_train, y_test = train_test_split(X, y)
为 GridSearchCV 定义参数 NN 参数
tuned_params = [{'solver': ['sgd'], 'learning_rate': ['constant'], "learning_rate_init" : [0.001, 0.01, 0.05, 0.1]},
{"learning_rate_init" : [0.001, 0.01, 0.05, 0.1]}]
CV 方法和模型
cv_method = KFold(n_splits=4, shuffle=True)
model = MLPClassifier()
应用网格
grid = GridSearchCV(estimator=model, param_grid=tuned_params, cv=cv_method, scoring='accuracy')
grid.fit(X_train, y_train)
y_pred = grid.predict(X_test)
如果我 运行:
print(grid.best_score_)
print(accuracy_score(y_test, y_pred))
结果是0.746478873239和0.902097902098
根据文档 "best_score_ : float, Score of best_estimator on the left out data"。我假设它是 运行 在 tuned_params 中指定的 8 种不同配置中获得的精度最高的,左边是 KFold 指定的次数输出由 KFold 指定的数据。我说得对吗?
还有一个问题。有没有一种方法可以找到在 train_test_split 中使用的测试数据的最佳大小,默认为 0.25?
非常感谢
参考资料
- http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html
- http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV
- http://scikit-learn.org/stable/modules/grid_search.html
- http://scikit-learn.org/stable/modules/cross_validation.html
- http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html#sphx-glr-auto-examples-model-selection-plot-nested-cross-validation-iris-py
grid.best_score_
是您在 tuned_params
中指定的单个参数组合的所有 cv 折叠的平均值。
为了访问有关网格搜索过程的其他相关详细信息,您可以查看 grid.cv_results_
属性。
来自documentation of GridSearchCV:
cv_results_ : dict of numpy (masked) ndarrays
A dict with keys as column headers and values as columns,
that can be imported into a pandas DataFrame
它包含像 'split0_test_score' 这样的键,
'split1_test_score',
'mean_test_score',
'std_test_score',
'rank_test_score',
'split0_train_score',
'split1_train_score',
'mean_train_score',
等,它提供了有关整个执行的附加信息。
我试图在著名的威斯康星癌症数据集(569 个样本,31 个特征 + 目标)上找到应用于乳腺癌样本分类的最佳模型神经网络模型。我正在使用 sklearn 0.18.1。到目前为止我没有使用规范化。等我解决这个问题再补充。
# some init code omitted
X_train, X_test, y_train, y_test = train_test_split(X, y)
为 GridSearchCV 定义参数 NN 参数
tuned_params = [{'solver': ['sgd'], 'learning_rate': ['constant'], "learning_rate_init" : [0.001, 0.01, 0.05, 0.1]},
{"learning_rate_init" : [0.001, 0.01, 0.05, 0.1]}]
CV 方法和模型
cv_method = KFold(n_splits=4, shuffle=True)
model = MLPClassifier()
应用网格
grid = GridSearchCV(estimator=model, param_grid=tuned_params, cv=cv_method, scoring='accuracy')
grid.fit(X_train, y_train)
y_pred = grid.predict(X_test)
如果我 运行:
print(grid.best_score_)
print(accuracy_score(y_test, y_pred))
结果是0.746478873239和0.902097902098
根据文档 "best_score_ : float, Score of best_estimator on the left out data"。我假设它是 运行 在 tuned_params 中指定的 8 种不同配置中获得的精度最高的,左边是 KFold 指定的次数输出由 KFold 指定的数据。我说得对吗?
还有一个问题。有没有一种方法可以找到在 train_test_split 中使用的测试数据的最佳大小,默认为 0.25?
非常感谢
参考资料
- http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html
- http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV
- http://scikit-learn.org/stable/modules/grid_search.html
- http://scikit-learn.org/stable/modules/cross_validation.html
- http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html#sphx-glr-auto-examples-model-selection-plot-nested-cross-validation-iris-py
grid.best_score_
是您在 tuned_params
中指定的单个参数组合的所有 cv 折叠的平均值。
为了访问有关网格搜索过程的其他相关详细信息,您可以查看 grid.cv_results_
属性。
来自documentation of GridSearchCV:
cv_results_ : dict of numpy (masked) ndarrays
A dict with keys as column headers and values as columns, that can be imported into a pandas DataFrame
它包含像 'split0_test_score' 这样的键, 'split1_test_score', 'mean_test_score', 'std_test_score', 'rank_test_score', 'split0_train_score', 'split1_train_score', 'mean_train_score', 等,它提供了有关整个执行的附加信息。