Spaces:
Runtime error
Runtime error
| import time | |
| from typing import List, Dict, Callable | |
| def summarize_predictions(pred_func: Callable, data) -> Dict: | |
| """ | |
| Exécute une fonction de prédiction sur des données et retourne | |
| les statistiques générales des prédictions. | |
| Args: | |
| pred_func: fonction de prédiction, doit accepter `data` et retourner une liste ou array de prédictions binaires (0/1) | |
| data: données sur lesquelles effectuer la prédiction | |
| Returns: | |
| dict contenant : total_samples, total_attacks, total_normal, | |
| processing_time, avg_pred_time, attack_ratio, normal_ratio | |
| """ | |
| start_time = time.time() | |
| predictions = pred_func(data) | |
| end_time = time.time() | |
| total_samples = len(predictions) | |
| total_attacks = sum(predictions) # 1 = attaque | |
| total_normal = total_samples - total_attacks | |
| processing_time = end_time - start_time | |
| avg_pred_time = processing_time / total_samples if total_samples > 0 else 0 | |
| attack_ratio = total_attacks / total_samples if total_samples > 0 else 0 | |
| normal_ratio = total_normal / total_samples if total_samples > 0 else 0 | |
| return { | |
| "total_samples": total_samples, | |
| "total_attacks": total_attacks, | |
| "total_normal": total_normal, | |
| "processing_time_sec": processing_time, | |
| "avg_pred_time_sec": avg_pred_time, | |
| "attack_ratio": attack_ratio, | |
| "normal_ratio": normal_ratio, | |
| "predictions": predictions | |
| } | |