Machine Learning-python用LSTM模型進行時間序列預測1(LSTM to do time series forecasting using python keras)
這篇實作LSTM多對一的模型,顧名思義就是用過去多筆資料去預測未來一筆。 整個處理流程可分為 1.讀取資料 2.正規化資料(較會收斂) 3.切割訓練集 4.打亂資料順序(訓練結果可更好) 5.建模型 6.最佳化並評估訓練結果 以下直接用程式範例說明 import pandas as pd import numpy as np from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten, LSTM, TimeDistributed, RepeatVector import mysql.connector from keras import optimizers from keras.callbacks import EarlyStopping #讀資料 xls_file = pd.ExcelFile('A.xlsx') df = xls_file.parse('sheet1') data=df #正規化 def normalize(train): train_norm = train.apply(lambda x: (x - np.mean(x)) / (np.max(x) - np.min(x))) return train_norm data=normalize(data) #切訓練集,24筆去預測未來1筆,Y_train就是你要預測的參數。 def buildTrain(train, pastDay, futureDay): X_train, Y_train = [], [] for i in range(train.shape[0]-futureDay-pastDay): X_train.append(np.array(train.iloc[i:i+pastDay])) Y_train.append(np.array(train.iloc[i+pastDay:i+pastDay+futureDay]["goal"])) retur...