【问题标题】:Predict future values after using polynomial regression in python在 python 中使用多项式回归后预测未来值
【发布时间】:2019-03-01 03:47:21
【问题描述】:

我目前正在使用 TensorFlow 和 SkLearn 来尝试制作一个模型,该模型可以根据室外摄氏温度来预测某个产品 X 的销售额。

我将温度数据集设置为 x 变量,并将销售额设置为 y 变量。如下图所示,温度和销售额之间存在某种相关性:

首先,我尝试进行线性回归以查看它的拟合程度。这是它的代码:

from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(x_train, y_train) #fit tries to fit the x variable and y variable.

#Let's try to plot it out.
y_pred = model.predict(x_train)

plt.scatter(x_train,y_train)
plt.plot(x_train,y_pred,'r')
plt.legend(['Predicted Line', 'Observed data'])
plt.show()

这导致预测线的拟合非常差:

但是,sklearn 的一个非常好的功能是您可以尝试根据温度来预测一个值,所以如果我要写

model.predict(15)

我会得到输出

array([6949.05567873])

这正是我想要的,我只是想更好地拟合,所以我尝试使用 sklearn 进行多项式回归,方法如下:

from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=8, include_bias=False) #the bias is avoiding the need to intercept
x_new = poly.fit_transform(x_train)
new_model = LinearRegression()
new_model.fit(x_new,y_train)

#plotting
y_prediction = new_model.predict(x_new) #this actually predicts x...?
plt.scatter(x_train,y_train)
plt.plot(x_new[:,0], y_prediction, 'r')
plt.legend(['Predicted line', 'Observed data'])
plt.show()

这条线现在似乎更合适了:

我的问题不是我不能使用 new_model.predict(x) 因为它会导致 "ValueError: shapes (1,1) and (8,) not aligned: 1 (dim 1) != 8 (dim 0)"。我知道这是因为我使用的是 8 度多项式,但是有什么方法可以让我使用多项式回归模型根据 ONE 温度预测 y 轴?

【问题讨论】:

  • 你能试试new_model.predict([x for _ in range(8)])吗?
  • 如果我写在new_model.predict([[30 for x_train in range(8)]]) 我确实得到了一个输出,但输出是 array([2862.55322278]) 根据模型我希望输出超过15k,你知道我为什么得到这么低的数字吗?

标签: python tensorflow machine-learning scikit-learn


【解决方案1】:

尝试使用new_model.predict([x**a for a in range(1,9)]) 或者根据你之前使用的代码,可以new_model.predict(poly.fit_transform(x))

因为你适合一条线

y = ax^1 + bx^2 + ... + h*x^8

您需要以相同的方式转换您的输入,即将其转换为没有截距和斜率项的多项式。这就是您传递给线性回归训练函数的内容。它学习该多项式的斜率项。您显示的图仅包含您索引到的 x^1 项 (x_new[:,0]),这意味着您使用的数据有更多列。

最后一点:始终确保您的训练数据和未来/验证数据经过相同的预处理步骤,以确保您的模型正常工作。

这里有一些细节:

让我们首先在合成数据上运行您的代码。

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from numpy.random import rand

x_train = rand(1000,1)
y_train = rand(1000,1)

poly = PolynomialFeatures(degree=8, include_bias=False) #the bias is avoiding the need to intercept
x_new = poly.fit_transform(x_train)
new_model = LinearRegression()
new_model.fit(x_new,y_train)

#plotting
y_prediction = new_model.predict(x_new) #this predicts y
plt.scatter(x_train,y_train)
plt.plot(x_new[:,0], y_prediction, 'r')
plt.legend(['Predicted line', 'Observed data'])
plt.show()

现在我们可以通过将 x 值转换为没有截距的 8 次多项式来预测 y 值

print(new_model.predict(poly.fit_transform(0.25)))

[[0.47974408]]

【讨论】:

  • 我有一个问题,如果我尝试预测new_model.predict(poly.fit_transform(30)),我会得到我期望的输出:array([16963.83798785])。但是,如果我尝试用 35 度而不是 30 度进行预测,我会得到输出 array([-71530.20368012]) 我在数据集中的最高度数是 29.56,所以它无法预测正确的东西是有道理的,但为什么会下降这么多?根据目前的图表,我假设它会上涨。感谢您的帮助。
  • 一个 8 次多项式绝对会过度拟合您的数据,它会在您的数据结束后下降。尝试低次多项式并使用一些交叉验证来找出最适合您的数据的模型。查找偏差-方差权衡以开始寻找最佳模型。查找正则化方法。
猜你喜欢
  • 2016-11-10
  • 2015-07-22
  • 2022-01-03
  • 2022-11-27
  • 2019-01-24
  • 1970-01-01
  • 2019-02-01
  • 2019-07-29
  • 2018-10-19
相关资源
最近更新 更多