【问题标题】:python logistic regression (beginner)python逻辑回归(初级)
【发布时间】:2016-03-09 16:14:14
【问题描述】:

我正在使用 python 教自己一些逻辑回归。我正在尝试将演练here 中的课程应用于维基百科条目here 中的小数据集。

有些事情似乎不太对劲。 Wikipedia 和 Excel Solver(使用 this video 中的方法验证)给出截距 -4.0777 和系数 1.5046,但我从 github 示例构建的代码分别输出 -0.924200 和 0.756024。

我尝试使用的代码如下。有什么明显的错误吗?

import numpy as np
import pandas as pd
from patsy import dmatrices
from sklearn.linear_model import LogisticRegression


X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
3.5,4.0,4.25,4.5,4.75,5.0,5.5]
y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]

zipped = list(zip(X,y))
df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])

y, X = dmatrices('p_or_f ~ study_hrs',
                  df, return_type="dataframe")

y = np.ravel(y)

model = LogisticRegression()
model = model.fit(X,y)
print(pd.DataFrame(np.transpose(model.coef_),X.columns))

>>>
                  0
Intercept -0.924200
study_hrs  0.756024

【问题讨论】:

    标签: python machine-learning scikit-learn logistic-regression patsy


    【解决方案1】:

    解决方案

    只需将模型创建行改为

    model = LogisticRegression(C=100000, fit_intercept=False)
    

    问题分析

    默认情况下,sklearn 解决 regularized LogisticRegression,拟合强度 C=1(小 C-大正则化,大 C-小正则化)。

    这个类使用正则化逻辑回归 liblinear 库、newton-cg 和 lbfgs 求解器。它可以同时处理 密集和稀疏的输入。使用 C 有序数组或 CSR 矩阵 包含 64 位浮点数以获得最佳性能;任何其他输入 格式将被转换(和复制)。

    因此要获得您应该适合的模型

    model = LogisticRegression(C=1000000)
    

    给了

    Intercept -2.038853 # this is actually half the intercept
    study_hrs  1.504643 # this is correct
    

    此外,问题还在于您在 patsy 中处理数据的方式,请参阅简化的正确示例

    import numpy as np
    from sklearn.linear_model import LogisticRegression
    
    X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
    3.5,4.0,4.25,4.5,4.75,5.0,5.5]
    y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
    
    X = np.array([[x] for x in X])
    y = np.ravel(y)
    
    model = LogisticRegression(C=1000000.)
    model = model.fit(X,y)
    
    print('coef', model.coef_)
    print('intercept', model.intercept_)
    

    给予

    coef [[ 1.50464059]]
    intercept [-4.07769916]
    

    究竟是什么问题?当您执行dmatrices 时,默认情况下它会将您的输入数据嵌入一列(偏差)

    X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
    3.5,4.0,4.25,4.5,4.75,5.0,5.5]
    y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
    
    zipped = list(zip(X,y))
    df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
    
    y, X = dmatrices('p_or_f ~ study_hrs',
                      df, return_type="dataframe")
    
    print(X)
    

    导致

        Intercept  study_hrs
    0           1       0.50
    1           1       0.75
    2           1       1.00
    3           1       1.25
    4           1       1.50
    5           1       1.75
    6           1       1.75
    7           1       2.00
    8           1       2.25
    9           1       2.50
    10          1       2.75
    11          1       3.00
    12          1       3.25
    13          1       3.50
    14          1       4.00
    15          1       4.25
    16          1       4.50
    17          1       4.75
    18          1       5.00
    19          1       5.50
    

    这就是为什么产生的偏差只是真实偏差的 一半 - scikit 学习还添加了一列...所以你现在有 两个偏差 ,因此最佳解决方案是给它们每个权重的一半。

    那么你能做什么呢?

    • 不要以这种方式使用 patsy
    • 禁止帕西添加偏见
    • 告诉 sklearn 不要添加偏差

    .

    import numpy as np
    import pandas as pd
    from patsy import dmatrices
    from sklearn.linear_model import LogisticRegression
    
    X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
    3.5,4.0,4.25,4.5,4.75,5.0,5.5]
    y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
    
    zipped = list(zip(X,y))
    df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
    
    y, X = dmatrices('p_or_f ~ study_hrs',
                      df, return_type="dataframe")
    
    y = np.ravel(y)
    
    model = LogisticRegression(C=100000, fit_intercept=False)
    model = model.fit(X,y)
    print(pd.DataFrame(np.transpose(model.coef_),X.columns))
    

    给予

    Intercept -4.077571
    study_hrs  1.504597
    

    根据需要

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 2011-04-14
      • 2014-12-19
      • 2018-11-05
      相关资源
      最近更新 更多