# gridsearchcv参数_机器学习模型的超参数优化

## 引言

• 神经网络训练中的学习率
• 支持向量机中的 参数和 参数
• k 近邻算法中的 参数
……

## 超参数优化方法

### 2. 网格化寻优（Grid Search）

from sklearn.datasets import load_iris
from sklearn.svm import SVC
svc = SVR()
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVR
grid = GridSearchCV(
estimator=SVR(kernel='rbf'),
param_grid={
'C': [0.1, 1, 100, 1000],
'epsilon': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10],
'gamma': [0.0001, 0.001, 0.005, 0.1, 1, 3, 5]
},
cv=5, scoring='neg_mean_squared_error', verbose=0, n_jobs=-1)

grid.fit(X,y)

#print the best score throughout the grid search
print grid.best_score_
#print the best parameter used for the highest score of the model.
print grid.best_param_

## 随机寻优（Random Search）

<br>随机寻优方法

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(random_state = 42)
from sklearn.model_selection import RandomizedSearchCV
random_grid = {'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}
rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=42, n_jobs = -1)# Fit the random search model

rf_random.fit(X,y)

#print the best score throughout the grid search
print rf_random.best_score_
#print the best parameter used for the highest score of the model.
print rf_random.best_param_

Output:
{'bootstrap': True,
'max_depth': 70,
'max_features': 'auto',
'min_samples_leaf': 4,
'min_samples_split': 10,
'n_estimators': 400}

## 贝叶斯优化方法（Bayesian Optimization）

1. 超参数优化通常不是一个平滑的过程
2. 超参数优化往往具有非凸的性质

## 总结

deephub翻译组：Oliver Lee

• 0
点赞
• 0
收藏
• 0
评论
07-29 2938
09-20 4507
09-18 534
09-27 793
04-07 1369
08-14 1432

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助