def read_data(): from assets import models df = pd.read_csv('assets/data.csv') for index, _df in df.iterrows(): date = datetime.datetime.strptime(_df['date'],'%Y/%m/%d').date() row = models.Data(date=date, subscribers=_df['subscribers'],reviews=_df['reviews']) db_session.add(row) db_session.commit()
def read_data(): #初期データの読込(csv) from assets import models #from assets.database import db_session import pandas as pd import datetime df = pd.read_csv('japanese-stock-price.csv') for index, _df in df.iterrows(): date = datetime.datetime.strptime(_df['date'],'%Y/%m/%d').date() row = models.Data(date = date, subscribers = _df['subscribers'], reviews = _df['reviews']) db_session.add(row) db_session.commit()
def write_data(): from assets import models # 2019/02/01~の情報を取得(UTCとの時差は9時間)(close_timeは日本時間で午前9時) res = requests.get('https://api.cryptowat.ch/markets/bitflyer/btcjpy/ohlc?periods=86400&after=1548979200') data = json.loads(res.text) # 取得した情報をデータフレームに格納 daily_data = pd.DataFrame(data['result']['86400'], columns=['close_time', 'open_price', 'high_price', 'low_price', 'close_price', 'volume', 'quote_volume']) daily_data['date'] = pd.to_datetime(daily_data['close_time'], unit='s') daily_data = daily_data[:-1] # 最終行を削除(取得した時点の情報が次の日の情報となっているため) # 日本時間に変更 diff_JST_from_UTC = 9 daily_data['date'] = daily_data['date'] + datetime.timedelta(hours=diff_JST_from_UTC) # 過去相場の予測結果をDBに格納するために以下で学習済みモデルへの入力データを作成 period = 5 data = daily_data['close_price'] # 各データをリストに格納するためにリストを作成 input_tensor = [] label_tensor = [] mean_list = [] std_list = [] data_len = len(data) #総データ数 #変数とラベルの生成 for i in range(0, data_len - period, 1): window = data.values[i:i + period] mean = np.mean(window) std = np.std(window) mean_list.append(mean) std_list.append(std) input_tensor.append((window-mean) / std) label_tensor.append((data.values[i + period]-mean) / std) input_tensor = np.array(input_tensor).reshape(len(data)-period, period, 1) label_tensor = np.array(label_tensor).reshape(len(data)-period, 1) mean_list = np.array(mean_list) std_list = np.array(std_list) # label_tensorの最初はdaily_data['date']の5列目からなので日付データを整形 date = daily_data['date'][5:].reset_index(drop=True) # 学習済みモデルを取得 with open("lstm_model.pickle", "rb") as f: model = pickle.load(f) # 学習済みモデルで予測 pred = model.predict(input_tensor) pred = pred.reshape(-1) pred = pred*std_list + mean_list label_tensor = label_tensor.reshape(-1) real = label_tensor*std_list + mean_list past_df = pd.DataFrame({'date':date, 'pred':pred, 'real':real}) # 過去情報をDBに格納 for index, _past_df in past_df.iterrows(): row = models.Data(date = _past_df['date'], real_price = _past_df['real'], pred_price = _past_df['pred']) db_session.add(row) db_session.commit() # デプロイした日の翌日の相場を予測し、DBに格納するコードを記述 # windowを作成し標準化 window = 5 data_five = db_session.query(models.Data).all()[-window:] input_will = [] for price in data_five: input_will.append(price.real_price) input_will = np.array(input_will).reshape(1, window, 1) input_mean = np.mean(input_will) input_std = np.std(input_will) sta_input = (input_will - input_mean) / input_std # 翌日の価格を予測、標準化してあるので元に戻す future_price = model.predict(sta_input) future_price = future_price.reshape(-1) future_price = future_price*input_std + input_mean future_price = float(future_price) # 翌日の日付を取得 today = db_session.query(models.Data).all()[-1].date next_day = today + datetime.timedelta(days=1) # 翌日の日付、予測した価格をデータベースに格納 row = models.Data(date = next_day, real_price = None, pred_price = future_price) db_session.add(row) db_session.commit()