Python 在gridsearchcv期间打印网格搜索中使用的参数
我试图在网格搜索执行时查看gridsearchcv中自定义分数函数中当前使用的参数。理想情况下,这看起来像: 编辑:为了澄清,我希望使用网格搜索中的参数,因此我需要能够在函数中访问它们Python 在gridsearchcv期间打印网格搜索中使用的参数,python,scikit-learn,grid-search,Python,Scikit Learn,Grid Search,我试图在网格搜索执行时查看gridsearchcv中自定义分数函数中当前使用的参数。理想情况下,这看起来像: 编辑:为了澄清,我希望使用网格搜索中的参数,因此我需要能够在函数中访问它们 def fit(X, y): grid = {'max_features':[0.8,'sqrt'], 'subsample':[1, 0.7], 'min_samples_split' : [2, 3], 'min_samples
def fit(X, y):
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]}
clf = GradientBoostingClassifier()
score_func = make_scorer(make_custom_score, needs_proba=True)
model = GridSearchCV(estimator=clf,
param_grid=grid,
scoring=score_func,
cv=5)
def make_custom_score(y_true, y_score):
'''
y_true: array-like, shape = [n_samples] Ground truth (true relevance labels).
y_score : array-like, shape = [n_samples] Predicted scores
'''
print(parameters_used_in_current_gridsearch)
…
return score
我知道我可以在执行完成后获取参数,但我试图在代码执行时获取参数。不确定这是否满足您的用例,但有一个详细的参数仅适用于此类内容:
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import SGDRegressor
estimator = SGDRegressor()
gscv = GridSearchCV(estimator, {
'alpha': [0.001, 0.0001], 'average': [True, False],
'shuffle': [True, False], 'max_iter': [5], 'tol': [None]
}, cv=3, verbose=2)
gscv.fit([[1,1,1],[2,2,2],[3,3,3]], [1, 2, 3])
这会将以下内容打印到标准输出:
您可以参考文档,但也可以为更详细的内容指定更高的值。不确定这是否满足您的用例,但有一个详细参数仅适用于此类内容:
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import SGDRegressor
estimator = SGDRegressor()
gscv = GridSearchCV(estimator, {
'alpha': [0.001, 0.0001], 'average': [True, False],
'shuffle': [True, False], 'max_iter': [5], 'tol': [None]
}, cv=3, verbose=2)
gscv.fit([[1,1,1],[2,2,2],[3,3,3]], [1, 2, 3])
这会将以下内容打印到标准输出:
您可以参考文档,但也可以为更详细的内容指定更高的值。如果您需要在网格搜索步骤之间执行某些操作,则需要使用一些较低级别的Scikit学习功能编写自己的例程 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
如果您需要在网格搜索步骤之间实际执行某些操作,则需要使用一些较低级别的Scikit学习功能编写自己的例程 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
您可以让自己的记分员注意到分数和记分员之间的差异,而不是在自定义分数上使用make_scorer!!它接受三个参数和签名估计器,X_检验,y_检验。看
在该函数中,您可以访问已在网格搜索中的训练数据上训练的估计器对象。然后,您可以轻松访问该估计器的所有参数。但一定要返回一个浮点值作为分数
比如:
def make_custom_scorer(estimator, X_test, y_test):
'''
estimator: scikit-learn estimator, fitted on train data
X_test: array-like, shape = [n_samples, n_features] Data for prediction
y_test: array-like, shape = [n_samples] Ground truth (true relevance labels).
y_score : array-like, shape = [n_samples] Predicted scores
'''
# Here all_params is a dict of all the parameters in use
all_params = estimator.get_params()
# You need to do some filtering to get the parameters you want,
# but that should be easy I guess (just specify the key you want)
parameters_used_in_current_gridsearch = {k:v for k,v in all_params.items()
if k in ['max_features', 'subsample', ..., 'n_estimators']}
print(parameters_used_in_current_gridsearch)
y_score = estimator.predict(X_test)
# Use whichever metric you want here
score = scoring_function(y_test, y_score)
return score
您可以让自己的记分员注意到分数和记分员之间的差异,而不是在自定义分数上使用make_scorer!!它接受三个参数和签名估计器,X_检验,y_检验。看
在该函数中,您可以访问已在网格搜索中的训练数据上训练的估计器对象。然后,您可以轻松访问该估计器的所有参数。但一定要返回一个浮点值作为分数
比如:
def make_custom_scorer(estimator, X_test, y_test):
'''
estimator: scikit-learn estimator, fitted on train data
X_test: array-like, shape = [n_samples, n_features] Data for prediction
y_test: array-like, shape = [n_samples] Ground truth (true relevance labels).
y_score : array-like, shape = [n_samples] Predicted scores
'''
# Here all_params is a dict of all the parameters in use
all_params = estimator.get_params()
# You need to do some filtering to get the parameters you want,
# but that should be easy I guess (just specify the key you want)
parameters_used_in_current_gridsearch = {k:v for k,v in all_params.items()
if k in ['max_features', 'subsample', ..., 'n_estimators']}
print(parameters_used_in_current_gridsearch)
y_score = estimator.predict(X_test)
# Use whichever metric you want here
score = scoring_function(y_test, y_score)
return score
我看到了详细的参数,但我希望能够具体使用网格中的参数。因此,我需要能够访问函数中的实际参数。我已经看到了详细参数,但我希望能够专门使用网格中的参数。正因为如此,我需要能够访问函数中的实际参数。我最终使用这种方法来做我特别需要做的事情,所以我接受了这个答案。其他人的回答并不一定是错的,他们只是没有给我所需要的。@RyanL很高兴这有帮助。请注意,我在代码中犯了一个错误,使用ParameterSampler Random sampled而不是ParameterGrid deterministic Grid我最终使用这种方法来做我特别需要做的事情,所以我接受了这个答案。其他人的回答并不一定是错的,他们只是没有给我所需要的。@RyanL很高兴这有帮助。请注意,我在代码中犯了一个错误,使用ParameterSampler随机采样而不是ParameterGrid确定性网格