从sklearn中的Pipeline对象返回系数

spi*_*006 15 python pipeline scikit-learn cross-validation

我适合一个Pipeline物体RandomizedSearchCV

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)
Run Code Online (Sandbox Code Playgroud)

我想访问该coef_属性,best_estimator_但我无法做到这一点.我尝试使用coef_下面的代码访问.

sgd_randomized_pipe.best_estimator_.coef_

但是我得到以下AttributeError ...

AttributeError:'Pipeline'对象没有属性'coef_'

scikit-learn文档说这coef_是属性SGDClassifier,属于我的类base_estimator_.

我究竟做错了什么?

Viv*_*mar 23

在使用named_stepsdict 创建管道时,您始终可以使用分配给它们的名称.

scaler = sgd_randomized_pipe.best_estimator_.named_steps['scl']
classifier = sgd_randomized_pipe.best_estimator_.named_steps['clf']
Run Code Online (Sandbox Code Playgroud)

然后访问所有像的属性coef_,intercept_它们是提供给对应的拟合估计等等.

这是文档中指定的Pipeline公开的形式属性:

named_steps:dict

只读属性,用于按用户名称访问任何步骤参数.键是步骤名称,值是步骤参数.


Roo*_*shi 7

我认为这应该有效:

sgd_randomized_pipe.named_steps['clf'].coef_
Run Code Online (Sandbox Code Playgroud)