【问题标题】:Can scikit-learn's LogisticRegression() automatically normalize input data to z-scores?scikit-learn 的 LogisticRegression() 可以自动将输入数据标准化为 z 分数吗?
【发布时间】:2015-09-11 08:56:08
【问题描述】:

有没有办法让LogisticRegression() 的实例自动将提供的用于拟合/训练的数据标准化为z-scores 以构建模型? LinearRegression() 有一个 normalize=True 参数,但也许这对 LogisticRegression() 没有意义?

如果是这样,在调用predict_proba() 之前,我是否必须手动标准化未标记的输入向量(即重新计算每列的平均值、标准差)?如果模型已经执行了可能代价高昂的计算,这会很奇怪。

谢谢

【问题讨论】:

  • 按 z-score,你的意思是像 x - x.mean() / x.std() 这样的东西吗?
  • 是的,这是指“标准分数”的常用方式

标签: python scikit-learn logistic-regression


【解决方案1】:

这是你要找的吗?

from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression


X, y = make_classification(n_samples=1000, n_features=100, weights=[0.1, 0.9], random_state=0)
X.shape

# build pipe: first standardize by substracting mean and dividing std
# next do classificaiton
pipe = make_pipeline(StandardScaler(), LogisticRegression(class_weight='auto'))

# fit
pipe.fit(X, y)
# predict
pipe.predict_proba(X)

# to get back mean/std
scaler = pipe.steps[0][1]
scaler.mean_
Out[12]: array([ 0.0313, -0.0334,  0.0145, ..., -0.0247,  0.0191,  0.0439])

scaler.std_
Out[13]: array([ 1.    ,  1.0553,  0.9805, ...,  1.0033,  1.0097,  0.9884])

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 2015-04-07
    • 2014-03-31
    • 2021-07-12
    • 2018-08-12
    • 2013-10-01
    • 1970-01-01
    • 2013-01-09
    相关资源
    最近更新 更多