小编在这里为大家推荐,郑州达内IT培训17年专业IT培训机构,美国上市集团,开设IT培训班Java、python、大数据、linux、UI、会计等IT培训,泛IT培训和非IT培训共24大课程,0元试听,随到随学,推荐就业!接下来为您分享,Python机器学习:超参数调优
# 超参数搜索空间
bds = [{'name': 'learning_rate', 'type': 'continuous', 'domain': (0, 1)},
{'name': 'gamma', 'type': 'continuous', 'domain': (0, 5)},
{'name': 'max_depth', 'type': 'continuous', 'domain': (1, 50)}]
# Optimization objective 模型F
def cv_score(parameters):
parametersparameters = parameters[0]
score = cross_val_score(
XGBRegressor(learning_rate=parameters[0],
gamma=int(parameters[1]),
max_depth=int(parameters[2])),
X, y, scoring='neg_mean_squared_error').mean()
score = np.array(score)
return score
# acquisition就是选择不同的Acquisition Function
optimizer = GPyOpt.methods.BayesianOptimization(f = cv_score, # function to optimize
domain = bds, # box-constraints of the problem
acquisition_type ='LCB', # LCB acquisition
acquisition_weight = 0.1) # Exploration exploitation
x_best = optimizer.X[np.argmax(optimizer.Y)]
print("Best parameters: learning_rate="+str(x_best[0])+",gamma="+str(x_best[1])+",max_depth="+str(x_best[2]))
# Best parameters: learning_rate=0.4272184438229706,gamma=1.4805727469635759,max_depth=41.8460390442754
·bayesian-optimization例子
from sklearn.datasets import make_classification
from xgboost import XGBRegressor
from sklearn.model_selection import cross_val_score
from bayes_opt import BayesianOptimization
iris = load_iris()
X = iris.data
y = iris.target
x_train, x_test, y_train, y_test = train_test_split(X,y,test_size = 0.3,random_state = 14)
bds ={'learning_rate': (0, 1),
'gamma': (0, 5),
'max_depth': (1, 50)}
# Optimization objective
def cv_score(learning_rate, gamma, max_depth):
score = cross_val_score(
XGBRegressor(learning_ratelearning_rate=learning_rate,
gamma=int(gamma),
max_depth=int(max_depth)),
X, y, scoring='neg_mean_squared_error').mean()
score = np.array(score)
return score
rf_bo = BayesianOptimization(
cv_score,
bds
)
rf_bo.maximize()
rf_bo.max
| iter | target | gamma | learni... | max_depth |
-------------------------------------------------------------
| 1 | -0.0907 | 0.7711 | 0.1819 | 20.33 |
| 2 | -0.1339 | 4.933 | 0.6599 | 8.972 |
| 3 | -0.07285 | 1.55 | 0.8247 | 33.94 |
| 4 | -0.1359 | 4.009 | 0.3994 | 25.55 |
| 5 | -0.08773 | 1.666 | 0.9551 | 48.67 |
| 6 | -0.05654 | 0.0398 | 0.3707 | 1.221 |
| 7 | -0.08425 | 0.6883 | 0.2564 | 33.25 |
| 8 | -0.1113 | 3.071 | 0.8913 | 1.051 |
| 9 | -0.9167 | 0.0 | 0.0 | 2.701 |
| 10 | -0.05267 | 0.0538 | 0.1293 | 1.32 |
| 11 | -0.08506 | 1.617 | 1.0 | 32.68 |
| 12 | -0.09036 | 2.483 | 0.2906 | 33.21 |
| 13 | -0.08969 | 0.4662 | 0.3612 | 34.74 |
| 14 | -0.0723 | 1.295 | 0.2061 | 1.043 |
| 15 | -0.07531 | 1.903 | 0.1182 | 35.11 |
| 16 | -0.08494 | 2.977 | 1.0 | 34.57 |
| 17 | -0.08506 | 1.231 | 1.0 | 36.05 |
| 18 | -0.07023 | 2.81 | 0.838 | 36.16 |
| 19 | -0.9167 | 1.94 | 0.0 | 36.99 |
| 20 | -0.09041 | 3.894 | 0.9442 | 35.52 |
| 21 | -0.1182 | 3.188 | 0.01882 | 35.14 |
| 22 | -0.08521 | 0.931 | 0.05693 | 31.66 |
| 23 | -0.1003 | 2.26 | 0.07555 | 31.78 |
| 24 | -0.1018 | 0.08563 | 0.9838 | 32.22 |
| 25 | -0.1017 | 0.8288 | 0.9947 | 30.57 |
| 26 | -0.9167 | 1.943 | 0.0 | 30.2 |
| 27 | -0.08506 | 1.518 | 1.0 | 35.04 |
| 28 | -0.08494 | 3.464 | 1.0 | 32.36 |
| 29 | -0.1224 | 4.296 | 0.4472 | 33.47 |
| 30 | -0.1017 | 0.0 | 1.0 | 35.86 |
=============================================================
{'target': -0.052665895082105285,
'params': {'gamma': 0.05379782654053811,
'learning_rate': 0.1292986176550608,
'max_depth': 1.3198257775801387}}
bayesian-optimization只支持较大化,如果score是越小越好,可以加一个负号转化为较大值优化。
两者的优化结果并不是十分一致的,可能和实现方式和选择的算法不同,也和初始化的拟合数据集有关系。
5. 总结
本文介绍三种超参数优化的策略,希望对你有帮助。简要总结如下:
·GridSearchCV网格搜索,给定超参和取值范围,遍历所有组合得到较优参数。首先你要给定一个先验的取值,不能取得太多,否则组合太多,耗时太长。可以启发式的尝试。
·RandomSearchCV随机搜索,搜索超参数的值不是固定,是在一定范围内随机的值
·贝叶斯优化,采用高斯过程迭代式的寻找较优参数,每次迭代都是在上一次迭代基础上拟合高斯函数上,寻找比上一次迭代更优的参数,推荐gpyopt库。
温馨提示:为了不影响您的时间,来校区前或者遇到不明白的问题请先电话咨询,方便我校安排相关课程的专业老师为您解答,选取适合您的课程。以上是郑州达内教育的小编为您分享的关于Python机器学习:超参数调优的内容,希望可以为同学们提供帮助,更多Python资讯请持续关注郑州达内教育。(来自:51testing.com)