Spaces:
Running
Running
File size: 5,240 Bytes
aac75d5 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
#!/usr/bin/env python3
"""
Exemple 2 : Comparer plusieurs modèles avec différents hyperparamètres
Usage: python examples/02_compare_models.py
"""
import mlflow
import pandas as pd
from mlflow.tracking import MlflowClient
# Configuration
mlflow.set_tracking_uri("sqlite:///mlflow.db")
client = MlflowClient()
def compare_all_runs(experiment_name="Default"):
"""Compare tous les runs d'une expérience."""
print(f"📊 Comparaison de tous les runs dans '{experiment_name}'\n")
# Récupérer l'expérience
experiment = client.get_experiment_by_name(experiment_name)
if not experiment:
print(f"❌ Expérience '{experiment_name}' introuvable")
return None
# Récupérer tous les runs
runs = client.search_runs(
experiment_ids=[experiment.experiment_id], order_by=["start_time DESC"]
)
if not runs:
print(f"❌ Aucun run trouvé")
return None
print(f"✅ {len(runs)} run(s) trouvé(s)\n")
# Créer un DataFrame pour comparaison
data = []
for run in runs:
from datetime import datetime
row = {
"run_id": run.info.run_id[:8], # 8 premiers caractères
"status": run.info.status,
"start_time": datetime.fromtimestamp(run.info.start_time / 1000).strftime(
"%Y-%m-%d %H:%M"
),
}
# Ajouter les métriques
for metric_name in ["cv_f1", "test_precision", "test_recall", "test_f1"]:
row[metric_name] = run.data.metrics.get(metric_name, None)
# Ajouter quelques hyperparamètres importants
for param_name in ["clf__n_estimators", "clf__max_depth", "clf__learning_rate"]:
param_value = run.data.params.get(param_name, None)
if param_value:
try:
row[param_name] = (
float(param_value)
if "." in str(param_value)
else int(param_value)
)
except:
row[param_name] = param_value
data.append(row)
# Créer DataFrame
df = pd.DataFrame(data)
# Afficher le tableau
print("📈 Comparaison des modèles :")
print("=" * 120)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 120)
print(df.to_string(index=False))
print("=" * 120)
# Statistiques
print(f"\n📊 Statistiques :")
if "cv_f1" in df.columns:
print(f" CV F1 moyen : {df['cv_f1'].mean():.4f}")
print(f" CV F1 max : {df['cv_f1'].max():.4f}")
print(f" CV F1 min : {df['cv_f1'].min():.4f}")
print(f" Écart-type : {df['cv_f1'].std():.4f}")
# Meilleur run
if "cv_f1" in df.columns:
best_idx = df["cv_f1"].idxmax()
best_run = df.iloc[best_idx]
print(f"\n🏆 Meilleur run : {best_run['run_id']}")
print(f" CV F1 : {best_run['cv_f1']:.4f}")
return df
def plot_metrics_comparison(experiment_name="Default"):
"""Génère un graphique de comparaison (nécessite matplotlib)."""
try:
import matplotlib.pyplot as plt
experiment = client.get_experiment_by_name(experiment_name)
if not experiment:
return
runs = client.search_runs(
experiment_ids=[experiment.experiment_id], order_by=["start_time ASC"]
)
# Extraire les données
run_names = [f"Run {i + 1}" for i in range(len(runs))]
cv_f1_scores = [run.data.metrics.get("cv_f1", 0) for run in runs]
test_f1_scores = [run.data.metrics.get("test_f1", 0) for run in runs]
# Créer le graphique
fig, ax = plt.subplots(figsize=(12, 6))
x = range(len(runs))
width = 0.35
ax.bar(
[i - width / 2 for i in x], cv_f1_scores, width, label="CV F1", alpha=0.8
)
ax.bar(
[i + width / 2 for i in x],
test_f1_scores,
width,
label="Test F1",
alpha=0.8,
)
ax.set_xlabel("Runs")
ax.set_ylabel("F1 Score")
ax.set_title(f"Comparaison des F1 scores - Expérience: {experiment_name}")
ax.set_xticks(x)
ax.set_xticklabels(run_names, rotation=45)
ax.legend()
ax.grid(axis="y", alpha=0.3)
plt.tight_layout()
plt.savefig("mlflow_comparison.png", dpi=300, bbox_inches="tight")
print(f"\n📊 Graphique sauvegardé : mlflow_comparison.png")
except ImportError:
print("\n⚠️ matplotlib non installé, graphique non généré")
print(" Installation : pip install matplotlib")
if __name__ == "__main__":
# Comparer tous les runs
df = compare_all_runs("Default")
if df is not None:
# Générer un graphique
plot_metrics_comparison("Default")
print(f"\n💡 Conseils :")
print(f" - Les runs avec CV F1 élevé sont de meilleurs candidats")
print(
f" - Vérifier que test_f1 est cohérent avec cv_f1 (pas de surapprentissage)"
)
print(
f" - Favoriser les modèles avec moins de paramètres si performances similaires"
)
|