赞
踩
作者:Sivasai,来源:AI公园
导读
ML工作流中最困难的部分之一是为模型找到最好的超参数。 ML模型的性能与超参数直接相关。维基百科上说,“Hyperparameter optimization或tuning是为学习算法选择一组最优的hyperparameters的问题”。ML工作流中最困难的部分之一是为模型找到最好的超参数。ML模型的性能与超参数直接相关。超参数调优的越好,得到的模型就越好。调优超参数可能是非常乏味和困难的,更像是一门艺术而不是科学。
#importing required librariesfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.model_selection import train_test_splitfrom sklearn.model_selection import KFold , cross_val_scorefrom sklearn.datasets import load_winewine = load_wine()X = wine.datay = wine.target#splitting the data into train and test setX_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.3,random_state = 14)#declaring parameters gridk_value = list(range(2,11))algorithm = ['auto','ball_tree','kd_tree','brute']scores = []best_comb = []kfold = KFold(n_splits=5)#hyperparameter tunningfor algo in algorithm:for k in k_value: knn = KNeighborsClassifier(n_neighbors=k,algorithm=algo) results = cross_val_score(knn,X_train,y_train,cv = kfold) print(f'Score:{round(results.mean(),4)} with algo = {algo} , K = {k}') scores.append(results.mean()) best_comb.append((k,algo))best_param = best_comb[scores.index(max(scores))]print(f'\nThe Best Score : {max(scores)}')print(f"['algorithm': {best_param[1]} ,'n_neighbors': {best_param[0]}]")
缺点:
GridSearchCV
是如何工作的:
from sklearn.model_selection import GridSearchCVknn = KNeighborsClassifier()grid_param = { 'n_neighbors' : list(range(2,11)) , 'algorithm' : ['auto','ball_tree','kd_tree','brute'] }grid = GridSearchCV(knn,grid_param,cv = 5)grid.fit(X_train,y_train)#best parameter combinationgrid.best_params_#Score achieved with best parameter combinationgrid.best_score_#all combinations of hyperparametersgrid.cv_results_['params']#average scores of cross-validationgrid.cv_results_['mean_test_score']
缺点:
由于它尝试了超参数的每一个组合,并根据交叉验证得分选择了最佳组合,这使得GridsearchCV非常慢。
RandomizedSearchCV
是如何工作的,
from sklearn.model_selection import RandomizedSearchCVknn = KNeighborsClassifier()grid_param = { 'n_neighbors' : list(range(2,11)) , 'algorithm' : ['auto','ball_tree','kd_tree','brute'] }rand_ser = RandomizedSearchCV(knn,grid_param,n_iter=10)rand_ser.fit(X_train,y_train)#best parameter combinationrand_ser.best_params_#score achieved with best parameter combinationrand_ser.best_score_#all combinations of hyperparametersrand_ser.cv_results_['params']#average scores of cross-validationrand_ser.cv_results_['mean_test_score']
缺点:
随机搜索的问题是它不能保证给出最好的参数组合。
BayesSearchCV
来理解这
Installation: pip install scikit-optimize
from skopt import BayesSearchCVimport warningswarnings.filterwarnings("ignore")# parameter ranges are specified by one of belowfrom skopt.space import Real, Categorical, Integerknn = KNeighborsClassifier()#defining hyper-parameter gridgrid_param = { 'n_neighbors' : list(range(2,11)) , 'algorithm' : ['auto','ball_tree','kd_tree','brute'] }#initializing Bayesian SearchBayes = BayesSearchCV(knn , grid_param , n_iter=30 , random_state=14)Bayes.fit(X_train,y_train)#best parameter combinationBayes.best_params_#score achieved with best parameter combinationBayes.best_score_#all combinations of hyperparametersBayes.cv_results_['params']#average scores of cross-validationBayes.cv_results_['mean_test_score']
另一个实现贝叶斯搜索的类似库是
bayesian-optimization
。
Installation: pip install bayesian-optimization缺点: 要在2维或3维的搜索空间中得到一个好的代理曲面需要十几个样本,增加搜索空间的维数需要更多的样本。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。