参考文献:株価の終値価格kerasのLSTM予測に基づきます
MinMaxScalerをインポートするときにエラーが発生します「インポート_arpackのはImportErrorから:. DLLのロードに失敗しました:指定されたプログラムを見つけることができません。」
#importを日時 のPdとしてインポートパンダ NPとしてインポートnumpyの #fromのnumpyのインポートrow_stack、column_stack TSとしてインポートtushare の#import matplotlibの PLTとしてインポートmatplotlib.pyplot #from matplotlib.pylabインポートdate2num #from matplotlib.datesインポートたDateFormatter、WeekdayLocator、DayLocator、月曜日、YEARLY #fromのmatplotlib.financeインポートquotes_historical_yahoo_ohlc、candlestick_ohlc sklearn.preprocessing輸入MinMaxScalerのから #https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#sphx-glr-auto-examples-preprocessing-plot -all-スケーリング-PY keras.modelsからシーケンシャルをインポート keras.layersからLSTM、高密度、アクティベーションをインポート DF = ts.get_hist_data( '601857'、= '2018年1月12日を' = '2016年6月15日'、始端) DD = DF [ '開く'、 '高'、 '低'、 '閉じます' ] #print(dd.values.shape [0]) DD1 = DD .sort_index() DD2 = dd1.values.flatten() DD3 = pd.DataFrame(DD1] [ '近い') DEF LOAD_DATA(DF、sequence_length = 10、スプリット= 0.8): #DF = pd.read_csv(FILE_NAME、月= ''、usecols = [1]) #data_all = np.array(DF).astype(フロート) data_all = np.array(DF) .astype(フロート) スケーラ= MinMaxScaler() data_all = scaler.fit_transform(data_all) データ= [] iに対して範囲(LEN(data_all) - sequence_length - 1)において: data.append(data_all [I:+ sequence_length + 1 ]) reshaped_data = np.array(データ).astype( 'のfloat64 ") #np.random.shuffle(reshaped_data) 統一された正規化されたxとyの#は、正規化されていない X = reshaped_dataの[し:、:-1] Y = reshaped_data [:、-1] INT = split_boundary(reshaped_data.shape [0] *スプリット) train_x X = [:split_boundary] test_x = xで[split_boundary:] train_y Y = [:split_boundary] test_y = Y [split_boundary:] 戻りtrain_x、train_y、test_x、test_y、スケーラ DEFのbuild_model(): #1 input_dim最後の次元は、入力train_xで、寸法(N_SAMPLES、time_steps、input_dim)train_x モデルシーケンシャル=を() 。(LSTM(input_dim = 1、output_dim = model.add 6、return_sequences =真。 )) #1 model.add(LSTM(6、input_dim = 1、return_sequences = TRUE)) #model.add(LSTM(6、input_shape =(なし、1)、return_sequences = TRUE))""」 #1 model.add(LSTM(input_dim = 1、output_dim = 6、input_length = 10、return_sequences = TRUE)) #model.add(LSTM(6、input_dim = 1、input_length = 10、return_sequences = TRUE)) model.add(LSTM(6、input_shape =(10、1)、return_sequences = TRUE))""」 印刷(model.layers ) #1 model.add(LSTM(100、return_sequences =真)) #のmodel.add(LSTM(100、return_sequences =真)) model.add)(LSTM(100、return_sequences = False)が model.add(高密度(output_dim = 1)) model.add(活性化( '線形')) model.compile(損失= 'MSE'、オプティマイザ= 'rmsprop') リターン・モデル デフtrain_model(train_x、train_y、test_x、test_y): モデル= build_model() してみてください: model.fit(train_x、train_y、BATCH_SIZE = 512、nb_epoch = 300、validation_split = 0.1) 予測= model.predict(test_x) 予測= np.reshape(予測、(predict.size、)) KeyboardInterrupt除い: プリント(予測) プリント(test_y) プリント(予測) 印刷(test_y) 試す: 図= plt.figure(1) plt.plot(予測、 'R') plt.plot(test_y、 'G-') plt.legend([ '予測'、 '真']) を除きますeと例外: 印刷(e)の リターンを予測、test_y 場合__name__ == '__main__': #train_x、train_y、test_x、test_y、スケーラ= LOAD_DATA( '国際航空-passengers.csv') train_x、train_y、test_x、test_y、スケーラ= LOAD_DATA(DD3、sequence_length = 10、スプリット= 0.8) train_x = np.reshape(train_x、(train_x.shape [0]、train_x.shape [1]、1)) test_x = np.reshape(test_x、(test_x.shape [0]、test_x.shape [1]、1)) predict_y、 test_y = train_model(train_x、train_y、test_x、test_y) predict_y = scaler.inverse_transform([i]はiに対するpredict_yで]) test_y = scaler.inverse_transform(test_y) FIG2 = plt.figure(2) plt.plot(predict_y、 G ':') plt.plot(test_y、 'R-') plt.show()