简体   繁体   English

Sklearn 线性回归输出

[英]Sklearn Linear Regression output

I'm trying to fit a parabola into a simple generated dataset using linear regression, however no matter what I do the curve I get straight out of the model turns out to be an incomprehensible mess.我正在尝试使用线性回归将抛物线拟合到一个简单的生成数据集中,但是无论我做什么曲线,我直接从模型中得到的结果都是难以理解的混乱。

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")

This code outputs following graph:此代码输出以下图形:

代码输出

However, when I manually generate the plot from the coefficients that the model produces by simply changing in the following line of code, I get exactly the result I want.但是,当我通过简单地更改以下代码行从模型生成的系数手动生成图时,我得到了我想要的结果。

手动输出

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

This seems like a bit of a clunky way of going about things so I'd like to learn how to generate the curve properly.这似乎有点笨拙的处理方式,所以我想学习如何正确生成曲线。 I think the issue must be the discrete nature of my data but I haven't been able to figure it out on my own.我认为问题一定是我的数据的离散性,但我无法自己弄清楚。

Here is your bug fixed:这是您修复的错误:

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

In your code you are plotting xfit values on X-axis while f(xtrain) on Y-axis.在您的代码中,您在 X 轴上绘制xfit值,而在 Y 轴上绘制f(xtrain)

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

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