David
David

Reputation: 433

Build a grid search for tuning hyperparameters

I need to build a grid search for tuning hyperparameters. Supose a two ranges: lambda = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] and sigma = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]. How can I build a custom grid search? I have created a custom SVM class, called CustomSVM(), with fit(), predict() and score() methods. I would like to make a search grid on that class to see which parameters are the best suited.

I have thought

for x in lambda:
   for y in sigma: 
      ...

but I am not sure how to coninue.

Upvotes: 0

Views: 445

Answers (1)

JAV
JAV

Reputation: 675

Sklearn has a grid search class, could you use this?

from sklearn.model_selection import GridSearchCV

parameters = {
            'lambda': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1], 
            'sigma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]}

svm_gs = GridSearchCV(customSVM, parameters)
svm_gs.fit(your.data, your.target)

I don't see why your for loop solution wouldn't work as well; you can reference the GridSearchCV docs for ideas.

Upvotes: 1

Related Questions