前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用线性回归和LSTM做股价预测

用线性回归和LSTM做股价预测

作者头像
杨熹
发布2018-12-28 15:37:06
1.5K0
发布2018-12-28 15:37:06
举报
文章被收录于专栏:杨熹的专栏

本文以微软的股价为例,详细注释在代码块里:


1. 导入相关的包
代码语言:javascript
复制
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline

2. 描述性统计
代码语言:javascript
复制
df.head()
代码语言:javascript
复制
df.describe()

3. 可视化
代码语言:javascript
复制
#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
代码语言:javascript
复制
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 这一列的向右错位 forecast_out=5 个位置,多出的是 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 个数,用来预测未来的数据
    X = X[:-forecast_out]                               # X 去掉最后 forecast_out 几个数
    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                                        # 要预测未来几个时间步 
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)
代码语言:javascript
复制
forecast= learner.predict(X_lately)

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

5. LSTM
代码语言:javascript
复制
# 导入 keras 等相关包
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:,:]

# 构造 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']])
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.12.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 导入相关的包
  • 2. 描述性统计
  • 3. 可视化
  • 4.Linear Regression
  • 5. LSTM
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档