如何使用sklearn python预测未来的数据帧?

时间:2018-02-20 11:56:13

标签: python python-2.7 scikit-learn forecasting sklearn-pandas

我正在运行this链接中的示例。

我在几次修改后成功运行了代码。这是修改后的代码:

import quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
import datetime

style.use('ggplot')

df = quandl.get("WIKI/GOOGL")
df = df[['Adj. Open',  'Adj. High',  'Adj. Low',  'Adj. Close', 'Adj. Volume']]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0

df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
forecast_col = 'Adj. Close'
df.fillna(value=-99999, inplace=True)
forecast_out = int(math.ceil(0.01 * len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)

X = np.array(df.drop(['label'], 1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out]

df.dropna(inplace=True)

y = np.array(df['label'])

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2)
clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)

forecast_set = clf.predict(X_lately)
df['Forecast'] = np.nan

last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
one_day = 86400
next_unix = last_unix + one_day

for i in forecast_set:
    next_date = datetime.datetime.fromtimestamp(next_unix)
    next_unix += 86400
    df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)]+[i]

df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

但我面临的问题是预测未来的数据帧。这是输出图像:

image output

我要到2017-2018,如图所示。如何进一步进入2019年,2020年或5年后?

2 个答案:

答案 0 :(得分:3)

您的代码使用此DataFrame作为X来生成预测:

df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]

这意味着如果您想要预测未来五年的价格,您需要这些['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']数据点以便将来的价值进一步预测。

请注意,图片中的预测是根据在此处作为测试集分隔的历史数据创建的:X_lately = X[-forecast_out:]。因此,它预测的每个点都使用历史数据来预测未来的某些点。

如果您真的想要使用此模型预测未来5年,您首先需要预测/计算所有这些变量:predicted_X = ['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume'],并继续在clf.predict(predicted_X)内部运行一些循环。< / p>

我相信这Machine Learning Course for Trading at Udacity可能对你来说是一个很好的资源,它将为你提供一个更好的框架和思维方式来解决这类问题。

我希望我的回答清楚,对您有帮助,如果不是让我知道,我会澄清或回答其他问题。

按照我的说法更新您的模型:

import quandl
import numpy as np
from sklearn import preprocessing, model_selection
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
import datetime

style.use('ggplot')

df = quandl.get("WIKI/GOOGL")
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0

df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
forecast_col = 'Adj. Close'
df.fillna(value=-99999, inplace=True)
forecast_out = 1
df['label'] = df[forecast_col].shift(-forecast_out)

X = np.array(df.drop(['label'], 1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out]

df.dropna(inplace=True)

y = np.array(df['label'])

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)

# Instantiate regressors
reg_close = LinearRegression(n_jobs=-1)
reg_close.fit(X_train, y_train)

reg_hl = LinearRegression(n_jobs=-1)
reg_hl.fit(X_train, y_train)

reg_pct = LinearRegression(n_jobs=-1)
reg_pct.fit(X_train, y_train)

reg_vol = LinearRegression(n_jobs=-1)
reg_vol.fit(X_train, y_train)

# Prepare variables for loop
last_close = df['Adj. Close'][-1]
last_date = df.iloc[-1].name.timestamp()
df['Forecast'] = np.nan
predictions_arr = X_lately

for i in range(100):
    # Predict next point in time
    last_close_prediction = reg_close.predict(predictions_arr)
    last_hl_prediction = reg_hl.predict(predictions_arr)
    last_pct_prediction = reg_pct.predict(predictions_arr)
    last_vol_prediction = reg_vol.predict(predictions_arr)

    # Create np.Array of current predictions to serve as input for future predictions
    predictions_arr = np.array((last_close_prediction, last_hl_prediction, last_pct_prediction, last_vol_prediction)).T
    next_date = datetime.datetime.fromtimestamp(last_date)
    last_date += 86400

    # Outputs data into DataFrame to enable plotting
    df.loc[next_date] = [np.nan, np.nan, np.nan, np.nan, np.nan, float(last_close_prediction)]

df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

这个模型不是很有用,因为它很快就会向上爆炸,但是它的实现中有一些有趣且不寻常的东西。

为了更准确地预测未来价格,您还需要实施某种随机游走。

你也可以使用不同的模型而不是LinearRegression,例如RandomForestRegressor,这会产生非常不同的结果。

from sklearn.ensemble import RandomForestRegressor

clf_close = RandomForestRegressor(n_jobs=-1)
clf_close.fit(X_train, y_train)

clf_hl = RandomForestRegressor(n_jobs=-1)
clf_hl.fit(X_train, y_train)

clf_pct = RandomForestRegressor(n_jobs=-1)
clf_pct.fit(X_train, y_train)

clf_vol = RandomForestRegressor(n_jobs=-1)
clf_vol.fit(X_train, y_train)

不是预测价格,而是根据某些进入参数和退出参数来预测特定头寸(买入或卖出)是否有利可图。 Udacity course涵盖了这种方法。

随机漫步模型:

import quandl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import datetime
import random

style.use('ggplot')

df = quandl.get("WIKI/GOOGL")
df = df[['Adj. Close']]

df.dropna(inplace=True)

# Prepare variables for loop

last_close = df['Adj. Close'][-1]
last_date = df.iloc[-1].name.timestamp()
df['Forecast'] = np.nan

for i in range(1000):
    # Create np.Array of current predictions to serve as input for future predictions
    modifier = random.randint(-100, 105) / 10000 + 1
    last_close *= modifier
    next_date = datetime.datetime.fromtimestamp(last_date)
    last_date += 86400

    # Outputs data into DataFrame to enable plotting
    df.loc[next_date] = [np.nan, last_close]

df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

随机漫步输出图像

Random Walk Graph

答案 1 :(得分:1)

  1. 使用所有数据来估算模型(因此没有培训和测试集)
  2. 使用估算的模型预测T + 1时刻。
  3. 将T + 1时刻插回数据
  4. 回到1,直到你提前5年。
  5. 或更好,学习时间序列统计。