Heatmap

Model Feature Importance

Comparing feature importance across ML models

Output
Model Feature Importance
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Features and models
features = ['Age', 'Income', 'Education', 'Location', 'Tenure', 
            'Usage', 'Support_Calls', 'Payment_Delay', 'Contract', 'Satisfaction']
models = ['Random Forest', 'XGBoost', 'LightGBM', 'CatBoost', 'Neural Net']

# Generate importance scores
importance = np.random.rand(len(features), len(models))
# Add structure - some features more important across models
importance[0, :] += 0.3  # Age important
importance[5, :] += 0.25  # Usage important
importance[7, :] += 0.2  # Payment_Delay important
importance = importance / importance.sum(axis=0)  # Normalize

df = pd.DataFrame(importance, index=features, columns=models)

# NEON cyan-magenta colormap
neon_cm = LinearSegmentedColormap.from_list('neon_cm', ['#ffffff', '#27D3F5', '#4927F5', '#F5276C'])

fig, ax = plt.subplots(figsize=(9, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

sns.heatmap(df, cmap=neon_cm, annot=True, fmt='.1%', linewidths=2, linecolor='#ffffff',
            annot_kws={'size': 9, 'fontweight': '500'},
            cbar_kws={'shrink': 0.8, 'label': 'Importance'}, ax=ax)

ax.set_title('Feature Importance Comparison', color='#1f2937', fontsize=14, fontweight='bold', pad=15)
ax.set_xlabel('Model', color='#1f2937', fontsize=11)
ax.set_ylabel('Feature', color='#1f2937', fontsize=11)
ax.tick_params(colors='#374151', labelsize=10)
ax.set_xticklabels(ax.get_xticklabels(), rotation=30, ha='right')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support