我想使用set_params()设置SVC的参数,如下面的示例代码所示。
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()
如果我运行代码,我得到以下错误:
ValueError: Invalid parameter k for estimator SVC
如何将关键字放入set_params()正确?
问题实际上是如何使用字符串作为关键字参数。您可以构造一个参数字典,并使用**
语法将其传递给set_params
。
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)
虽然前面的答案很好,但使用多个参数来涵盖这种情况可能会很有用。在这种情况下,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)
可以使用多个超参数
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()