盘它!!一步到位,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数据集是非常的难找(看了好多有代码没数据集索引),哭了,真的找了好久。