Skip to content

Instantly share code, notes, and snippets.

@isaacarroyov
Created January 11, 2022 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacarroyov/1f8aef2fc16bd4868946f75f9b822562 to your computer and use it in GitHub Desktop.
Save isaacarroyov/1f8aef2fc16bd4868946f75f9b822562 to your computer and use it in GitHub Desktop.
from sklearn.model_selection import StratifiedKFold, GridSearchCV
# Declare grid of hyper-parameters
dict_hyperparams = dict(svm__gamma=[0.001,0.01,0.1],
svm__C = [1,10,100,1_000])
# Cross Validation for GridSearchCV
crossVal = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
model_02 = Pipeline(steps= [
('hogdescriptor', DescriptorHOG()),
('pca', PCA(n_components=0.9, svd_solver='full')),
('scaler', StandardScaler()),
('svm', SVC(kernel='rbf'))
])
grid = GridSearchCV(
estimator= model_02, # Model
param_grid= dict_hyperparams, # hyper-parameters
cv= crossVal, # Cross Validation
n_jobs=-1, # Use all CPU's
verbose= 5 # Info
)
# Train the model
grid.fit(X_train,y_train)
# Best hyper-parameters
gamma_02 = grid.best_params_['svm__gamma']
C_02 = grid.best_params_['svm__C']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment