-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_predict.py
63 lines (53 loc) · 2.28 KB
/
coin_predict.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
import warnings
import pandas as pd
from joblib import dump, load
import json
import requests
from datetime import datetime
import coin_preproc as cp
warnings.filterwarnings("ignore")
def req(url):
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
return pd.DataFrame(json.loads(response.text))
def saveData(coins, y_true, y_pred,ACCURACY, PRECISION):
coins_json = json.dumps(coins.to_list())
y_true_json = json.dumps(y_true.to_dict(orient='list'))
y_pred_json = json.dumps(y_pred.to_dict(orient='list'))
js_text = f"const coins = {coins_json}, y_true={y_true_json}, y_pred={y_pred_json},N={N},M={M},R={R},DATE='{datetime.now()}',ACCURACY={ACCURACY},PRECISION={PRECISION};"
with open("web/coins/data.js", 'w') as file:
file.write(js_text)
def update_prediction(model,coins,TYPE,N,M,R):
TP,TN,FP,FN=0,0,0,0
COUNT = 200
total = []
y_pred=pd.DataFrame({coin: [-1] * COUNT for coin in coins})
y_true=pd.DataFrame({coin: [-1] * COUNT for coin in coins})
for coin in coins:
df_coin = req(f"https://api.upbit.com/v1/candles/{TYPE}?market={coin}&count={COUNT}")
X = cp.make_X2(cp.make_X1(df_coin), N)
trues = cp.make_y(df_coin["change_rate"].reset_index(drop=True)[:-N]+1, M,R)
y_true[coin][:len(trues)] = trues
predictions = model.predict(X)
y_pred[coin][:len(predictions)] = predictions
total.append(X.loc[0:0])
val_pred,val_true = y_pred[coin][M:len(predictions)],y_true[coin][M:len(predictions)]
TP += ((val_pred == 1)&(val_true == 1)).sum()
TN += ((val_pred == 0)&(val_true == 0)).sum()
FP += ((val_pred == 1)&(val_true == 0)).sum()
FN += ((val_pred == 0)&(val_true == 1)).sum()
df_total = pd.concat(total, axis=0,ignore_index=True)
df_total['market'] = coins
print("TP:",TP,"TN:",TN,"FP:",FP,"FN:",FN)
ACCURACY=(TP+TN)/(TP+TN+FP+FN)
PRECISION=-1 if TP+FP == 0 else (TP)/(TP+FP)
print("정확도:",ACCURACY)
print("정밀도:",PRECISION)
saveData(coins, y_true, y_pred,ACCURACY, PRECISION)
if __name__ == "__main__":
N=15
M=40
R=1.7
model = load("extratree_15_40_17.pkl")
coins=pd.read_csv("coin/target_coins.csv")["market"]
update_prediction(model, coins, "days",N,M,R)