绘制逻辑回归线

时间:2021-01-31 16:22:39

标签: arrays numpy matplotlib linear-regression logistic-regression

这是我在这里发表的第一篇文章,所以我不确定提问的正确形式是什么。我试图把结果的图片,但自从我第一次发帖以来,该网站告诉我,我需要 10 个正面帖子才能获得一些可信度,所以我认为我的图表没有出现。另外,我是法国人,不是完美的双语。请放纵一下,我愿意接受所有意见和建议。我真的需要这个用于我主人的项目。非常感谢!

我有两组数组,其中包含数千个值,其中一个 (x_1_3) 是温度的所有值,而 y_0_100 仅包含与 x_1_3 中排序的每个温度相关联的 0 和 100。

    x_1_3 = array([[ 2.02],
   [ 2.01],
   [ 3.08],
   ...,
   [ 0.16],
   [ 0.17],
   [-2.12]])

y_0_100 = array([  0.,   0.,   0., ..., 100., 100., 100.])

y_0_100 中的 0 代表固体沉淀,100 代表液体沉淀我只想在我的值上绘制逻辑回归线

(我也尝试将值放入数据框中,但没有用)

     dfsnow_rain
AirTemp liquid%
0   2.02    0.0
1   2.01    0.0
2   3.08    0.0
3   3.05    0.0
4   4.89    0.0
... ... ...
7526    0.78    100.0
7527    0.40    100.0
7528    0.16    100.0
7529    0.17    100.0
7530    -2.12   100.0
7531 rows × 2 columns



X = x_1_3
y = y_0_100
# Fit the classifier
clf = linear_model.LogisticRegression(C=1e5)
clf.fit(X, y)

# and plot the result
plt.figure(1, figsize=(10, 5))
plt.clf()
plt.scatter(X.ravel(), y, color='black', zorder=20)
X_test = np.linspace(-15, 15, 300)

loss = expit(X_test * clf.coef_ + clf.intercept_).ravel()
plt.plot(X_test, loss, color='red', linewidth=3)

ols = linear_model.LinearRegression()
ols.fit(X, y)
plt.plot(X_test, ols.coef_ * X_test + ols.intercept_, linewidth=1)
#plt.axhline(1, color='.5')

plt.ylabel('y')
plt.xlabel('X')
plt.xticks(range(-10, 10))
plt.yticks([0, 100, 10])
plt.ylim(0, 100)
plt.xlim(-10, 10)
plt.legend(('Logistic Regression Model', 'Linear Regression Model'),
           loc="lower right", fontsize='small')
plt.tight_layout()
plt.show()

Chart results

当我放大时,我发现我的逻辑回归线并不平坦,它是在很小范围内弯曲的线(见下图)

Chart when it's zoomed

我想要更像这样的东西:

Logistic regression chart i would like

我在这里做错了什么?我只想在从 y0 到 y100 的值之间绘制一条回归线

0 个答案:

没有答案