Skip to main content
 首页 » 编程设计

python之GridSearchCV.best_score_ 得分设置为 'accuracy' 和 CV 时的含义

2025年05月04日15myhome

我正在尝试在著名的威斯康星癌症数据集(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 ,best_estimator 在遗漏数据上的得分”。我假设它是运行 tuned_pa​​rams 中指定的 8 种不同配置所获得的精度最高的,KFold 指定的次数,以及 KFold 指定的遗漏数据。我说得对吗?

还有一个问题。有没有一种方法可以找到用于 train_test_split(默认为 0.25)的最佳测试数据大小?

非常感谢

引用文献

请您参考如下方法:

grid.best_score_ 是您在 tuned_pa​​rams 中指定的单个参数组合的所有 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', 等,它提供了有关整个执行的附加信息。