Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit set_params()

I want to set parameters of SVC using set_params() as shown in the following sample code.

from sklearn.svm import SVC

params = {'C': [.1, 1, 10]}

for k, v in params.items():
    for val in v:
        clf = SVC().set_params(k=val)
        print(clf)
        print()

If I run the code, I get the following error:

ValueError: Invalid parameter k for estimator SVC

How can I put the key into set_params() correctly?

like image 502
flexup Avatar asked Oct 06 '15 08:10

flexup


2 Answers

The problem is actually how to use a string as a keyword argument. You can construct a parameter dict and pass it to set_params using the ** syntax.

from sklearn.svm import SVC

params = {'C': [.1, 1, 10]}

for k, v in params.items():
    for val in v:
        clf = SVC().set_params(**{k: val})
        print(clf)
        print()

Out:

SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)

SVC(C=1, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)

SVC(C=10, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
like image 101
yangjie Avatar answered Sep 29 '22 08:09

yangjie


While the previous answer just works fine it might be useful to also cover the case with more than one parameter. In this case sklearn also has a nice convenience function to create the parameter grid which makes it just more readable.

from sklearn.model_selection import ParameterGrid
from sklearn.svm import SVC
param_grid = ParameterGrid({'C': [.1, 1, 10], 'gamma':["auto", 0.01]})

for params in param_grid:
    svc_clf = SVC(**params)
    print (svc_clf)

Which gives a similar results:

SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0,In [235]: 
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma=0.01, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
SVC(C=1, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
SVC(C=1, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma=0.01, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
SVC(C=10, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
SVC(C=10, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma=0.01, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
like image 27
Kam Sen Avatar answered Sep 29 '22 10:09

Kam Sen