Scikit set_params()

时间:2015-10-06 08:29:48

标签: python scikit-learn

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?

3 个答案:

答案 0 :(得分:3)

问题实际上是如何使用字符串作为关键字参数。您可以使用set_params语法构造参数dict并将其传递给**

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()

输出:

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)

答案 1 :(得分:2)

虽然之前的答案运行正常但是也可以使用多个参数覆盖案例。在这种情况下,sklearn还有一个很好的便利功能来创建参数网格,使其更具可读性。

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)

这给出了类似的结果:

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)

答案 2 :(得分:0)

您可以使用许多超参数来实现

from sklearn.svm import SVC

params = {'C': [.1, 1, 10],  'gamma':["auto", 0.01],'tol':[0.001,0.003]}
for k, v in params.items():
     For val in v:
         clf = SVC().set_params(**{k: val})
         print(clf)
         print()