3 回答
TA贡献1911条经验 获得超7个赞
如果您需要在网格搜索步骤之间实际执行某些操作,则需要使用一些较低级别的 Scikit-learn 功能编写自己的例程。
GridSearchCV在内部使用ParameterGrid该类,您可以对其进行迭代以获得参数值的组合。
基本循环看起来像这样
import sklearn
from sklearn.model_selection import ParameterGrid, KFold
clf = GradientBoostingClassifier()
grid = {
'max_features': [0.8,'sqrt'],
'subsample': [1, 0.7],
'min_samples_split': [2, 3],
'min_samples_leaf': [1, 3],
'learning_rate': [0.01, 0.1],
'max_depth': [3, 8, 15],
'n_estimators': [10, 20, 50]
}
scorer = make_scorer(make_custom_score, needs_proba=True)
sampler = ParameterGrid(grid)
cv = KFold(5)
for params in sampler:
for ix_train, ix_test in cv.split(X, y):
clf_fitted = clone(clf).fit(X[ix_train], y[ix_train])
score = scorer(clf_fitted, X[ix_test], y[ix_test])
# do something with the results
添加回答
举报