GridSearchCV()或RandomizedSearchCV()方法的初級用法見:
http://www.reibang.com/writer#/notebooks/52233537/notes/117238898
Pipeline對象的使用方法見:
http://www.reibang.com/writer#/notebooks/52233537/notes/117140178
具體例子見(jupyter notebook):
E:\cgx硬盤\★Python and AI\(cgx★★)scikit learn 學習筆記\sklearn_cgx\GridSearchCV和RandomizedSearchCV參數搜索\GridSearchCV_and_Pipeline.ipynb
Cas1: Pipeline的每個計算步驟只搜索一種方法的一種或多種參數群扶;
這種情況很簡單直觀蕊蝗,在Pipeline中清晰定義了每一個計算步驟,然后在my_param_grid 中明確定義這幾個計算步驟需要被搜索的參數辰企、范圍闸昨、輪數蚯斯。
如下示例,Pipeline中明確定義了第1計算步驟 reduce_dim(自定義的字符串)用PCA()方法饵较,第2計算步驟 classify(自定義的字符串)用LinearSVC()方法拍嵌。
然后在my_param_grid1 中清晰指出了要搜索的參數是reduce_dim__n_components和classify__C,他們分別表示了PCA()的n_components參數和LinearSVC()的C參數循诉。
my_param_grid2 與 my_param_grid1本質完全相同横辆,只是多設置了一輪新的搜索。
# 定義Pipeline對象(這里每個計算步驟都是我們實際要搜索的方法茄猫,PCA()和LinearSVC())
pipe = Pipeline([('reduce_dim', PCA(iterated_power=7)),
('classify', LinearSVC(dual=False, max_iter=10000))])
# # 針對Pipeline對象的每個計算步驟狈蚤,設置參數搜索范圍
my_param_grid1 = {'reduce_dim__n_components': [2, 4, 8],
'classify__C': [1, 10, 100, 1000]} # dict (單輪搜索)
my_param_grid2 = [{'reduce_dim__n_components': [2, 4, 8],
'classify__C': [1, 10, 100, 1000]},
{'reduce_dim__n_components': [13, 20, 26],
'reduce_dim__n_oversamples': [5, 10],
'classify__C': [50, 100]}] # dict list (多輪搜索)
Cas2: Pipeline的每個計算步驟搜索多種方法的一種或多種參數;
與Cas1不同划纽,Cas2在Pipeline對應的每個步驟可以搜索多種方法脆侮,此時在Pipeline中定義的計算步驟只相當于做了一個占位符,而在my_param_grid 中為每個計算步驟設置了多種方法選擇勇劣,以及對應的需要被搜索的參數靖避、范圍、輪數比默。
如下示例幻捏,Pipeline中的第1計算步驟 reduce_dim 可以是'passthrough' 或 PCA(iterated_power=7) 或 [PCA(iterated_power=7), NMF()]...,前面說了退敦,可以直接將其視為為占位符粘咖;第2計算步驟 classify 用LinearSVC()方法,當然這里也可以用多個方法組成的list(但因為他是最后的估計器侈百,因此不能用'passthrough'瓮下,否則報錯)。
然后在my_param_grid1 中清晰指出了:
對于reduce_dim步驟钝域,真正要搜索的方法是[PCA(iterated_power=10), NMF(), FastICA()]三種讽坏,且針對這三種方法要搜索參數是'n_components(reduce_dim__n_components)'(需要注意的是,待搜索參數例证,必須是這些方法都有的參數路呜,否則會報錯)。
my_param_grid2 與 my_param_grid1本質完全相同,只是針對reduce_dim步驟還要搜索SelectKBest(chi2)方法的k參數胀葱,因為這個參數是PCA()和NMF()所沒有的漠秋,因此要單獨拿出來搜索。同時針對classify步驟抵屿,要搜索[SVC(), LinearSVC()]兩種方法的C參數庆锦。
# 定義Pipeline對象(這里每個計算步驟無論是:'passthrough'、PCA(iterated_power=7)還是[PCA(iterated_power=7), NMF()]轧葛,都是占個位置而已)
pipe = Pipeline([('reduce_dim', 'passthrough' 或 PCA(iterated_power=7) 或 [PCA(iterated_power=7), NMF()]),
('classify', LinearSVC(dual=False, max_iter=10000))])
# 針對Pipeline對象的每個計算步驟搂抒,真正設置搜索方法和對應搜索參數、范圍尿扯、輪數求晶。
my_param_grid1 = {'reduce_dim': [PCA(iterated_power=10), NMF(), FastICA()],
'reduce_dim__n_components': [2, 4, 8],
'classify__C': [1, 10, 100, 1000]} # dict
my_param_grid = [{'reduce_dim': [PCA(iterated_power=10), NMF(), FastICA()],
'reduce_dim__n_components': [2, 4, 8],
'classify__C': [1, 10, 100, 1000]},
{'reduce_dim': [SelectKBest(chi2)],
'reduce_dim__k': [2, 4, 8],
'classify': [SVC(), LinearSVC()],
'classify__C': [1, 10, 100, 1000]}]
注意
my_param_grid 中所有的dict的'key'的定義,由三部分組成衷笋,以‘reduce_dim__n_components’為例說明:
(1)第一部分芳杏,pipe對象中的‘key’:reduce_dim
(2)第二部分,兩個短下劃線:__
(3)第三部分右莱,reduce_dim對應的方法(這里是PCA)的輸入參數名稱:n_components
如果沒有太多的超參數需要調優(yōu)蚜锨,并且 pipeline 運行時間不長,請使用 GridSearchCV慢蜓;
對于較大的搜索空間和訓練緩慢的模型,請使用 HalvingGridSearchCV郭膛;
對于非常大的搜索空間和訓練緩慢的模型晨抡,請使用 HalvingRandomSearchCV。