-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrealtime_prediction.py
83 lines (58 loc) · 2.12 KB
/
realtime_prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
import schedule
import time
import numpy as np
import joblib
import json
bitcoin_price_url = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD"
ticker_url = "http://127.0.0.1:5000/ticker"
# predict next hour
def predict_nexthour(data):
data = np.float64(data)
values = data.reshape(-1, 1)
data = values.astype('float32')
#from sklearn.externals import joblib
scaler = joblib.load('model/scaler_.save')
data = scaler.transform(data)
data = np.reshape(data, (data.shape[0], data.shape[1], 1))
import pickle
with open('model/model_hour_.pickle', 'rb') as file:
model = pickle.load(file)
yhat = model.predict(data)
return scaler.inverse_transform(yhat.reshape(-1, 1))
def predict_nextday(data):
data = np.float64(data)
values = data.reshape(-1, 1)
data = values.astype('float32')
#from sklearn.externals import joblib
scaler = joblib.load('model/scaler_daily.save')
data = scaler.transform(data)
data = np.reshape(data, (data.shape[0], data.shape[1], 1))
import pickle
with open('model/model_daily.pickle', 'rb') as file:
model = pickle.load(file)
yhat = model.predict(data)
return scaler.inverse_transform(yhat.reshape(-1, 1))
def get_next_hour_bitcoin_price(c):
#print("cur_price: {} pred_price: {}".format(cur_price, pred_price))
return predict_nexthour(c)[0][0]
def get_next_day_bitcoin_price(c):
return predict_nextday(c)[0][0]
def send_price():
res = requests.get(bitcoin_price_url)
cur_price = float(res.json().get("USD"))
pred_price_h = get_next_hour_bitcoin_price(cur_price)
pred_price_d = get_next_day_bitcoin_price(cur_price)
# dump json
price_pair = {}
# float32 cant be serialized -> float64
price_pair['current'] = np.float64(cur_price)
price_pair['predict_h'] = np.float64(pred_price_h)
price_pair['predict_d'] = np.float64(pred_price_d)
#out = json.dumps(price_pair)
# print(out)
requests.post(ticker_url, json=price_pair)
schedule.every(1).hours.at(":00").do(send_price)
while True:
schedule.run_pending()
time.sleep(1)