さくら VPS LSTM 豪ドル/円予測
さくら VPS LSTM 豪ドル/円4時間足予測ファイル
さくら VPS LSTM 豪ドル/円4時間足予測ファイル(サイレント) keras-go-lstm-4h.py
をまとめます。
RNN 豪ドル/円4時間足予測最新表示サイレント化ファイル keras-go-4h-5.py
から変更した部分は赤字にしています。
# -*- coding: utf-8 -*- #!/usr/bin/python3 import os os.environ['TF_CPP_MIN_LOG_LEVEL']='1' import datetime import numpy as np import pandas as pd from matplotlib import pyplot as plt from tensorflow import keras from mplfinance.original_flavor import candlestick_ohlc def addBusinessDays_4h(current_date, business_days_to_add_h): # current_date : 処理する初日の日時 例) 2022-02-03 00:00:00 # business_days_to_add_h : 4時間ずつ延長する回数 cur_date8 = [] # 21/03/30 12~ cur_date4 = [] # 3/30 man_f = 1 #月曜フラグ 0 or 1 doyo_f = 0 #土曜フラグ 0 or 1 current_date0 = current_date weekday0 = current_date0.weekday() if(weekday0 == 0): #月 man_f = 0 while business_days_to_add_h > 0: current_date += datetime.timedelta(hours=4) weekday = current_date.weekday() hour_doyo = current_date.hour if weekday == 6: # sunday = 6 continue if((weekday == 5) and (hour_doyo == 8)): #土 doyo_f = 1 if((weekday == 5) and (doyo_f == 1)): #土 continue if((weekday == 0) and (man_f == 1)): #月 man_f = 0 continue # 年/月/日 時刻として文字列にする処理 # 例 22/03/30 12~ e11 = current_date.strftime("%y/%m/%d %H") e22 = e11.replace("/0", "/") e22 = e22 + "~" e33 = e22[3:] # 22/ 削除 cur_date8.append(e22) cur_date4.append(e33) business_days_to_add_h -= 1 return cur_date8, cur_date4 xl_df = pd.read_csv( "/home/yamada/public_html/manep_4h/mane_chart_go_4h.csv", encoding="cp932") raw_data0 = xl_df["終値(売り)"].values raw_data = raw_data0.copy() # 行列の平均、標準偏差を求めます。 mean = np.mean(raw_data) # print("Mean", mean) raw_data -= mean std = np.std(raw_data) # print("Std ",std) # 標準偏差値に変換 raw_data /= std # print("各標準偏差値") # print(raw_data) # 連続デ-タとする。一つおきは、2 sampling_rate = 1 # 過去20間隔デ-タをひとまとまりとして時系列予測する sequence_length = 20 delay = sampling_rate * sequence_length # print("delay:", delay) batch_size = 32 # 適当 # 検証デ-タのスタ-ト値 num_half_samples = int(0.5 * len(raw_data)) train_dataset = keras.utils.timeseries_dataset_from_array( raw_data, targets=raw_data[delay:], sampling_rate=sampling_rate, sequence_length=sequence_length, batch_size=batch_size, ) val_dataset = keras.utils.timeseries_dataset_from_array( raw_data[:-1], targets=raw_data[delay:], sampling_rate=sampling_rate, sequence_length=sequence_length, batch_size=batch_size, start_index=num_half_samples, ) test_dataset = keras.utils.timeseries_dataset_from_array( raw_data, targets=None, sampling_rate=sampling_rate, sequence_length=sequence_length, batch_size=batch_size, ) # テストデ-タセット表示 i = 0 for inputs_t in test_dataset: inputs_n = inputs_t.numpy() if i == 0: # print("Start test:", inputs_n[0]) i = i + 1 # print("End test:", inputs_n[-1]) # from tensorflow import keras from keras import layers from keras import initializers """ inputs = keras.Input(shape=(sequence_length,)) x = layers.Flatten()(inputs) x = layers.Dense( 20, activation="tanh", kernel_initializer='zeros' )(x) """ inputs = keras.Input(shape=(sequence_length, 1)) x = layers.LSTM(20, kernel_initializer='zeros')(inputs) outputs = layers.Dense(1)(x) model = keras.Model(inputs, outputs) # print("モデルア-キテクチャ") # print(model.summary()) callbacks_list = [ keras.callbacks.EarlyStopping( monitor="val_loss", patience=6, ), keras.callbacks.ModelCheckpoint( "/home/yamada/public_html/colab/jena_dense_lstm_4h.keras", save_best_only=True, ) ] model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"]) history = model.fit( train_dataset, epochs=40, verbose=0, validation_data=val_dataset, callbacks=callbacks_list) loss = history.history["mae"] # 平均絶対誤差(MAE) val_loss = history.history["val_mae"] epochs = range(1, len(loss) + 1) model = keras.models.load_model( "/home/yamada/public_html/colab/jena_dense_lstm_4h.keras") # pre = model.predict(test_dataset) pre = model.predict(test_dataset, verbose=0) pre1 = np.reshape(pre, (-1)) # print(f"Test 予測値:") # print(pre1) future_test = inputs_t[-1:] # print("future_test 最初の配列値") # print(future_test) future_result = [] for i in range(6): test_data_f = np.reshape(future_test, (1, 20, 1)) # batch_predict = model.predict(test_data_f) batch_predict = model.predict(test_data_f, verbose=0) future_test = np.delete(future_test, 0) future_test = np.append(future_test, batch_predict) future_result = np.append(future_result, batch_predict) # print("future_result :") # print(future_result) # ここまで colab-21 まとめ len_raw_data = len(raw_data) xx1 = np.arange(sequence_length, len_raw_data + 1) xx3 = np.arange(len_raw_data, len_raw_data + 6) pre_chg = pre.copy() pre_chg *= std pre_chg += mean pre_chg1 = np.reshape(pre_chg, (-1)) # print("pre_chg1:") # print(pre_chg1) f_result = future_result.copy() f_result *= std f_result += mean # ここから4時間ロ-ソク足表示 Open = xl_df["始値(売り)"].values High = xl_df["高値(売り)"].values Low = xl_df["安値(売り)"].values Close = xl_df["終値(売り)"].values Date = xl_df["日付"].values Idx = xl_df.index # X 軸日付処理 xDate = [] xD = [] dayf = 0 for i, key in enumerate(Date): day00 = str(key)[11:19] if((dayf == 1) and (day00 == '00:00:00')): dayf = 0 else: if((dayf == 0) and (day00 == '00:00:00')): e4 = str(key)[4:10] e6 = e4.replace("/0", "/") e8 = e6.lstrip("/") xDate.append(e8) xD.append(i) dayf = 1 tstr = Date[-1] if(dayf == 0): e4l = tstr[7:14] e6l = e4l.replace("/0", "/") e8l = e6l.lstrip("/") xDate.append(e8l) xD.append(i) # ロ-ソク4時間足表示 ohlc = zip( Idx, Open, High, Low, Close) fig = plt.figure( figsize=(8.34, 5.56)) ax = fig.add_subplot(1,1,1) #④ ax.grid() plt.plot(xx1, pre_chg) plt.plot(xx3, f_result, 'bo') candlestick_ohlc( ax, ohlc, width=0.5, alpha = 1, colorup='r', colordown='g') plt.xticks(xD, xDate) plt.title('AUS$ / JPY chart') plt.xlabel('Date') plt.ylabel('Yen') # plt.show plt.savefig( '/home/yamada/public_html/manep-img/mane_chart_go_lstm_4h.png') # ここから4時間足予測最終日時取得 tstr = Date[-1] tdatetime = datetime.datetime.strptime(tstr, '%Y/%m/%d %H:%M:%S') lastday = addBusinessDays_4h(tdatetime, 6) lastday_pd = pd.DataFrame(lastday[0]) f_result_pd = pd.DataFrame(np.round(f_result, 3)) # 有効数字3桁に変更 df_concat = pd.concat([lastday_pd, f_result_pd], axis = 1) df_concat.to_csv( '/home/yamada/public_html/manep-img/mane_chart_go_lstm_4h.csv', header=False, index=False)
さくら VPS LSTM 豪ドル/円4時間足予測ファイル(サイレント) keras-go-lstm-4h.py
がまとまりました。
引き続き、このファイルを保存します。