写出ARIMA和LSTM模型预测股票的代码,输入特征包括开盘价,收盘价,最低价,最高价,成交量,涨跌额,涨跌幅,输出为收盘价
时间: 2024-06-09 16:10:35 浏览: 20
ARIMA模型预测股票代码:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_model import ARIMA
# 读取数据
data = pd.read_csv('stock_data.csv', index_col='date', parse_dates=['date'])
data = data[['open', 'close', 'low', 'high', 'volume', 'change', 'change_pct']]
data.dropna(inplace=True)
# 定义ARIMA模型
model = ARIMA(data['close'], order=(5, 1, 0))
# 训练模型
results = model.fit()
# 预测未来5天的收盘价
forecast = results.forecast(steps=5)[0]
print(forecast)
```
LSTM模型预测股票代码:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
# 读取数据
data = pd.read_csv('stock_data.csv', index_col='date', parse_dates=['date'])
data = data[['open', 'close', 'low', 'high', 'volume', 'change', 'change_pct']]
data.dropna(inplace=True)
# 归一化数据
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data)
# 定义函数,用于创建LSTM模型
def create_lstm_model():
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(scaled_data.shape[1], 1)))
model.add(LSTM(units=50))
model.add(Dense(1))
return model
# 创建模型
model = create_lstm_model()
# 编译模型
model.compile(loss='mean_squared_error', optimizer='adam')
# 训练模型
model.fit(x_train, y_train, epochs=100, batch_size=32)
# 预测未来5天的收盘价
predicted_closing_price = model.predict(x_test)
predicted_closing_price = scaler.inverse_transform(predicted_closing_price)
print(predicted_closing_price)
```
相关推荐
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)