标准化sklearn中的回归量

时间:2018-02-22 11:26:24

标签: python-3.x scikit-learn regression

我正在使用sklearn,我想知道如何正确使用StandardScaler()。我构建了一个函数,允许在Ridge和Lasso回归之间切换,并获取alpha值,回归量X和预测变量Y.所有回归量都应该标准化。

from sklearn.linear_model import Ridge, Lasso
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler() # Standardize regressors by removing the mean and scaling to unit variance

def do_penalized_regression(X, y, penalty, type):
    if type == "ridge":
        lm = Ridge(alpha = penalty, normalize=False)
    elif type == "lasso":
        lm = Lasso(alpha = penalty, normalize=False)
lm.scaler.fit(X,y)
return lm

这是要走的路还是我应该提前标准化回归量?

1 个答案:

答案 0 :(得分:1)

您可以使用sklearn.pipeline.make_pipeline

from sklearn.linear_model import Ridge, Lasso
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline


model = make_pipeline(StandardScaler(), lm)

model.fit(X, y)
...