【问题标题】:Why I get just one coef_, when I am doing my linear regression with sklearn?当我使用 sklearn 进行线性回归时,为什么我只得到一个 coef_?
【发布时间】:2021-09-17 04:21:10
【问题描述】:

这是我的代码和下面的输出。我正在用 sklearn-lib 尝试它,女巫的作品。也许 x.reshape 是假的?

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sympy.stats import E

#My Data
y = np.array([1.88,3.65,5.86,8.43,11.47,15.98])
x = np.array([1,2,3,4,5,6])

linreg = LinearRegression()

#Grafikgrösse einstellen
x = x.reshape(-1,1)
linreg.fit(x,y)

y_pred = linreg.predict(x)

plt.scatter(x,y)
plt.plot(x,y_pred, color="red")
plt.title("'Linearer Verlauf' durch t=0")
plt.xlabel("Anzahl Umdrehungen")
plt.ylabel("Periode(s)")
plt.legend(['t1'])
plt.show()
print("r^2 =",linreg.score(x,y))
print(linreg.coef_)

【问题讨论】:

  • 您在寻找intercept_ 属性吗?
  • 你的x只有一个特征(每个y值对应一个x值),所以你的模型只能有一个coef_
  • 我想要 y = ax +b @IbrahimBerber 的 a 和 b 系数
  • 不,我计算了一下,b 应该是-1.77,a 是正确的@dm2

标签: python scikit-learn linear-regression


【解决方案1】:

公式:y = a*x + b

您的alinreg.coef_ 术语计算,它有一个值(因为只有一个术语依赖于x+b 术语由linreg.intercept_ 访问,从而得到-1.7746

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2020-08-06
    • 1970-01-01
    • 2014-07-06
    • 2020-07-08
    • 2020-07-01
    • 2018-08-11
    • 2021-07-19
    相关资源
    最近更新 更多