Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,43 +58,39 @@ def predict_patterns(data, future_hours=24):
|
|
| 58 |
|
| 59 |
# Fonction pour rechercher un pattern similaire
|
| 60 |
def find_similar_pattern(df, prediction, future_hours=24):
|
|
|
|
| 61 |
mse_scores = {}
|
| 62 |
-
for i in range(len(df) - future_hours):
|
| 63 |
past_pattern = df["price"].iloc[i:i + future_hours].values
|
| 64 |
mse = mean_squared_error(past_pattern, prediction[:len(past_pattern)])
|
| 65 |
mse_scores[i] = mse
|
| 66 |
|
|
|
|
| 67 |
best_match_index = min(mse_scores, key=mse_scores.get)
|
| 68 |
similar_pattern = df["price"].iloc[best_match_index:best_match_index + future_hours].values
|
| 69 |
return similar_pattern
|
| 70 |
|
| 71 |
# Fonction pour générer une prédiction dynamique (Courbe verte)
|
| 72 |
def generate_dynamic_prediction(df, initial_prediction, future_hours=24):
|
| 73 |
-
green_prediction =
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
new_row = pd.DataFrame([[future_timestamp, next_prediction]], columns=['timestamp', 'price'])
|
| 92 |
-
|
| 93 |
-
# On met à jour le DataFrame en ajoutant une nouvelle ligne avec la prédiction dynamique
|
| 94 |
-
df = pd.concat([df, new_row], ignore_index=False)
|
| 95 |
|
| 96 |
-
# Mise à jour des timestamps futurs
|
| 97 |
-
future_timestamps = df.index[-future_hours:]
|
| 98 |
return green_prediction, future_timestamps
|
| 99 |
|
| 100 |
# Configuration des dates
|
|
@@ -119,7 +115,6 @@ for coin, price_data in crypto_prices.items():
|
|
| 119 |
if price_data:
|
| 120 |
df = pd.DataFrame(price_data, columns=["timestamp", "price"])
|
| 121 |
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
|
| 122 |
-
df.set_index("timestamp", inplace=True)
|
| 123 |
|
| 124 |
# Générer les prédictions initiales (jaune)
|
| 125 |
predictions, future_timestamps = predict_patterns(price_data, future_hours=24)
|
|
@@ -128,18 +123,18 @@ for coin, price_data in crypto_prices.items():
|
|
| 128 |
initial_prediction = predictions["Trend"]
|
| 129 |
|
| 130 |
# Générer la prédiction dynamique (verte)
|
| 131 |
-
green_prediction,
|
| 132 |
|
| 133 |
# Affichage des résultats
|
| 134 |
st.write(f"#### {coin} - Prix et Prédictions")
|
| 135 |
fig, ax = plt.subplots(figsize=(12, 6))
|
| 136 |
-
ax.plot(df
|
| 137 |
|
| 138 |
# Courbe de prédiction initiale (jaune)
|
| 139 |
ax.plot(future_timestamps, initial_prediction, label="Prédiction initiale (jaune)", linestyle="--", color="yellow")
|
| 140 |
|
| 141 |
# Courbe de prédiction dynamique (verte)
|
| 142 |
-
ax.plot(
|
| 143 |
|
| 144 |
ax.set_title(f"{coin} - Prix (7 derniers jours + 24h prévus)")
|
| 145 |
ax.set_xlabel("Date")
|
|
|
|
| 58 |
|
| 59 |
# Fonction pour rechercher un pattern similaire
|
| 60 |
def find_similar_pattern(df, prediction, future_hours=24):
|
| 61 |
+
# Calculer la similarité entre la courbe prédite et les patterns passés
|
| 62 |
mse_scores = {}
|
| 63 |
+
for i in range(len(df) - future_hours): # Exclure la dernière période pour éviter la correspondance avec elle-même
|
| 64 |
past_pattern = df["price"].iloc[i:i + future_hours].values
|
| 65 |
mse = mean_squared_error(past_pattern, prediction[:len(past_pattern)])
|
| 66 |
mse_scores[i] = mse
|
| 67 |
|
| 68 |
+
# Trouver l'indice du pattern le plus similaire
|
| 69 |
best_match_index = min(mse_scores, key=mse_scores.get)
|
| 70 |
similar_pattern = df["price"].iloc[best_match_index:best_match_index + future_hours].values
|
| 71 |
return similar_pattern
|
| 72 |
|
| 73 |
# Fonction pour générer une prédiction dynamique (Courbe verte)
|
| 74 |
def generate_dynamic_prediction(df, initial_prediction, future_hours=24):
|
| 75 |
+
green_prediction = []
|
| 76 |
|
| 77 |
+
# Première prédiction
|
| 78 |
+
green_prediction.extend(initial_prediction)
|
| 79 |
+
|
| 80 |
+
# Boucle pour ajuster la prédiction à chaque étape
|
| 81 |
+
for i in range(future_hours // 5): # Prédiction par segments de 5 heures
|
| 82 |
+
start_idx = i * 5
|
| 83 |
+
end_idx = (i + 1) * 5
|
| 84 |
+
|
| 85 |
+
# Prendre les 5 dernières valeurs et chercher un pattern similaire
|
| 86 |
+
similar_pattern = find_similar_pattern(df, green_prediction[start_idx:end_idx], future_hours=5)
|
| 87 |
+
|
| 88 |
+
# Ajouter le pattern similaire à la prédiction
|
| 89 |
+
green_prediction.extend(similar_pattern)
|
| 90 |
+
|
| 91 |
+
# Création des timestamps de prévision pour la courbe verte
|
| 92 |
+
future_timestamps = pd.date_range(start=df.index[-1], periods=len(green_prediction) + 1, freq='H')[1:]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
|
|
|
| 94 |
return green_prediction, future_timestamps
|
| 95 |
|
| 96 |
# Configuration des dates
|
|
|
|
| 115 |
if price_data:
|
| 116 |
df = pd.DataFrame(price_data, columns=["timestamp", "price"])
|
| 117 |
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
|
|
|
|
| 118 |
|
| 119 |
# Générer les prédictions initiales (jaune)
|
| 120 |
predictions, future_timestamps = predict_patterns(price_data, future_hours=24)
|
|
|
|
| 123 |
initial_prediction = predictions["Trend"]
|
| 124 |
|
| 125 |
# Générer la prédiction dynamique (verte)
|
| 126 |
+
green_prediction, green_future_timestamps = generate_dynamic_prediction(df, initial_prediction, future_hours=24)
|
| 127 |
|
| 128 |
# Affichage des résultats
|
| 129 |
st.write(f"#### {coin} - Prix et Prédictions")
|
| 130 |
fig, ax = plt.subplots(figsize=(12, 6))
|
| 131 |
+
ax.plot(df["timestamp"], df["price"], label="Prix réel", color="blue")
|
| 132 |
|
| 133 |
# Courbe de prédiction initiale (jaune)
|
| 134 |
ax.plot(future_timestamps, initial_prediction, label="Prédiction initiale (jaune)", linestyle="--", color="yellow")
|
| 135 |
|
| 136 |
# Courbe de prédiction dynamique (verte)
|
| 137 |
+
ax.plot(green_future_timestamps, green_prediction, label="Prédiction dynamique (verte)", linestyle="--", color="green")
|
| 138 |
|
| 139 |
ax.set_title(f"{coin} - Prix (7 derniers jours + 24h prévus)")
|
| 140 |
ax.set_xlabel("Date")
|