日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python自带超参调优包

發(fā)布時間:2025/3/19 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python自带超参调优包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、bayesian-optimization

安裝

pip install bayesian-optimization

前期準備

from sklearn.datasets import make_classification from sklearn.ensemble import RandomForestClassifier from sklearn.cross_validation import cross_val_score from bayes_opt import BayesianOptimization# 產(chǎn)生隨機分類數(shù)據(jù)集,10個特征, 2個類別 x, y = make_classification(n_samples=1000,n_features=10,n_classes=2)

我們先看看不調(diào)參的結(jié)果:

rf = RandomForestClassifier() print(np.mean(cross_val_score(rf, x, y, cv=20, scoring='roc_auc')))>>> 0.965162

可以看到,不調(diào)參的話模型20此交叉驗證AUC均值是0.965162,算是一個不錯的模型,那么如果用bayes調(diào)參結(jié)果會怎么樣呢

bayes調(diào)參初探

我們先定義一個目標函數(shù),里面放入我們希望優(yōu)化的函數(shù)。比如此時,函數(shù)輸入為隨機森林的所有參數(shù),輸出為模型交叉驗證5次的AUC均值,作為我們的目標函數(shù)。因為bayes_opt庫只支持最大值,所以最后的輸出如果是越小越好,那么需要在前面加上負號,以轉(zhuǎn)為最大值。由于bayes優(yōu)化只能優(yōu)化連續(xù)超參數(shù),因此要加上int()轉(zhuǎn)為離散超參數(shù)。

