gpt4 book ai didi

python - 如何使用 sklearn python 预测 future 的数据帧?

转载 作者:行者123 更新时间:2023-12-05 09:17:14 25 4
gpt4 key购买 nike

我正在运行来自 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()

但我面临的问题是对 future 数据框的预测。这是输出图像:

image output

我一直到 2017-2018 年,如图所示。如何进一步走向2019年、2020年或5年后?

最佳答案

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

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

这意味着如果您想预测 future 五年的价格,您将需要这些 ['Adj.关闭','HL_PCT','PCT_change','调整。 Volume'] future 值的数据点,以保持更远的预测。

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

如果你真的想使用这个模型来预测 future 5 年,你首先需要预测/计算所有这些变量:predicted_X = ['Adj.关闭','HL_PCT','PCT_change','调整。 Volume'],并继续使用 clf.predict(predicted_X) 运行一些循环。

我相信这个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()

这个模型不是很有用,因为它很快向上爆炸,但是在它的实现中有一些有趣和不寻常的事情。

为了更真实地预测 future 价格,您还需要实现某种随机游走。

您也可以使用不同的模型代替 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

关于python - 如何使用 sklearn python 预测 future 的数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com