2017-04-09 33 views
0

我正在運行GridSearchCV,其中OneVsRestClasssifer使用SVC作爲估算值​​。這是我PipelineGridSearchCV參數方面:GridSearchCV是用rbf內核和不同程度計算SVC嗎?

pipeline = Pipeline([ 
    ('clf', OneVsRestClassifier(SVC(verbose=True), n_jobs=1)), 
    ]) 

parameters = { 
    "clf__estimator__C": [0.1, 1], 
    "clf__estimator__kernel": ['poly', 'rbf'], 
    "clf__estimator__degree": [2, 3], 
} 

grid_search_tune = GridSearchCV(pipeline, parameters, cv=2, n_jobs=8, verbose=10) 
grid_search_tune.fit(train_x, train_y) 

根據SVC的文檔degree參數僅用於由poly內核:

http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html

度:INT,可選(默認= 3)

多項式內核的程度 函數('poly')。被所有其他內核忽略。

,但是當我看到我的GridSearchCV的輸出似乎它的計算與一個rbf內核的不同值degree參數每個SVC配置不同的運行。

[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=3 
[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=3 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=3 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=3 

當內核設置爲rbf時,不應該忽略度數的所有值嗎?

回答

1

此處顯示的輸出僅爲GridSearchCV傳遞給內部估計器的參數的不同組合,即SVC。但是否使用它們取決於SVC。在這種情況下,SVC不會拋出任何錯誤,但也不會使用degree。你應該打印你懷疑的所有組合的分數。他們應該是平等的。這會告訴你degree參數未被使用。

注意:確保設置GridSearchCVrandom_state複製測試。

說明: GridSearchCV的工作是對參數,列車數據只是傳遞給估計器,用於擬合,然後使用該測試數據進行打分,並且導致這導致最佳得分的參數的組合。

當參數的不兼容組合傳遞給估計器時,它取決於實現,參數是被忽略還是引發錯誤。

例如,在LogisticRegression,有兩個參數:

penalty : str, ‘l1’ or ‘l2’, default: ‘l2’ 
     Used to specify the norm used in the penalization. 

solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’. 
     Algorithm to use in the optimization problem. 
     ‘newton-cg’, ‘lbfgs’ and ‘sag’ only handle L2 penalty. 

正如你可以看到,如果我用l1處罰與​​解算器,它會導致不兼容。所以估計者可能會選擇忽略懲罰參數或者拋出錯誤。在這種情況下,它會引發錯誤。

+0

感謝您的回答,我必須檢查分數,但我懷疑對於每個不同的度數值,運行一個SVC(kernel ='rbf')正在計算,如果是真的,這是浪費時間,因爲所有的分數應該是相同的。 GridSearchCV應該是「聰明的」,足以放棄這些分數,我會寫關於它的sklearn郵件列表。 –

+1

@DavidBatista是的。相同的運行將針對不同的度數值進行計算。確定郵件列表。我們可以將字典更改爲只有兼容組合的字典列表。像'tuned_pa​​rameters'在:http://scikit-learn.org/stable/auto_examples/model_selection/grid_search_digits.html#sphx-glr-auto-examples-model-selection-grid-search-digits-py –

+0

啊,很好,這是一個簡單的解決方案!謝謝 :) –

相關問題