交叉验证之sklearn.model_selection.GridSearchCV

from sklearn.model_selection import GridSearchCV
tree_param_grid={'min_samples_split':list((3,6,9)),'n_estimators':list((10,50,100))}#对这3*3个参数组合的结果进行比较
grid=GridSearchCV(RandomForestRegressor(),param_grid=tree_param_grid,cv=5)
grid.fit(data_train,target_train)
grid.best_score_#成员变量,输出最好的结果
Out[45]:0.80685458833050794
grid.best_params_#成员变量,输出最好的结果对应的参数
Out[46]:{'min_samples_split': 3, 'n_estimators': 100}

sklearn.model_selection.GridSearchCV(estimator,
param_grid,
scoring=None,
fit_params=None,
n_jobs=None,
iid=’warn’,
refit=True,
cv=’warn’,
verbose=0,
pre_dispatch=‘2*n_jobs’,
error_score=’raise-deprecating’,
return_train_score=’warn’)

只介绍重要的几个参数:
estimator : estimator object
This is assumed to implement the scikit-learn estimator interface. Either estimator needs to provide a score function, or scoring must be passed.
要进行参数选择的模型

param_grid : dict or list of dictionaries
A single string (see The scoring parameter: defining model evaluation rules) or a callable (see Defining your scoring strategy from metric functions) to evaluate the predictions on the test set.
要进行评价的模型参数,用字典表示

cv : int, cross-validation generator or an iterable, optional
Determines the cross-validation splitting strategy. Possible inputs for cv are:
•None, to use the default 3-fold cross validation,
•integer, to specify the number of folds in a (Stratified)KFold,
•An object to be used as a cross-validation generator.
•An iterable yielding train, test splits.
交叉验证的交叉数。

猜你喜欢

转载自blog.csdn.net/Du_Shuang/article/details/84309305