def rf_cv(n_estimators, min_samples_split, max_features, max_depth):val = cross_val_score(RandomForestClassifier(n_estimators=int(n_estimators),min_samples_split=int(min_samples_split),max_features=min(max_features, 0.999), # floatmax_depth=int(max_depth),random_state=2),x, y, scoring='roc_auc', cv=5).mean()return val

然后我們就可以實例化一個bayes優(yōu)化對象了:

rf_bo = BayesianOptimization(rf_cv,{'n_estimators': (10, 250),'min_samples_split': (2, 25),'max_features': (0.1, 0.999),'max_depth': (5, 15)})

里面的第一個參數(shù)是我們的優(yōu)化目標函數(shù),第二個參數(shù)是我們所需要輸入的超參數(shù)名稱,以及其范圍。超參數(shù)名稱必須和目標函數(shù)的輸入名稱一一對應(yīng)。

完成上面兩步之后,我們就可以運行bayes優(yōu)化了!

rf_bo.maximize()

完成的時候會不斷地輸出結(jié)果,如下圖所示:

等到程序結(jié)束,我們可以查看當前最優(yōu)的參數(shù)和結(jié)果:

rf_bo.res['max']>>> {'max_params': {'max_depth': 5.819908283575526,'max_features': 0.4951745603509127,'min_samples_split': 2.3110014720414958,'n_estimators': 249.73529231990733},'max_val': 0.9774079407940794}

bayes調(diào)參進階

上面bayes算法得到的參數(shù)并不一定最優(yōu),當然我們會遇到一種情況,就是我們已經(jīng)知道有一組或是幾組參數(shù)是非常好的了,我們想知道其附近有沒有更好的。這個操作相當于上文bayes優(yōu)化中的Explore操作,而bayes_opt庫給了我們實現(xiàn)此方法的函數(shù):

rf_bo.explore({'n_estimators': [10, 100, 200],'min_samples_split': [2, 10, 20],'max_features': [0.1, 0.5, 0.9],'max_depth': [5, 10, 15]} )

這里我們添加了三組較優(yōu)的超參數(shù),讓其在該參數(shù)基礎(chǔ)上進行explore,可能會得到更好的結(jié)果。

同時,我們還可以修改高斯過程的參數(shù),高斯過程主要參數(shù)是核函數(shù)(kernel),還有其他參數(shù)可以參考sklearn.gaussianprocess

gp_param={'kernel':None} rf_bo.maximize(**gp_param)

最終我們的到參數(shù)如下:

{'max_params': {'max_depth': 5.819908283575526,'max_features': 0.4951745603509127,'min_samples_split': 2.3110014720414958,'n_estimators': 249.73529231990733},'max_val': 0.9774079407940794}

運行交叉驗證測試一下:

rf = RandomForestClassifier(max_depth=6, max_features=0.39517, min_samples_split=2, n_estimators=250) np.mean(cross_val_score(rf, x, y, cv=20, scoring='roc_auc')) >>> 0.9754953

得到最終結(jié)果是0.9755,比之前的0.9652提高了約0.01,做過kaggle的朋友都懂,這在后期已經(jīng)是非常大的提高了!到后面想提高0.001都極其困難,因此bayes優(yōu)化真的非常強大!

結(jié)束!

Reference

  • [1] J. Snoek, H. Larochelle, and R. P. Adams, “Practical bayesianoptimization of machine learning algorithms,” in Advances in neural information processing systems, 2012, pp. 2951–2959.
  • [2] 高斯過程:http://www.gaussianprocess.org/gpml/
  • [3] 高斯過程:https://www.zhihu.com/question/46631426?sort=created
  • [4] 高斯過程:http://www.360doc.com/content/17/0810/05/43535834_678049865.shtml
  • [5] Brochu E, Cora V M, De Freitas N. A tutorial on Bayesian optimization of expensive cost functions, with application to active user modeling and hierarchical reinforcement learning[J]. arXiv preprint arXiv:1012.2599, 2010.

?二、Hyperopt
? 安裝:

pip install hyperopt def q (args) :x, y = argsreturn x ?? 2 + y ?? 2

Hyperopt提供了一個優(yōu)化接口,這個接口接受一個評估函數(shù)和參數(shù)空間,能計算出參數(shù)空間內(nèi)的一個點的損失函數(shù)值。用戶還要指定空間內(nèi)參數(shù)的分布情況。?
Hyheropt四個重要的因素:指定需要最小化的函數(shù),搜索的空間,采樣的數(shù)據(jù)集(trails database)(可選),搜索的算法(可選)。?
首先,定義一個目標函數(shù),接受一個變量,計算后返回一個函數(shù)的損失值,比如要最小化函數(shù)q(x,y) = x**2 + y**2:
?

from hyperopt import hp space = [hp.uniform(’x’, 0, 1), hp.normal(’y’, 0, 1)]

然后,定義一個參數(shù)空間,比如x在0-1區(qū)間內(nèi)取值,y是實數(shù),所以

第三,指定搜索的算法,算法也就是hyperopt的fmin函數(shù)的algo參數(shù)的取值。當前支持的算法由隨機搜索(對應(yīng)是hyperopt.rand.suggest),模擬退火(對應(yīng)是hyperopt.anneal.suggest),TPE算法。舉個栗子:
?

from hyperopt import hp, fmin, rand, tpe, space_eval best = fmin(q, space, algo=rand.suggest) print space_eval(space, best)

?搜索算法本身也有內(nèi)置的參數(shù)決定如何去優(yōu)化目標函數(shù),我們可以指定搜索算法的參數(shù),比如針對TPE,指定jobs:

?

from functools import partial from hyperopt import hp, fmin, tpe algo = partial(tpe.suggest, n_startup_jobs=10) best = fmin(q, space, algo=algo) print space_eval(space, best)

關(guān)于參數(shù)空間的設(shè)置,比如優(yōu)化函數(shù)q,輸入fmin(q,space=hp.uniform(‘a(chǎn)’,0,1)).hp.uniform函數(shù)的第一個參數(shù)是標簽,每個超參數(shù)在參數(shù)空間內(nèi)必須具有獨一無二的標簽。hp.uniform指定了參數(shù)的分布。其他的參數(shù)分布比如?
hp.choice返回一個選項,選項可以是list或者tuple.options可以是嵌套的表達式,用于組成條件參數(shù)。?
hp.pchoice(label,p_options)以一定的概率返回一個p_options的一個選項。這個選項使得函數(shù)在搜索過程中對每個選項的可能性不均勻。?
hp.uniform(label,low,high)參數(shù)在low和high之間均勻分布。?
hp.quniform(label,low,high,q),參數(shù)的取值是round(uniform(low,high)/q)*q,適用于那些離散的取值。?
hp.loguniform(label,low,high)繪制exp(uniform(low,high)),變量的取值范圍是[exp(low),exp(high)]?
hp.randint(label,upper) 返回一個在[0,upper)前閉后開的區(qū)間內(nèi)的隨機整數(shù)。?
搜索空間可以含有l(wèi)ist和dictionary.
?

from hyperopt import hp list_space = [ hp.uniform(’a’, 0, 1), hp.loguniform(’b’, 0, 1)] tuple_space = ( hp.uniform(’a’, 0, 1), hp.loguniform(’b’, 0, 1)) dict_space = { ’a’: hp.uniform(’a’, 0, 1), ’b’: hp.loguniform(’b’, 0, 1)}

使用sample函數(shù)從參數(shù)空間內(nèi)采樣:
?

from hyperopt.pyll.stochasti import sample print sample(list_space) # => [0.13, .235] print sample(nested_space) # => [[{’case’: 1, ’a’, 0.12‘}, {’case’: 2, ’b’: 2.3}], # ’extra_literal_string’, # 3]

在參數(shù)空間內(nèi)使用函數(shù):
?

from hyperopt.pyll import scope def foo(x): return str(x) ? 3 expr_space = { ’a’: 1 + hp.uniform(’a’, 0, 1), ’b’: scope.minimum(hp.loguniform(’b’, 0, 1), 10), ’c’: scope.call(foo, args=(hp.randint(’c’, 5),)), }

—————–這是一條有點短的昏割線———————————–

在blog上發(fā)現(xiàn)了一段使用感知器判別鳶尾花數(shù)據(jù)的代碼,使用的學(xué)習(xí)率是0.1,迭代40次得到了一個測試集上正確率為82%的結(jié)果。使用hyperopt優(yōu)化參數(shù),將正確率提升到了91%。

from sklearn import datasets import numpy as np from sklearn.cross_validation import train_test_split from sklearn.metrics import accuracy_score iris = datasets.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=0)from sklearn.preprocessing import StandardScaler sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test)from sklearn.linear_model import Perceptron ppn = Perceptron(n_iter=40, eta0=0.1, random_state=0) ppn.fit(X_train_std, y_train)y_pred = ppn.predict(X_test_std) print accuracy_score(y_test, y_pred)def percept(args):global X_train_std,y_train,y_testppn = Perceptron(n_iter=int(args["n_iter"]),eta0=args["eta"]*0.01,random_state=0)ppn.fit(X_train_std, y_train)y_pred = ppn.predict(X_test_std)return -accuracy_score(y_test, y_pred)from hyperopt import fmin,tpe,hp,partial space = {"n_iter":hp.choice("n_iter",range(30,50)),"eta":hp.uniform("eta",0.05,0.5)} algo = partial(tpe.suggest,n_startup_jobs=10) best = fmin(percept,space,algo = algo,max_evals=100) print best print percept(best) #0.822222222222 #{'n_iter': 14, 'eta': 0.12877033763511717} #-0.911111111111

