我是機器學習的新手并且有一些困惑,對于這個微不足道的問題深表歉意。我有時間序列資料集,非常簡單,只有兩列 - 日期和價格。我正在預測價格并想為我的模型添加一些功能,例如過去 10 天的移動平均線。如果我拆分資料集學習:驗證 80:20。對于前 80 天,我可以計算移動平均線。我的驗證集呢?我應該使用預測值作為移動平均線的輸入嗎?是否有針對此類解決方案的現成實施?我正在使用 python scikit-learn 庫。
uj5u.com熱心網友回復:
好的,這是一個使用 GOOG 股票收盤歷史資料的 250 個資料點的解決方案。我已經用注釋解釋了代碼。請隨時詢問那里是否有模糊之處。如您所見,我使用 pandas,在該庫中有一個方便的函式“rolling”,可以計算滾動平均值等。我手動拆分資料集,但也可以通過例如 sklearn.model_selection.train_test_split 來完成
import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np
# Read data from file
df = pd.read_csv("GOOG.csv")
# Calculate 10 day rolling mean and drop first 10 rows because we cannot calculate rolling mean for them
# shift moves the averages one step ahead so day 10 gets moving average of days 0-9, etc...
df["Rolling_10d_close"] = df['Close'].rolling(10).mean().shift(1)
df = df.dropna()
# Split data into training and validation sets
training_last_row = int(len(df) * 0.8)
training_data = df.iloc[:training_last_row]
validation_data = df.iloc[training_last_row:]
# Train model on training set of data
x = training_data["Rolling_10d_close"].to_numpy().reshape(-1, 1)
y = training_data["Close"].to_numpy().reshape(-1, 1)
reg = LinearRegression().fit(x, y)
print(reg.coef_, reg.intercept_)
# prints [[0.95972717]] [4.14010503]
# Test the performance of predictions on the validation data set
x_pred = validation_data["Rolling_10d_close"].to_numpy().reshape(-1, 1)
y_pred = validation_data["Close"].to_numpy().reshape(-1, 1)
print(reg.score(x_pred, y_pred))
# prints 0.02467230502090556
uj5u.com熱心網友回復:
有趣的問題。看起來您正在創建一個自回歸模型,即根據先前的預測來預測未來值的模型。因此,您得出的結論是正確的,即在驗證集中,您需要計算預測的前 10 天移動平均值。據我所知,沒有內置功能可以做到這一點。然而,實施起來應該不會太困難。也許這樣的事情會奏效。
s = list(range(80))
predictions = []
for i in range(20):
ten_day = sum(s[-10:])/10
pred = predict(ten_day)
predictions.append(pred)
s.append(pred)
但我建議您使用谷歌自回歸模型以獲得更多見解。您也可以查看https://stats.stackexchange.com/a/346918以獲取有關如何拆分資料的一些資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/536693.html
