管道对象的超参数调整
Hyper parameter tuning on pipeline object
我有这个管道,
pl = Pipeline([
('union', FeatureUnion(
transformer_list = [
('numeric_features', Pipeline([
("selector", get_numeric_data),
])),
('text_features', Pipeline([
("selector",get_text_data),
("vectorizer", HashingVectorizer(token_pattern=TOKENS_ALPHANUMERIC,non_negative=True, norm=None, binary=False, ngram_range=(1,2))),
('dim_red', SelectKBest(chi2, chi_k))
]))
])), ("clf",LogisticRegression())
])
当我尝试做
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
c_space = np.logspace(-5, 8, 15)
param_grid = {"C": c_space,"penalty": ['l1', 'l2']}
logreg_cv = GridSearchCV(pl,param_grid=param_grid,cv=5)
logreg_cv.fit(X_train,y_train)
它让我震惊
ValueError: Invalid parameter penalty for estimator
Pipeline(memory=None,
steps=[('union', FeatureUnion(n_jobs=1,
transformer_list=[('numeric_features', Pipeline(memory=None,
steps=[('selector', FunctionTransformer(accept_sparse=False,
func= at 0x00000190ECB49488>, inv_kw_args=None,
inverse_func=None, kw_args=None, pass_y=...ty='l2', random_state=None,
solver='liblinear', tol=0.0001,
verbose=0, warm_start=False))]). Check the list of available parameters
with estimator.get_params().keys()
.
虽然 "C" 和 "penalty" 在这种情况下是合法参数。请帮我锄一下。
"C" 和 "penalty" 是 LogisticRegression 的合法参数,而不是您发送到 GridSearchCV 的管道对象。
您的管道目前有两个组件,"union"
和 "clf"
。现在管道不知道发送参数的哪一部分。您需要将这些在管道中使用的名称附加参数,以便它可以识别它们并将它们发送到正确的对象。
这样做:
param_grid = {"clf__C": c_space,"clf__penalty": ['l1', 'l2']}
注意pipeline中对象名和参数之间有两个下划线
它在 Pipeline and FeatureUnion here 的文档中提到:
Parameters of the estimators in the pipeline can be accessed using the
__ syntax
用各种例子来演示用法。
接下来,如果你想说改变 HashingVectorizer 的 ngram_range
,你会这样做:
"union__text_features__vectorizer__ngram_range" : [(1,3)]
我有这个管道,
pl = Pipeline([
('union', FeatureUnion(
transformer_list = [
('numeric_features', Pipeline([
("selector", get_numeric_data),
])),
('text_features', Pipeline([
("selector",get_text_data),
("vectorizer", HashingVectorizer(token_pattern=TOKENS_ALPHANUMERIC,non_negative=True, norm=None, binary=False, ngram_range=(1,2))),
('dim_red', SelectKBest(chi2, chi_k))
]))
])), ("clf",LogisticRegression())
])
当我尝试做
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
c_space = np.logspace(-5, 8, 15)
param_grid = {"C": c_space,"penalty": ['l1', 'l2']}
logreg_cv = GridSearchCV(pl,param_grid=param_grid,cv=5)
logreg_cv.fit(X_train,y_train)
它让我震惊
ValueError: Invalid parameter penalty for estimator Pipeline(memory=None, steps=[('union', FeatureUnion(n_jobs=1, transformer_list=[('numeric_features', Pipeline(memory=None, steps=[('selector', FunctionTransformer(accept_sparse=False, func= at 0x00000190ECB49488>, inv_kw_args=None, inverse_func=None, kw_args=None, pass_y=...ty='l2', random_state=None, solver='liblinear', tol=0.0001, verbose=0, warm_start=False))]). Check the list of available parameters with
estimator.get_params().keys()
.
虽然 "C" 和 "penalty" 在这种情况下是合法参数。请帮我锄一下。
"C" 和 "penalty" 是 LogisticRegression 的合法参数,而不是您发送到 GridSearchCV 的管道对象。
您的管道目前有两个组件,"union"
和 "clf"
。现在管道不知道发送参数的哪一部分。您需要将这些在管道中使用的名称附加参数,以便它可以识别它们并将它们发送到正确的对象。
这样做:
param_grid = {"clf__C": c_space,"clf__penalty": ['l1', 'l2']}
注意pipeline中对象名和参数之间有两个下划线
它在 Pipeline and FeatureUnion here 的文档中提到:
Parameters of the estimators in the pipeline can be accessed using the __ syntax
用各种例子来演示用法。
接下来,如果你想说改变 HashingVectorizer 的 ngram_range
,你会这样做:
"union__text_features__vectorizer__ngram_range" : [(1,3)]