ad_engine_example.py
1 """ADEngine: Full anomaly detection lifecycle. 2 3 Demonstrates PyOD's ADEngine for automatic detector selection, 4 execution, analysis, explanation, and report generation. 5 """ 6 from pyod.utils.ad_engine import ADEngine 7 from pyod.utils.data import generate_data 8 9 # Generate sample data 10 X_train, X_test, y_train, y_test = generate_data( 11 n_train=300, n_test=100, n_features=20, contamination=0.1) 12 13 # Initialize the engine 14 engine = ADEngine() 15 16 # === Full lifecycle === 17 print("=" * 60) 18 print("FULL ANOMALY DETECTION LIFECYCLE") 19 print("=" * 60) 20 21 # 1. Profile 22 profile = engine.profile_data(X_train) 23 print("\n1. Data profile:", profile['data_type'], 24 "(%d samples, %d features)" % (profile['n_samples'], 25 profile['n_features'])) 26 27 # 2. Plan 28 plan = engine.plan_detection(profile, priority='speed') 29 print("2. Plan:", plan['detector_name'], "-", plan['reason']) 30 31 # 3. Execute 32 result = engine.run_detection(X_train, plan, X_test=X_test) 33 print("3. Detection: %d anomalies (%.1f%%) in %.3fs" 34 % (result['n_anomalies'], result['anomaly_ratio'] * 100, 35 result['runtime_seconds'])) 36 37 # 4. Analyze 38 analysis = engine.analyze_results(result, X=X_train) 39 print("4. Analysis:", analysis['summary']) 40 41 # 5. Explain 42 explanations = engine.explain_findings(result, X=X_train, top_k=3) 43 print("5. Top anomalies:") 44 for exp in explanations: 45 print(" Sample %d: score=%.4f (%s)" 46 % (exp['index'], exp['score'], exp['label'])) 47 48 # 6. Suggest next step 49 suggestion = engine.suggest_next_step(result, analysis) 50 print("6. Suggestion:", suggestion['action'], "-", suggestion['reason']) 51 52 # 7. Report 53 report = engine.generate_report(result, analysis) 54 print("\n7. Report preview (first 500 chars):") 55 print(report[:500]) 56 57 # === Knowledge queries === 58 print("\n" + "=" * 60) 59 print("KNOWLEDGE QUERIES") 60 print("=" * 60) 61 62 print("\nAvailable text detectors:") 63 for d in engine.list_detectors(data_type='text'): 64 print(" %s: %s" % (d['name'], d['full_name'])) 65 66 print("\nECOD explained:") 67 info = engine.explain_detector('ECOD') 68 print(" %s" % info['full_name']) 69 print(" Best for: %s" % info['best_for']) 70 71 print("\nADBench top 5:") 72 bench = engine.get_benchmarks('ADBench') 73 print(" %s" % bench['ADBench']['rankings']['overall_top_5'])