Spaces:
Sleeping
Sleeping
AudreyMireille
commited on
Commit
•
b6b63d1
1
Parent(s):
30b35c4
Upload 5 files
Browse files- convertcsv.csv +26 -0
- images/cryptos.jpeg +0 -0
- pages/Predictions.py +181 -0
- requirements.txt +13 -0
- utils.py +13 -0
convertcsv.csv
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name,symbol,usym,gecko
|
2 |
+
Bitcoin,BTC,₿,bitcoin
|
3 |
+
Ethereum,ETH,Ξ,ethereum
|
4 |
+
Cardano,ADA,₳,cardano
|
5 |
+
XRP,XRP,✕,ripple
|
6 |
+
Solana,SOL,◎,solana
|
7 |
+
Polkadot,DOT,●,polkadot
|
8 |
+
Dogecoin,DOGE,Ð,dogecoin
|
9 |
+
Dai,DAI,◈,dai
|
10 |
+
Litecoin,LTC,Ł,litecoin
|
11 |
+
Algorand,ALGO,Ⱥ,algorand
|
12 |
+
Bitcoin Cash,BCH,Ƀ,bitcoin-cash
|
13 |
+
ECOMI,OMI,Ο,ecomi
|
14 |
+
Internet Computer,ICP,∞,internet-computer
|
15 |
+
Ethereum Classic,ETC,ξ,ethereum-classic
|
16 |
+
Monero,XMR,ɱ,monero
|
17 |
+
Tezos,XTZ,ꜩ,tezos
|
18 |
+
Iota,MIOTA,ɨ,iota
|
19 |
+
EOS,EOS,ε,eos
|
20 |
+
Bitcoin SV,BSV,Ɓ,bitcoin-cash-sv
|
21 |
+
Maker,MKR,Μ,maker
|
22 |
+
Zcash,ZEC,ⓩ,zcash
|
23 |
+
Dash,DASH,Đ,dash
|
24 |
+
Nano,XNO,Ӿ,nano
|
25 |
+
Augur,REP,Ɍ,augur
|
26 |
+
Steem,STEEM,ȿ,steem
|
images/cryptos.jpeg
ADDED
pages/Predictions.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
import random as rn
|
5 |
+
import yfinance as yf
|
6 |
+
import streamlit as st
|
7 |
+
import datetime as dt
|
8 |
+
import plotly.graph_objects as go
|
9 |
+
from plotly.subplots import make_subplots
|
10 |
+
from sklearn.preprocessing import MinMaxScaler
|
11 |
+
|
12 |
+
np.random.seed(1)
|
13 |
+
tf.random.set_seed(1)
|
14 |
+
rn.seed(1)
|
15 |
+
from keras.models import Sequential
|
16 |
+
from keras.layers import Dense, Dropout, LSTM
|
17 |
+
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
|
18 |
+
from millify import millify
|
19 |
+
import pandas_datareader as web
|
20 |
+
from cryptocmd import CmcScraper
|
21 |
+
from datetime import datetime, timedelta
|
22 |
+
|
23 |
+
# import utils
|
24 |
+
from utils import *
|
25 |
+
import time
|
26 |
+
|
27 |
+
|
28 |
+
# récupération des données sur 10 ans
|
29 |
+
start = dt.datetime.today() - dt.timedelta(10 * 365)
|
30 |
+
end = dt.datetime.today()
|
31 |
+
|
32 |
+
a = start.strftime("%d-%m-%Y")
|
33 |
+
b = end.strftime("%d-%m-%Y")
|
34 |
+
|
35 |
+
st.write("## Prédiction du cours des cryptomonnaies")
|
36 |
+
st.write(f"### Date du jour : {b}")
|
37 |
+
|
38 |
+
csv = pd.read_csv("convertcsv.csv")
|
39 |
+
symbol = csv["symbol"].tolist()
|
40 |
+
|
41 |
+
# creating sidebar
|
42 |
+
ticker_input = st.selectbox(
|
43 |
+
"Saisir ou choisir une crypto", symbol, index=symbol.index("BTC")
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
# initialisation du scraper avec les deux dates
|
48 |
+
scraper = CmcScraper(ticker_input, a, b)
|
49 |
+
# Pandas dataFrame for the same data
|
50 |
+
df = scraper.get_dataframe()
|
51 |
+
|
52 |
+
|
53 |
+
if ticker_input:
|
54 |
+
# Enregistrement du temps de début
|
55 |
+
start_time = time.time()
|
56 |
+
st.write(f"Génération des prédictions pour : {ticker_input}...")
|
57 |
+
# tri du dataframe par dates croissantes vu que les données sont rendues par ordre décroissant
|
58 |
+
crypto_df = df.sort_values(["Date"], ascending=True, axis=0)
|
59 |
+
|
60 |
+
|
61 |
+
# création du dataframe pour les LSTM
|
62 |
+
crypto_df_lstm = pd.DataFrame(index=range(0, len(crypto_df)), columns=["Date", "Close"])
|
63 |
+
for i in range(0, len(crypto_df)):
|
64 |
+
crypto_df_lstm["Date"][i] = crypto_df["Date"][i]
|
65 |
+
crypto_df_lstm["Close"][i] = crypto_df["Close"][i]
|
66 |
+
|
67 |
+
# on fixe de la date comme index
|
68 |
+
crypto_df_lstm.index = crypto_df_lstm.Date
|
69 |
+
crypto_df_lstm.drop("Date", axis=1, inplace=True)
|
70 |
+
crypto_df_lstm = crypto_df_lstm.sort_index(ascending=True)
|
71 |
+
|
72 |
+
|
73 |
+
dataset = crypto_df_lstm.values
|
74 |
+
# division 70% (train) 20% (test)
|
75 |
+
train_index = int(0.70 * len(dataset))
|
76 |
+
|
77 |
+
# train set
|
78 |
+
train = dataset[:train_index]
|
79 |
+
# test set
|
80 |
+
valid = dataset[train_index:]
|
81 |
+
|
82 |
+
# mise à l'échelle des données
|
83 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
84 |
+
scaled_data = scaler.fit_transform(dataset)
|
85 |
+
|
86 |
+
x_train, y_train = [], []
|
87 |
+
window = 30 # un mois
|
88 |
+
for i in range(window, len(train)):
|
89 |
+
# récupération des données par bloc d'un mois (30 jours)
|
90 |
+
x_train.append(scaled_data[i - window : i, 0])
|
91 |
+
# récupération de la prochaine valeur comme étiquette
|
92 |
+
y_train.append(scaled_data[i, 0])
|
93 |
+
x_train, y_train = np.array(x_train), np.array(y_train)
|
94 |
+
|
95 |
+
# mise sous la forme (nombre d'échantillons, fenêtre de temps, nombre de features)
|
96 |
+
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
|
97 |
+
|
98 |
+
# construction du réseau LSTM par la méthode fonctionnelle
|
99 |
+
# le réseau n'est pas complexifié pour permettre une exécution rapide
|
100 |
+
model = Sequential()
|
101 |
+
model.add(LSTM(units=100, return_sequences=True, input_shape=(x_train.shape[1], 1)))
|
102 |
+
model.add(Dropout(0.2))
|
103 |
+
model.add(LSTM(units=100))
|
104 |
+
model.add(Dropout(0.2))
|
105 |
+
model.add(Dense(1))
|
106 |
+
|
107 |
+
model.compile(loss="mean_squared_error", optimizer="adam")
|
108 |
+
print("Entrainement du modèle")
|
109 |
+
model.fit(x_train, y_train, epochs=1, batch_size=32,validation_split=0.3)
|
110 |
+
|
111 |
+
|
112 |
+
# récupération de toutes les valeurs du test set (valid) et des window valeurs avant elles, ce qui explique le -window
|
113 |
+
inputs = crypto_df_lstm[len(crypto_df_lstm) - len(valid) - window :].values
|
114 |
+
inputs = inputs.reshape(-1, 1)
|
115 |
+
inputs = scaler.transform(inputs)
|
116 |
+
|
117 |
+
X_test = []
|
118 |
+
for i in range(window, inputs.shape[0]):
|
119 |
+
X_test.append(inputs[i - window : i, 0])
|
120 |
+
X_test = np.array(X_test)
|
121 |
+
|
122 |
+
# transformation du test set à un format approprié
|
123 |
+
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
|
124 |
+
closing_price = model.predict(X_test)
|
125 |
+
closing_price = scaler.inverse_transform(closing_price)
|
126 |
+
|
127 |
+
# for plotting
|
128 |
+
train = crypto_df[:train_index]
|
129 |
+
valid = crypto_df[train_index:]
|
130 |
+
valid["Predictions"] = closing_price
|
131 |
+
|
132 |
+
fig_preds = go.Figure()
|
133 |
+
fig_preds.add_trace(
|
134 |
+
go.Scatter(x=train["Date"], y=train["Close"], name="Train Set")
|
135 |
+
)
|
136 |
+
|
137 |
+
fig_preds.add_trace(
|
138 |
+
go.Scatter(x=valid["Date"], y=valid["Close"], name="Test Set")
|
139 |
+
)
|
140 |
+
|
141 |
+
fig_preds.add_trace(
|
142 |
+
go.Scatter(x=valid["Date"], y=valid["Predictions"], name="Prédictions")
|
143 |
+
)
|
144 |
+
|
145 |
+
fig_preds.update_layout(
|
146 |
+
legend=dict(orientation="h", yanchor="bottom", y=1, xanchor="left", x=0),
|
147 |
+
height=600,
|
148 |
+
title_text=f"Valeurs actuelles VS Valeurs prédites pour {ticker_input}",
|
149 |
+
template="gridon",
|
150 |
+
)
|
151 |
+
|
152 |
+
st.plotly_chart(fig_preds, use_container_width=True)
|
153 |
+
|
154 |
+
# Prédiction au jour suivant
|
155 |
+
real_data = [inputs[len(inputs) - window : len(inputs + 1), 0]]
|
156 |
+
real_data = np.array(real_data)
|
157 |
+
real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1], 1))
|
158 |
+
|
159 |
+
prediction = model.predict(real_data)
|
160 |
+
prediction = scaler.inverse_transform(prediction)
|
161 |
+
# calcul des métriques
|
162 |
+
perfs = performances(valid["Close"],valid["Predictions"])
|
163 |
+
end_time = time.time()
|
164 |
+
|
165 |
+
# Convertir la dernière en objet datetime
|
166 |
+
date_obj = dt.datetime.strptime(b, "%d-%m-%Y")
|
167 |
+
|
168 |
+
# Ajouter un jour
|
169 |
+
nouvelle_date = date_obj + dt.timedelta(days=1)
|
170 |
+
|
171 |
+
# Convertir la nouvelle date en format souhaité
|
172 |
+
nouvelle_date_str = nouvelle_date.strftime("%d-%m-%Y")
|
173 |
+
|
174 |
+
with st.container():
|
175 |
+
col_1, col_2, col_3,col_4 = st.columns(4)
|
176 |
+
col_1.metric(f"Préd. jour **{nouvelle_date_str}** :",f"{str(round(float(prediction), 2))}")
|
177 |
+
col_2.metric(f"R2 Score :", f"{perfs[0]} %")
|
178 |
+
col_3.metric(f"RMSE :",f"{perfs[1]}")
|
179 |
+
col_4.metric(f"Temps mis : ",f"{taken_time(start_time,end_time)}")
|
180 |
+
else:
|
181 |
+
st.write("Aucune crypto sélectionnée.")
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
finta==1.3
|
2 |
+
keras==2.9.0
|
3 |
+
millify==0.1.1
|
4 |
+
numpy==1.21.2
|
5 |
+
pandas==1.3.4
|
6 |
+
plotly==5.1.0
|
7 |
+
scikit_learn==1.1.1
|
8 |
+
streamlit==1.10.0
|
9 |
+
ta==0.9.0
|
10 |
+
tensorflow==2.9.1
|
11 |
+
yfinance==0.1.63
|
12 |
+
cryptocmd==0.6.1
|
13 |
+
pandas_datareader==0.10.0
|
utils.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
|
4 |
+
|
5 |
+
# fonction d'affichage des performances
|
6 |
+
def performances(y_pred,y_true):
|
7 |
+
r2 = round(r2_score(y_pred,y_true)*100,2)
|
8 |
+
rmse=round(np.sqrt(np.mean(np.power((np.array(y_pred)-y_true),2))),2)
|
9 |
+
return r2, rmse
|
10 |
+
|
11 |
+
|
12 |
+
def taken_time(start_time, end_time):
|
13 |
+
return(f"{round((end_time-start_time)/60,2)} min.")
|