Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,38 +58,25 @@ 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 |
-
# 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):
|
| 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 |
-
|
|
|
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
future_timestamps = [last_timestamp + timedelta(hours=i) for i in range(1, future_hours + 1)]
|
| 80 |
-
|
| 81 |
-
# Boucle pour ajuster la prédiction à chaque étape
|
| 82 |
-
for i in range(future_hours // 5): # Prédiction par segments de 5 heures
|
| 83 |
-
start_idx = i * 5
|
| 84 |
-
end_idx = (i + 1) * 5
|
| 85 |
-
|
| 86 |
-
# Prendre les 5 dernières valeurs et chercher un pattern similaire
|
| 87 |
-
similar_pattern = find_similar_pattern(df, green_prediction[start_idx:end_idx], future_hours=5)
|
| 88 |
-
|
| 89 |
-
# Ajouter le pattern similaire à la prédiction
|
| 90 |
-
green_prediction.extend(similar_pattern)
|
| 91 |
|
| 92 |
-
#
|
| 93 |
green_prediction = green_prediction[:len(future_timestamps)]
|
| 94 |
|
| 95 |
return green_prediction, future_timestamps
|
|
|
|
| 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 |
+
# Prendre la prédiction initiale comme base
|
| 74 |
+
green_prediction = initial_prediction
|
| 75 |
|
| 76 |
+
# Assurer que la longueur des deux correspondantes
|
| 77 |
+
future_timestamps = pd.date_range(df.index[-1], periods=future_hours+1, freq='H')[1:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Si la longueur des prédictions dépasse la taille de future_timestamps, ajuster
|
| 80 |
green_prediction = green_prediction[:len(future_timestamps)]
|
| 81 |
|
| 82 |
return green_prediction, future_timestamps
|