我已经使用 RandomizedSearchCV
拟合了一个 Pipeline
对象
pipe_sgd = Pipeline([('scl', StandardScaler()), ('clf', SGDClassifier(n_jobs=-1))])param_dist_sgd = {'clf__loss': ['log'], 'clf__penalty': [None, 'l1', 'l2', 'elasticnet'], 'clf__alpha': np.linspace(0.15, 0.35), 'clf__n_iter': [3, 5, 7]}sgd_randomized_pipe = RandomizedSearchCV(estimator = pipe_sgd, param_distributions=param_dist_sgd, cv=3, n_iter=30, n_jobs=-1)sgd_randomized_pipe.fit(X_train, y_train)
我想访问 best_estimator_
的 coef_
属性,但无法做到。我尝试使用以下代码访问 coef_
。
sgd_randomized_pipe.best_estimator_.coef_
然而,我得到了以下 AttributeError…
AttributeError: ‘Pipeline’ object has no attribute ‘coef_’
scikit-learn 文档说明 coef_
是 SGDClassifier
的属性,而 SGDClassifier
是我的 base_estimator_
的类。
我做错了什么?
回答:
你总可以使用在创建管道时分配给它们的名称,通过使用 named_steps
字典来访问它们。
scaler = sgd_randomized_pipe.best_estimator_.named_steps['scl']classifier = sgd_randomized_pipe.best_estimator_.named_steps['clf']
然后你可以访问所有可用的属性,如 coef_
、intercept_
等,这些属性对应于已拟合的估计器。
这是 Pipeline 正式暴露的属性,如文档中所指定:
named_steps : dict
只读属性,通过用户指定的名称访问任何步骤参数。键是步骤名称,值是步骤参数。