關鍵詞: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數據集是非常的難找(看了好多有代碼沒數據集索引),哭了,真的找了好久。