为什么使用sklearn和sigmoid函数生成的逻辑回归模型时结果不同

时间:2019-01-18 02:28:49

标签: python scikit-learn logistic-regression

这是细节:

假设我有一个具有以下系数和截距的模型:

# Coef
w1 = 0.018056353337078567
w2 = 0.000646433629145055
w3 = 0.11595942738379618
w4 = 0.021109268199259484
w5 = 0.05204164353607967
w6 = -0.11317012710348132
w7 = -0.05215587577473489
w8 = -2.0132721508721287
intercept = -2.0132721508721287

和我的样本:

# Sample
x1 = 10
x2 = 70.05
x3 = 15
x4 = 24
x5 = 1
x6 = 2
x7 = 17
x8 = 1

当我使用sklearn logistic regression时,加载模型并调用model.predict_proba后,我得到了

[[0.21018339 0.78981661]]

但是我将这些参数放到了S型函数中,我得到了

0.0681390750219555

很明显0.78981661!= 0.0681390750219555,但我想知道为什么会发生这种情况。

以下是sigmoid函数的代码:

import numpy as np

# Coef
w1 = 0.018056353337078567
w2 = 0.000646433629145055
w3 = 0.11595942738379618
w4 = 0.021109268199259484
w5 = 0.05204164353607967
w6 = -0.11317012710348132
w7 = -0.05215587577473489
w8 = -2.0132721508721287
intercept = -2.0132721508721287
# Sample
x1 = 10
x2 = 70.05
x3 = 15
x4 = 24
x5 = 1
x6 = 2
x7 = 17
x8 = 1

z = \
    w1 * x1 + \
    w2 * x2 + \
    w3 * x3 + \
    w4 * x4 + \
    w5 * x5 + \
    w6 * x6 + \
    w7 * x7 + \
    w8 * x8 + \
    + intercept


y = 1/(1+np.exp(-z))

print(y)

1 个答案:

答案 0 :(得分:0)

内部相同。我在计算第一个时出错。