繁体   English   中英

Sklearn 线性回归输出

[英]Sklearn Linear Regression output

我正在尝试使用线性回归将抛物线拟合到一个简单的生成数据集中,但是无论我做什么曲线,我直接从模型中得到的结果都是难以理解的混乱。

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

#xtrain, ytrain datasets have been generated earlier

model = LinearRegression(fit_intercept = True)
model.fit(np.hstack([xtrain, xtrain**2]), ytrain)  
xfit = np.linspace(-3,3,20)  
yfit = model.predict(np.hstack([xtrain, xtrain**2]))
plt.plot(xfit, yfit)
plt.scatter(xtrain, ytrain, color="black")

此代码输出以下图形:

代码输出

但是,当我通过简单地更改以下代码行从模型生成的系数手动生成图时,我得到了我想要的结果。

手动输出

yfit = model.coef_[0]*xfit + model.coef_[1]*xfit**2 + model.intercept_

这似乎有点笨拙的处理方式,所以我想学习如何正确生成曲线。 我认为问题一定是我的数据的离散性,但我无法自己弄清楚。

这是您修复的错误:

yfit = model.predict(np.hstack([xfit, xfit**2]))

在您的代码中,您在 X 轴上绘制xfit值,而在 Y 轴上绘制f(xtrain)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM