| import numpy as np |
| import matplotlib.pyplot as plt |
| import os |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.metrics import roc_auc_score |
|
|
| |
| OUTPUT_DIR = "ensemble_results" |
| if not os.path.exists(OUTPUT_DIR): |
| os.makedirs(OUTPUT_DIR) |
|
|
| def main(): |
| print("π Starting Hybrid Ensemble Verification...") |
|
|
| |
| q_result_path = "qsvc_results/qsvc_results.npz" |
| if not os.path.exists(q_result_path): |
| print("β Error: QSVC results not found. Run 'run_qsvc_m1_final.py' first.") |
| return |
| |
| q_data = np.load(q_result_path) |
| q_preds = q_data['preds'] |
| y_test_q = q_data['targets'] |
| |
| print(f" β
Loaded QSVC Predictions (AUC: {roc_auc_score(y_test_q, q_preds):.4f})") |
|
|
| |
| |
| possible_paths = ['vG.0.1/qgan_data_optimized.npz', 'qgan_data_optimized.npz'] |
| data_path = next((p for p in possible_paths if os.path.exists(p)), None) |
| |
| if not data_path: |
| print("β Error: qgan_data_optimized.npz not found.") |
| return |
|
|
| data = np.load(data_path) |
| X_train_full = data['X_train'] |
| y_train_full = data['y_train'] |
| X_test_full = data['X_test'] |
| y_test_full = data['y_test'] |
|
|
| |
| |
| print(" π² Training Random Forest (Classical Baseline)...") |
| rf = RandomForestClassifier(n_estimators=100, random_state=42) |
| rf.fit(X_train_full, y_train_full) |
| |
| |
| |
| X_test_sub = X_test_full[:len(q_preds)] |
| y_test_sub = y_test_full[:len(q_preds)] |
| |
| |
| if not np.allclose(y_test_q, y_test_sub): |
| print("β οΈ Warning: Target mismatch! The test sets might not align.") |
| print(" Using QSVC targets as truth.") |
| |
| c_preds = rf.predict_proba(X_test_sub)[:, 1] |
| c_auc = roc_auc_score(y_test_sub, c_preds) |
| print(f" β
Random Forest Predictions (AUC: {c_auc:.4f})") |
|
|
| |
| |
| |
| correlation = np.corrcoef(c_preds, q_preds)[0, 1] |
| print(f" π Prediction Correlation: {correlation:.4f}") |
| if correlation < 0.8: |
| print(" β¨ GREAT! Models are thinking differently.") |
| else: |
| print(" β οΈ Models are very similar.") |
|
|
| |
| |
| print("\n π€ Calculating Ensemble Scores...") |
| best_auc = 0 |
| best_w = 0 |
| results = [] |
| |
| |
| for w in np.arange(0.0, 1.1, 0.1): |
| |
| ensemble_preds = (w * c_preds) + ((1 - w) * q_preds) |
| score = roc_auc_score(y_test_sub, ensemble_preds) |
| results.append(score) |
| |
| print(f" Weight {w:.1f} Classical / {1-w:.1f} Quantum -> AUC: {score:.5f}") |
| |
| if score > best_auc: |
| best_auc = score |
| best_w = w |
|
|
| |
| print("\n" + "="*40) |
| print("π HYBRID ENSEMBLE RESULTS") |
| print("="*40) |
| print(f"π² Classical Best: {c_auc:.4f}") |
| print(f"βοΈ Quantum Best: {roc_auc_score(y_test_q, q_preds):.4f}") |
| print("-" * 30) |
| print(f"π€ Hybrid Best: {best_auc:.4f} (w={best_w:.1f})") |
| print("="*40) |
|
|
| if best_auc > c_auc: |
| print("π SUCCESS: Quantum Intelligence improved the result!") |
| print(f" Gain: +{best_auc - c_auc:.4f}") |
| else: |
| print("π Result: No gain. Classical model dominated.") |
|
|
| |
| plt.figure(figsize=(8, 6)) |
| plt.plot(np.arange(0.0, 1.1, 0.1), results, marker='o') |
| plt.axhline(y=c_auc, color='r', linestyle='--', label='Classical Baseline') |
| plt.title("Ensemble Performance: Mixing Classical & Quantum") |
| plt.xlabel("Weight (Classical)") |
| plt.ylabel("AUC Score") |
| plt.legend() |
| plt.grid(True) |
| plt.savefig(f"{OUTPUT_DIR}/ensemble_sweep.png") |
| print(f" πΈ Saved sweep plot to {OUTPUT_DIR}/ensemble_sweep.png") |
|
|
| if __name__ == "__main__": |
| main() |