盘它!!一步到位,Tensorflow 2的实战 !!LSTM下的股票预测(附详尽代码及数据集)
关键词:tensorflow2、LSTM、时间序列、股票预测
Tensorflow 2.0发布已经有一段时间了,各种新API的确简单易用,除了官方文档以外能够找到的学习资料也很多,但是大都没有给出实战的部分找了好多量化分析中的博客和代码,发现在tensorflow方面大家都还是在用1.x的版本,始终没有找到关于2.x的代码,于是自己写了一段,与大家共勉。
import numpy as np import matplotlib.pyplot as plt import pandas as pd import tensorflow as tf # from tensorflow.keras import layers from sklearn.preprocessing import MinMaxScaler # Part 1 - Data Preprocessing # Importing the libraries dataset_train = pd.read_csv('NSE-TATAGLOBAL.csv') training_set = dataset_train.iloc[:, 1:2].values # print(dataset_train.head()) # Feature Scaling sc = MinMaxScaler(feature_range=(0, 1)) training_set_scaled = sc.fit_transform(training_set) # Creating a data structure with 60 timesteps and 1 output X_train = [] y_train = [] for i in range(60, 2035): X_train.append(training_set_scaled[i - 60:i, 0]) y_train.append(training_set_scaled[i, 0]) X_train, y_train = np.array(X_train), np.array(y_train) # Reshaping X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)) # Part 2 - Building the RNN # Initialising the RNN regressor = tf.keras.Sequential() # Adding the first LSTM layer and some Dropout regularisation regressor.add(tf.keras.layers.LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1))) regressor.add(tf.keras.layers.Dropout(0.2)) # Adding a second LSTM layer and some Dropout regularisation regressor.add(tf.keras.layers.LSTM(units=50, return_sequences=True)) regressor.add(tf.keras.layers.Dropout(0.2)) # Adding a third LSTM layer and some Dropout regularisation regressor.add(tf.keras.layers.LSTM(units=50, return_sequences=True)) regressor.add(tf.keras.layers.Dropout(0.2)) # Adding a fourth LSTM layer and some Dropout regularisation regressor.add(tf.keras.layers.LSTM(units=50)) regressor.add(tf.keras.layers.Dropout(0.2)) # Adding the output layer regressor.add(tf.keras.layers.Dense(units=1)) # Compiling the RNN regressor.compile(optimizer='adam', loss='mean_squared_error') # Fitting the RNN to the Training set regressor.fit(X_train, y_train, epochs=100, batch_size=32) # Part 3 - Making the predictions and visualising the results # Getting the real stock price of 2017 dataset_test = pd.read_csv('tatatest.csv') real_stock_price = dataset_test.iloc[:, 1:2].values # Getting the predicted stock price of 2017 dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis=0) inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values inputs = inputs.reshape(-1, 1) inputs = sc.transform(inputs) X_test = [] for i in range(60, 76): 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)) predicted_stock_price = regressor.predict(X_test) predicted_stock_price = sc.inverse_transform(predicted_stock_price) # Visualising the results plt.plot(real_stock_price, color='red', label='Real TATA Stock Price') plt.plot(predicted_stock_price, color='blue', label='Predicted TAT Stock Price') plt.title('TATA Stock Price Prediction') plt.xlabel('Time') plt.ylabel('TATA Stock Price') plt.legend() plt.show()
项目比较demo,但是凭借这个基本可以达到一个框架,另外我在其他随笔中也有相关的学习,欢迎大家讨论学习
使用的tata数据集是非常的难找(看了好多有代码没数据集索引),哭了,真的找了好久。
风雨兼程,前程可待!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具