用英文描述一下我的LSTM架构,每一层是什么# 从yahoo读取股票数据 from pandasdatareader.data import DataReader import…
- User
用英文描述一下我的LSTM架构,每一层是什么# 从yahoo读取股票数据 from pandas_datareader.data import DataReader import yfinance as yf import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import keras.models import tensorflow
from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.layers import Dropout sns.set_style('whitegrid') plt.style.use('fivethirtyeight') %matplotlib inline
时间戳
from datetime import datetime
设置股票名称
techList = ['GOOG', 'MSFT', 'AMZN']
设置起始时间 取过去一年的数据
end = datetime.now() start = datetime(end.year - 1, end.month, end.day)
下载数据
for stock in techList: globals()[stock] = yf.download(stock, start, end) companyList = [GOOG, MSFT, AMZN] companyName = ['GOOGLE', 'MICROSOFT', 'AMAZON'] for company, comName in zip(companyList, companyName): company['Company Name'] = comName df = pd.concat(companyList, axis=0) df.tail(10)
plt.figure(figsize=(15, 5)) plt.subplots_adjust(top=1.25, bottom=1.2)
for i, company in enumerate(companyList, 1): plt.subplot(1, 3, i) company['Adj Close'].plot() plt.ylabel('Adj Close') plt.xlabel(None) plt.title(f"Closing Price of {techList[i-1]})")
plt.tight_layout()
交易量趋势图
plt.figure(figsize=(15, 5)) plt.subplots_adjust(top=1.25, bottom=1.2)
for i, company in enumerate(companyList, 1): plt.subplot(1, 3, i) company['Volume'].plot() plt.ylabel('Volume') plt.xlabel(None) plt.title(f"Sales Volume of {techList[i-1]}")
plt.tight_layout()
移动平均线
maDay = [10, 20, 50]
for ma in maDay: for company in companyList: column_name = f"MA for {ma} days" company[column_name] = company['Adj Close'].rolling(ma).mean()
fig, axes = plt.subplots(nrows=1, ncols=3) fig.set_figheight(5) fig.set_figwidth(15)
GOOG[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[0]) axes[0].set_title('GOOGLE')
MSFT[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[1]) axes[1].set_title('MICROSOFT')
AMZN[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[2]) axes[2].set_title('AMAZON')
fig.tight_layout()
日均收益率
for company in companyList: company['Daily Return'] = company['Adj Close'].pct_change()
fig, axes = plt.subplots(nrows=1, ncols=3) fig.set_figheight(5) fig.set_figwidth(15)
GOOG['Daily Return'].plot(ax=axes[0], legend=True, linestyle='--', marker='o') axes[0].set_title('GOOGLE')
MSFT['Daily Return'].plot(ax=axes[1], legend=True, linestyle='--', marker='o') axes[1].set_title('MICROSOFT')
AMZN['Daily Return'].plot(ax=axes[2], legend=True, linestyle='--', marker='o') axes[2].set_title('AMAZON')
- Assistant
My LSTM architecture consists of two LSTM layers with 64 and 32 units respectively, followed by two dense layers with 25 and 1 units. The input shape for the LSTM layers is (60,1), as each sequence is composed of 60 previous time steps. The optimizer is set to 'adam' and the loss function is 'mean_squared_error'. The model is trained on a batch size of 1 and for 1 epoch. The data is first normalized with MinMaxScaler and then split into a training set (95%) and a testing set (5%). The training set is used to fit the model, while the testing set is used to evaluate the performance of the model. The root mean squared error (RMSE) is used to evaluate the performance of the model, with a lower RMSE indicating better performance. The final output of the model is a prediction of the next day's closing price of a given stock. The prediction is then plotted alongside the actual closing price to visualize the performance of the model.