小编Vio*_*997的帖子

如何使用 scikit-learn 在 Python 中打印简单线性回归的截距和斜率?

我试图用简单的线性回归(只有一个自变量)来预测汽车价格(通过机器学习)。变量是“每加仑公路英里数”

0      27
1      27
2      26
3      30
4      22
       ..
200    28
201    25
202    23
203    27
204    25
Name: highway-mpg, Length: 205, dtype: int64
Run Code Online (Sandbox Code Playgroud)

和“价格”:

0      13495.0
1      16500.0
2      16500.0
3      13950.0
4      17450.0
        ...   
200    16845.0
201    19045.0
202    21485.0
203    22470.0
204    22625.0
Name: price, Length: 205, dtype: float64
Run Code Online (Sandbox Code Playgroud)

使用以下代码:

from sklearn.linear_model import LinearRegression

x = df["highway-mpg"]
y = df["price"]
lm = LinearRegression()

lm.fit([x],[y])
Yhat = lm.predict([x])

print(Yhat)
print(lm.intercept_)
print(lm.coef_)
Run Code Online (Sandbox Code Playgroud)

但是,截距和斜率系数打印命令给了我以下输出:

[[0. 0. …
Run Code Online (Sandbox Code Playgroud)

python machine-learning scikit-learn

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

machine-learning ×1

python ×1

scikit-learn ×1