【问题标题】:True out-of-sample forecasting of time series in PythonPython中时间序列的真实样本外预测
【发布时间】:2020-08-11 02:38:24
【问题描述】:

我正在尝试在 Python 中执行真正的样本外预测。我已经研究了几天,但没有运气。

我遇到了下面显示的用于股票价格预测的示例代码,我试图对其进行修改以预测由热化学过程(时间序列问题)引起的温度变化。据我了解,示例代码将历史数据集(例如 100 个数据点)移动 'n' 天,然后将剩余的数据点分成两组进行训练(80%)和测试(20%)然后它继续预测/估计预定“n”天的股票价值。

是否可以修改此代码以预测历史数据集之外的真实样本外因变量?

感谢您的帮助。

from pandas_datareader import data
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

df = data.DataReader('FB', 'yahoo', start= '2015-01-01', end='2020-04-27')

df = df[['Close']]

print (df.tail())

# variable for predicting 'n' days out in the future
forecast = 1

# create another column called prediction that is shifted n days out
df['predicted'] = df[['Close']].shift(-forecast)

# Convert the dataframe to numpy array
X = np.array(df.drop(['predicted'],1))

# Remove the last n rows
X = X[:-forecast]

# Create the dependent dataset 
y = np.array(df['predicted'])

# Get all the y values except the last n rows
y = y[:-forecast]

# Split data into %training and %testing
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

# Create and train the linear regression model
lr = LinearRegression()
lr.fit(x_train, y_train)

# Testing the model using score (returns the coefficient of determination R^2)
lr_score = lr.score(x_test, y_test)

# Create x_forecast equals to the last n rows of the original dataset from the close column
x_forecast = np.array(df.drop(['predicted'],1))[-forecast:]

lr_prediction = lr.predict(x_forecast)

print (lr_score)

print (lr_prediction)

【问题讨论】:

  • 问题有点不清楚。第 101 个因变量是什么意思?
  • 我刚刚修改了问题。我需要代码来预测历史数据集中最后一天后一天的因变量值。

标签: python


【解决方案1】:

不太清楚你在问什么。运行您提供的代码,它只是创建一个线性方程,根据前几天的值预测下一个值。

lr                         #Linear equation that was calculated from the data. 
input  = X[0:10]           #Input is 10 different points. 
output = lr.predict(input) #Output is the 10 points that are predicted from the input. 

我相信您提供的代码已经在进行您正在寻找的“第二天预测”。

如果您要预测热量类型数据,请确保拟合指数函数,因为线性函数可能不太准确。

【讨论】:

  • 感谢有关指数函数的提示。如果将“预测”的值增加到 10,代码将预测 10 天后的值,这是没有意义的。我需要它来预测未来 10 天(或 n 天)的值。
  • 什么?这不是真的,只需打印 df['Close'][:10] 和 df['predicted'][:10]。您可以清楚地看到输入零(例如)是 78.449997,输出是 77.190002,这也是“NEXT”天的值,而不是前几天。
  • 好的。那么在这种情况下,如何获取代码来预测 2020 年 5 月 27 日的股票价值?
  • 请注意,值 X[-1] 是 4 月 24 日。您可以在 print(df['Close'][-5:]) 中看到这一点。让我们通过 lr 运行一些值,print(x[-5:]);打印(lr.predict(X[-5:]))。您可以看到 X 的输入和这些 X 的预测值。最后一个值为 2020 年 4 月 27 日。
【解决方案2】:

基本上,这是一个普通的机器学习任务,称为线性回归,其中一个函数(线性、二次,并不重要)适合数据集。在机器学习任务中,您试图预测 examplelabel。一个例子是一条数据,例子的features是你知道的数据点的属性,例子的标签是你要预测的数据的属性。 样本外预测已得到很好的解释here,但在机器学习术语中,您将模型拟合到您称为训练集的数据分区(样本内预测)。然后,您通过预测数据的另一个分区(称为测试集)(样本外预测)的标签来测试模型的泛化能力。当然,重要的是您的模型没有在测试集上进行训练,否则您的样本外泛化结果将有偏差并且人为地良好。

鉴于这些机器学习术语,您应该能够按照here 或任意数量的在线博客文章中所述执行简单的线性回归。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2020-08-12
    • 2017-05-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多