GridSearch for an estimator inside a OneVsRestClassifier

后端 未结 3 434
南旧
南旧 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 17:51

    For Python 3, the following code should be used

    from sklearn.datasets import load_iris
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.svm import SVC
    from sklearn.model_selection 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,
                                 scoring='f1_weighted')
    
    model_tunning.fit(iris.data, iris.target)
    
    print(model_tunning.best_score_)
    print(model_tunning.best_params_)
    
    0 讨论(0)
  • 2020-12-12 17:52
    param_grid  = {"estimator__alpha": [10**-5, 10**-3, 10**-1, 10**1, 10**2]}
    
    clf = OneVsRestClassifier(SGDClassifier(loss='log',penalty='l1'))
    
    model = GridSearchCV(clf,param_grid, scoring = 'f1_micro', cv=2,n_jobs=-1)
    
    model.fit(x_train_multilabel, y_train)
    
    0 讨论(0)
  • 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}
    
    0 讨论(0)
提交回复
热议问题