# Erstelle nur das farbcodierte Stabilitätsdiagramm für die Top-5 Features import matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Patch # Anzahl der anzuzeigenden Features (Top-N mit höchster Varianz) num_features_to_show = 5 # Top-5 Features # Sortiere die Features nach Varianz (absteigend) sorted_features = sorted(feature_variances.items(), key=lambda x: x[1], reverse=True) # Beschränke auf die Top-N Features sorted_features = sorted_features[:num_features_to_show] features = [item[0] for item in sorted_features] variances = [item[1] for item in sorted_features] # Definiere Schwellenwerte für die Farbkodierung low_threshold = 0.0001 medium_threshold = 0.001 # Farbkodierung basierend auf Varianzwerten colors = [] for v in variances: if v < low_threshold: colors.append('green') # Niedrige Varianz - sehr stabil elif v < medium_threshold: colors.append('orange') # Mittlere Varianz - mäßig stabil else: colors.append('red') # Hohe Varianz - instabil # Erstelle das Balkendiagramm mit Farbkodierung plt.figure(figsize=(10, 6)) bars = plt.barh(features, variances, color=colors) # Füge Werte am Ende der Balken hinzu for i, v in enumerate(variances): plt.text(v + 0.00001, i, f"{v:.6f}", va='center') # Füge eine Legende hinzu legend_elements = [ Patch(facecolor='green', label='Sehr stabil (< 0.0001)'), Patch(facecolor='orange', label='Mäßig stabil (< 0.001)'), Patch(facecolor='red', label='Instabil (≥ 0.001)') ] plt.legend(handles=legend_elements, loc='lower right') plt.xlabel('Varianz') plt.title('Stabilität der Top-5 LIME-Features') plt.grid(axis='x', linestyle='--', alpha=0.7) plt.tight_layout() # Speichere die Abbildung plt.savefig('output/Lime_Varianz_Features.png', dpi=300) plt.show()