python 调整超参数算法gridsearch
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 调整超参数算法gridsearch相关的知识,希望对你有一定的参考价值。
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import GridSearchCV
## customize model
def customize_model(X:'array',y:'array',model:'scikit model',dparams:dict,njobs:int=2,ncv:int=3)->dict:
grid = GridSearchCV(estimator=model, param_grid=dparams, n_jobs=njobs, cv=ncv)
grid.fit(X, Y)
print('Best score = %s'%grid.best_score_)
print('Best estimator:\n%s'%grid.best_estimator_)
print('Best params:')
for k, v in grid.best_params_.items():
print('- %s: %s'%(k,v))
return grid.best_params_
# best parameters for ElasticNet model
alphas = numpy.array([1,0.1,0.01,0.001,0.0001,0])
l1_ratio = numpy.array([1,0.1,0.01,0.001,0.0001,0])
dparams = dict(alpha=alphas,l1_ratio=l1_ratio)
model = ElasticNet(random_state=0,fit_intercept=True)
customize_model(X,y,model,dparams)
from sklearn.model_selection import ParameterGrid
grid = ParameterGrid({'criterion':['mse','friedman_mse','mae'],'max_depth':[3,4,5,6,7,8,9,10]})
for params in grid:
print(params)
### simulation with each set of params
"""
[out]:
{'criterion': 'mse', 'max_depth': 3}
{'criterion': 'mse', 'max_depth': 4}
{'criterion': 'mse', 'max_depth': 5}
{'criterion': 'mse', 'max_depth': 6}
{'criterion': 'mse', 'max_depth': 7}
{'criterion': 'mse', 'max_depth': 8}
{'criterion': 'mse', 'max_depth': 9}
{'criterion': 'mse', 'max_depth': 10}
{'criterion': 'friedman_mse', 'max_depth': 3}
{'criterion': 'friedman_mse', 'max_depth': 4}
{'criterion': 'friedman_mse', 'max_depth': 5}
{'criterion': 'friedman_mse', 'max_depth': 6}
{'criterion': 'friedman_mse', 'max_depth': 7}
{'criterion': 'friedman_mse', 'max_depth': 8}
{'criterion': 'friedman_mse', 'max_depth': 9}
{'criterion': 'friedman_mse', 'max_depth': 10}
{'criterion': 'mae', 'max_depth': 3}
{'criterion': 'mae', 'max_depth': 4}
{'criterion': 'mae', 'max_depth': 5}
{'criterion': 'mae', 'max_depth': 6}
{'criterion': 'mae', 'max_depth': 7}
{'criterion': 'mae', 'max_depth': 8}
{'criterion': 'mae', 'max_depth': 9}
{'criterion': 'mae', 'max_depth': 10}
"""
# Grid Search for Algorithm Tuning
from pandas import read_csv
import numpy
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
url = "https://goo.gl/vhm1eU"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = read_csv(url, names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
alphas = numpy.array([1,0.1,0.01,0.001,0.0001,0])
param_grid = dict(alpha=alphas)
model = Ridge()
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid.fit(X, Y)
print(grid.best_score_)
print(grid.best_params_.alpha)
print(grid.best_estimator_)
以上是关于python 调整超参数算法gridsearch的主要内容,如果未能解决你的问题,请参考以下文章
使用 Gridsearch 进行超参数搜索,给出不起作用的参数值
我应该在 SMOTE 之前还是之后执行网格搜索(用于调整超参数)?
使用 GridSearch 进行超参数优化
火炉炼AI机器学习017-使用GridSearch搜索最佳参数组合
Python:没有机器学习的网格搜索?
网格搜索预处理多个超参数和多个估计器