SVC Model - setiamanlhc/python-snippet-code GitHub Wiki

Support Vector Machine Model

Train and Predict

from sklearn.svm import SVC
model = SVC()
model.fit(X_train, y_train)
prediction = model.predict(X_test)

Print Report

from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, prediction))
print(classification_report(y_test, prediction))

Grid Search

Train

param_grid = {'C':[0.1,1, 10, 100, 1000], 'gamma':[1,0.1,0.01,0.001,0.0001], 'kernel':['rbf']}
from sklearn.model_selection import GridSearchCV
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=3)
grid.fit(X_test,y_test)

Check Parameters

grid.best_params_
grid.best_estimator_

Predict and Print report

grid_prediction = grid.predict(X_test)
print(confusion_matrix(y_test, grid_prediction))
print(classification_report(y_test, grid_prediction))