超参数调优
- RandomSearch方法
- GridSearchCV调参
RandomSearch方法
from sklearn.model_selection import RandomizedSearchCV
n_estimators = [int(x) for x in np.linspace(start = 1, stop = 1000, num = 100)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 200, num = 20)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]
random_state=[int(x) for x in np.linspace(10, 200, num = 15)]
random_state.append(None)
random_grid = {'random_state':random_state,
'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
model_RandomForestRegressor = ensemble.RandomForestRegressor()
rf_random = RandomizedSearchCV(estimator=model_RandomForestRegressor, param_distributions=random_grid,
n_iter = 100, scoring='neg_mean_absolute_error',
cv = 3, verbose=2, random_state=2, n_jobs=-1)
rf_random.fit(x_train , y_train)
rf_random.best_params_
GridSearchCV调参
from sklearn.model_selection import GridSearchCV, KFold
random_state=[int(x) for x in np.linspace(0, 10, num = 1)]
random_state.append(None)
parameters = {'hidden_layer_sizes': [(4,4),(5,5),(6,6),(7,7),(8,8),(10,),(100,),(1000,)],
'random_state':random_state,
'activation':['logistic'],
'solver':['lbfgs'],
'alpha':[0.05],
'max_iter':[20000],
'learning_rate_init':[0.0001]
}
kfold = KFold(n_splits=10)
ann = MLPRegressor()
grid = GridSearchCV(ann, parameters, scoring='r2', cv=kfold)
grid = grid.fit(x_train, y_train)
reg = grid.best_estimator_
print('best score: %f'%grid.best_score_)
print('best parameters:')
for key in parameters.keys():
print('%s: %d'%(key, reg.get_params()[key]))
print('test score: %f'%reg.score(x_test, y_test))