用線性回歸和LSTM做股價(jià)預(yù)測(cè)

本文以微軟的股價(jià)為例,詳細(xì)注釋在代碼塊里:


1. 導(dǎo)入相關(guān)的包

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline

2. 描述性統(tǒng)計(jì)

df.head()
df.describe()

3. 可視化

#setting figure size
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10

#for normalizing data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))

#setting index as date
df['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')
df.index = df['Date']

#plot
plt.figure(figsize=(16,8))
plt.plot(df['Close Price'], label='Close Price history')

4.Linear Regression

from sklearn import preprocessing;
from sklearn.model_selection import cross_validate
from sklearn.model_selection import train_test_split
from sklearn import linear_model;

def prepare_data(df,forecast_col,forecast_out,test_size):
    label = df[forecast_col].shift(-forecast_out);      # 建立 label,是 forecast_col 這一列的向右錯(cuò)位 forecast_out=5 個(gè)位置,多出的是 na
    X = np.array(df[[forecast_col]]);                   # X 為 是 forecast_col 這一列
    X = preprocessing.scale(X)                          # processing X
    X_lately = X[-forecast_out:]                        # X_lately 是 X 的最后 forecast_out 個(gè)數(shù),用來(lái)預(yù)測(cè)未來(lái)的數(shù)據(jù)
    X = X[:-forecast_out]                               # X 去掉最后 forecast_out 幾個(gè)數(shù)
    label.dropna(inplace=True);                         # 去掉 na values
    y = np.array(label)                                
    
    X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=test_size) 

    response = [X_train,X_test , Y_train, Y_test , X_lately];
    return response;

forecast_col = 'Close Price'                            # 選擇 close 這一列
forecast_out = 5                                        # 要預(yù)測(cè)未來(lái)幾個(gè)時(shí)間步 
test_size = 0.2;                                        # test set 的大小

X_train, X_test, Y_train, Y_test , X_lately =prepare_data(df,forecast_col,forecast_out,test_size)

model = linear_model.LinearRegression();              

model.fit(X_train,Y_train);
score = model.score(X_test,Y_test);

score        
# 0.9913674520169482

y_test_predict = learner.predict(X_test)

plt.plot(y_test_predict)
plt.plot(Y_test)
forecast= learner.predict(X_lately)

forecast
# array([112.46087852, 109.20867432, 109.46117455, 108.9258753 ,
       110.10757453])

5. LSTM

# 導(dǎo)入 keras 等相關(guān)包
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM

# 選取 date 和 close 兩列
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close Price'])
for i in range(0,len(data)):
    new_data['Date'][i] = data['Date'][i]
    new_data['Close Price'][i] = data['Close Price'][i]

# setting index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)

# 分成 train and test
dataset = new_data.values

train = dataset[0:700,:]
test = dataset[700:,:]

# 構(gòu)造 x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)

x_train, y_train = [], []
for i in range(60,len(train)):
    x_train.append(scaled_data[i-60:i,0])
    y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)

x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))

# 建立 LSTM network
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))

model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=1, verbose=2)

inputs = new_data[len(new_data) - len(test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs  = scaler.transform(inputs)

X_test = []
for i in range(60,inputs.shape[0]):
    X_test.append(inputs[i-60:i,0])
X_test = np.array(X_test)

X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))

closing_price = model.predict(X_test)
closing_price = scaler.inverse_transform(closing_price)

#for plotting
train = new_data[:700]
test = new_data[700:]
test['Predictions'] = closing_price
plt.plot(train['Close Price'])
plt.plot(test[['Close Price','Predictions']])
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容