Search code examples
python-2.7machine-learningscikit-learnsvmgrid-search

Machine learning gridsearch for svm


I was doing a project where I needed to calculate the best estimator returned by gridsearch.

parameters = {'gamma':[0.1, 0.5, 1, 10, 100], 'C':[1, 5, 10, 100, 1000]}

# TODO: Initialize the classifier
svr = svm.SVC()

# TODO: Make an f1 scoring function using 'make_scorer' 
f1_scorer = make_scorer(score_func)

# TODO: Perform grid search on the classifier using the f1_scorer as the scoring method
grid_obj = grid_search.GridSearchCV(svr, parameters, scoring=f1_scorer)

# TODO: Fit the grid search object to the training data and find the optimal parameters
grid_obj = grid_obj.fit(X_train, y_train)
pred = grid_obj.predict(X_test)
def score_func():
    f1_score(y_test, pred, pos_label='yes')

# Get the estimator
clf = grid_obj.best_estimator_

I am not sure how to make the f1_scorer func since i make the prediction after creating the gridsearch object. I cannot declare f1_scorer after creating the obj because gridsearch uses it as a scoring method. Please help me how to create this scoring function for gridsearch.


Solution

  • clf = svm.SVC()
    
    # TODO: Make an f1 scoring function using 'make_scorer' 
    f1_scorer = make_scorer(f1_score,pos_label='yes')
    
    # TODO: Perform grid search on the classifier using the f1_scorer as the scoring method
    grid_obj = GridSearchCV(clf,parameters,scoring=f1_scorer)
    
    # TODO: Fit the grid search object to the training data and find the optimal parameters
    grid_obj = grid_obj.fit(X_train, y_train)
    
    # Get the estimator
    clf = grid_obj.best_estimator_