xgboost具有很多的參數(shù),把xgboost的代碼寫成一個函數(shù),然后傳入fmin中進行參數(shù)優(yōu)化,將交叉驗證的auc作為優(yōu)化目標。auc越大越好,由于fmin是求最小值,因此求-auc的最小值。所用的數(shù)據(jù)集是202列的數(shù)據(jù)集,第一列樣本id,最后一列是label,中間200列是屬性。
?

#coding:utf-8 import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler import xgboost as xgb from random import shuffle from xgboost.sklearn import XGBClassifier from sklearn.cross_validation import cross_val_score import pickle import time from hyperopt import fmin, tpe, hp,space_eval,rand,Trials,partial,STATUS_OKdef loadFile(fileName = "E://zalei//browsetop200Pca.csv"):data = pd.read_csv(fileName,header=None)data = data.valuesreturn datadata = loadFile() label = data[:,-1] attrs = data[:,:-1] labels = label.reshape((1,-1)) label = labels.tolist()[0]minmaxscaler = MinMaxScaler() attrs = minmaxscaler.fit_transform(attrs)index = range(0,len(label)) shuffle(index) trainIndex = index[:int(len(label)*0.7)] print len(trainIndex) testIndex = index[int(len(label)*0.7):] print len(testIndex) attr_train = attrs[trainIndex,:] print attr_train.shape attr_test = attrs[testIndex,:] print attr_test.shape label_train = labels[:,trainIndex].tolist()[0] print len(label_train) label_test = labels[:,testIndex].tolist()[0] print len(label_test) print np.mat(label_train).reshape((-1,1)).shapedef GBM(argsDict):max_depth = argsDict["max_depth"] + 5n_estimators = argsDict['n_estimators'] * 5 + 50learning_rate = argsDict["learning_rate"] * 0.02 + 0.05subsample = argsDict["subsample"] * 0.1 + 0.7min_child_weight = argsDict["min_child_weight"]+1print "max_depth:" + str(max_depth)print "n_estimator:" + str(n_estimators)print "learning_rate:" + str(learning_rate)print "subsample:" + str(subsample)print "min_child_weight:" + str(min_child_weight)global attr_train,label_traingbm = xgb.XGBClassifier(nthread=4, #進程數(shù)max_depth=max_depth, #最大深度n_estimators=n_estimators, #樹的數(shù)量learning_rate=learning_rate, #學(xué)習(xí)率subsample=subsample, #采樣數(shù)min_child_weight=min_child_weight, #孩子數(shù)max_delta_step = 10, #10步不降則停止objective="binary:logistic")metric = cross_val_score(gbm,attr_train,label_train,cv=5,scoring="roc_auc").mean()print metricreturn -metricspace = {"max_depth":hp.randint("max_depth",15),"n_estimators":hp.randint("n_estimators",10), #[0,1,2,3,4,5] -> [50,]"learning_rate":hp.randint("learning_rate",6), #[0,1,2,3,4,5] -> 0.05,0.06"subsample":hp.randint("subsample",4),#[0,1,2,3] -> [0.7,0.8,0.9,1.0]"min_child_weight":hp.randint("min_child_weight",5), #} algo = partial(tpe.suggest,n_startup_jobs=1) best = fmin(GBM,space,algo=algo,max_evals=4)print best print GBM(best)

?

總結(jié)

以上是生活随笔為你收集整理的python自带超参调优包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。