-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_feature_importance.py
More file actions
88 lines (68 loc) · 3.56 KB
/
Copy pathplot_feature_importance.py
File metadata and controls
88 lines (68 loc) · 3.56 KB
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
import os
import glob
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def generate_plots_per_feature(folder_path, output_root_dir="plots_features"):
"""
Iterates over each CSV, extracts each feature row by row,
and generates a horizontal barplot of its sub-column metrics.
"""
# 1. Find all CSV files in the folder
csv_files = glob.glob(os.path.join(folder_path, "*.csv"))
if not csv_files:
print(f"No CSV file found in: {folder_path}")
return
# Create the main folder to store all the plots
os.makedirs(output_root_dir, exist_ok=True)
for csv_file in csv_files:
csv_name = os.path.splitext(os.path.basename(csv_file))[0]
print(f"\n--- Processing file: {csv_name}.csv ---")
# Create a specific subfolder for this CSV file to keep things separate
csv_output_dir = os.path.join(output_root_dir, csv_name)
os.makedirs(csv_output_dir, exist_ok=True)
# Read the CSV file
df = pd.read_csv(csv_file)
name_features = df["Feature_Name"]
# 2. Loop over each row (each individual Feature)
for col in df.columns:
if col != "Feature_Name":
metrics = df[col]
feature_name = str(col).strip().replace("_Importance","")
safe_feature_name = "".join([c if c.isalnum() or c in ('_', '-') else '_' for c in feature_name])
metrics = pd.to_numeric(metrics, errors='coerce').dropna()
# Convert into a DataFrame usable by Seaborn/Matplotlib
df_metrics = pd.DataFrame({
'Metric_Name': name_features,
'Importance': metrics.values
})
df_metrics = df_metrics.drop(df_metrics[df_metrics['Importance'] == 0].index)
# Optional: Sort the bars by value for better readability
df_metrics = df_metrics.sort_values(by='Importance', ascending=False)
print(df_metrics)
# 3. Create the horizontal bar chart
# Adjust the figure size based on the number of sub-components (columns)
plt.figure(figsize=(12, max(5, len(df_metrics) * 0.25)))
sns.set_theme(style="whitegrid")
# Barplot horizontal
sns.barplot(
x='Importance',
y='Metric_Name',
data=df_metrics,
palette="coolwarm" # Gradient from blue (negative) to red (positive)
)
# Titles and labels
plt.title(f"Feature Importance for {feature_name}", fontsize=14, fontweight='bold', pad=12)
plt.xlabel("Importance Values", fontsize=11)
plt.ylabel("Feature Names", fontsize=11)
plt.tight_layout()
# 4. Save the individual PNG file
filename = f"{safe_feature_name}.png"
full_path = os.path.join(csv_output_dir, filename)
plt.savefig(full_path, dpi=150) # 150 DPI is more than enough and speeds up generation
plt.close() # Very important to free up RAM at each iteration
print(f"-> Success: {len(df)} plots generated in the folder: {csv_output_dir}")
# --- EXECUTION ---
# Replace "." with the path of your folder containing your CSV files
csv_folder_path = "/home/luciacev/Documents/SurgicalMovementPrediction-Training/feature_importance"
generate_plots_per_feature(csv_folder_path)