GridSearch for an estimator inside a OneVsRestClassifier

后端 未结 3 440
南旧
南旧 2020-12-12 17:25

I want to perform GridSearchCV in a SVC model, but that uses the one-vs-all strategy. For the latter part, I can just do this:

model_to_set = OneVsRestClassi         


        
3条回答
  •  长情又很酷
    2020-12-12 18:01

    When you use nested estimators with grid search you can scope the parameters with __ as a separator. In this case the SVC model is stored as an attribute named estimator inside the OneVsRestClassifier model:

    from sklearn.datasets import load_iris
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.svm import SVC
    from sklearn.grid_search import GridSearchCV
    from sklearn.metrics import f1_score
    
    iris = load_iris()
    
    model_to_set = OneVsRestClassifier(SVC(kernel="poly"))
    
    parameters = {
        "estimator__C": [1,2,4,8],
        "estimator__kernel": ["poly","rbf"],
        "estimator__degree":[1, 2, 3, 4],
    }
    
    model_tunning = GridSearchCV(model_to_set, param_grid=parameters,
                                 score_func=f1_score)
    
    model_tunning.fit(iris.data, iris.target)
    
    print model_tunning.best_score_
    print model_tunning.best_params_
    

    That yields:

    0.973290762737
    {'estimator__kernel': 'poly', 'estimator__C': 1, 'estimator__degree': 2}
    

提交回复
热议问题