Scikit set_params()
Scikit set_params()
我想使用 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()
我想使用 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()