GridSearchCV()或RandomizedSearchCV()方法的初級用法見:
http://www.itdecent.cn/writer#/notebooks/52233537/notes/117238898
Pipeline對象的使用方法見:
http://www.itdecent.cn/writer#/notebooks/52233537/notes/117140178
具體例子見(jupyter notebook):
E:\cgx硬盤\★Python and AI\(cgx★★)scikit learn 學習筆記\sklearn_cgx\GridSearchCV和RandomizedSearchCV參數(shù)搜索\GridSearchCV_and_Pipeline.ipynb
Cas1: Pipeline的每個計算步驟只搜索一種方法的一種或多種參數(shù);
這種情況很簡單直觀,在Pipeline中清晰定義了每一個計算步驟,然后在my_param_grid 中明確定義這幾個計算步驟需要被搜索的參數(shù)、范圍、輪數(shù)。
如下示例,Pipeline中明確定義了第1計算步驟 reduce_dim(自定義的字符串)用PCA()方法,第2計算步驟 classify(自定義的字符串)用LinearSVC()方法。
然后在my_param_grid1 中清晰指出了要搜索的參數(shù)是reduce_dim__n_components和classify__C,他們分別表示了PCA()的n_components參數(shù)和LinearSVC()的C參數(shù)。
my_param_grid2 與 my_param_grid1本質(zhì)完全相同,只是多設置了一輪新的搜索。
# 定義Pipeline對象(這里每個計算步驟都是我們實際要搜索的方法,PCA()和LinearSVC())
pipe = Pipeline([('reduce_dim', PCA(iterated_power=7)),
('classify', LinearSVC(dual=False, max_iter=10000))])
# # 針對Pipeline對象的每個計算步驟,設置參數(shù)搜索范圍
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的每個計算步驟搜索多種方法的一種或多種參數(shù);
與Cas1不同,Cas2在Pipeline對應的每個步驟可以搜索多種方法,此時在Pipeline中定義的計算步驟只相當于做了一個占位符,而在my_param_grid 中為每個計算步驟設置了多種方法選擇,以及對應的需要被搜索的參數(shù)、范圍、輪數(shù)。
如下示例,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()]三種,且針對這三種方法要搜索參數(shù)是'n_components(reduce_dim__n_components)'(需要注意的是,待搜索參數(shù),必須是這些方法都有的參數(shù),否則會報錯)。
my_param_grid2 與 my_param_grid1本質(zhì)完全相同,只是針對reduce_dim步驟還要搜索SelectKBest(chi2)方法的k參數(shù),因為這個參數(shù)是PCA()和NMF()所沒有的,因此要單獨拿出來搜索。同時針對classify步驟,要搜索[SVC(), LinearSVC()]兩種方法的C參數(shù)。
# 定義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對象的每個計算步驟,真正設置搜索方法和對應搜索參數(shù)、范圍、輪數(shù)。
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)的輸入?yún)?shù)名稱:n_components
如果沒有太多的超參數(shù)需要調(diào)優(yōu),并且 pipeline 運行時間不長,請使用 GridSearchCV;
對于較大的搜索空間和訓練緩慢的模型,請使用 HalvingGridSearchCV;
對于非常大的搜索空間和訓練緩慢的模型,請使用 HalvingRandomSearchCV。