Skip to content

Instantly share code, notes, and snippets.

@jamespaultg
Created March 16, 2018 07:12
Show Gist options
  • Save jamespaultg/a1b121ed3d4418f65af0973d07ff92c7 to your computer and use it in GitHub Desktop.
Save jamespaultg/a1b121ed3d4418f65af0973d07ff92c7 to your computer and use it in GitHub Desktop.
Linear SVC grid search in Python
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
SVCpipe = Pipeline([('scale', StandardScaler()),
('SVC',LinearSVC())])
# Gridsearch to determine the value of C
param_grid = {'SVC__C':np.arange(0.01,100,10)}
linearSVC = GridSearchCV(SVCpipe,param_grid,cv=5,return_train_score=True)
linearSVC.fit(X_train,y_train)
print(linearSVC.best_params_)
#linearSVC.coef_
#linearSVC.intercept_
bestlinearSVC = linearSVC.best_estimator_
bestlinearSVC.fit(X_train,y_train)
bestlinearSVC.coef_ = bestlinearSVC.named_steps['SVC'].coef_
bestlinearSVC.score(X_train,y_train)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment