Repository navigation
Expand file tree
/
Copy pathfix_plotting_error.py
More file actions
192 lines (158 loc) Β· 6.83 KB
/
Copy pathfix_plotting_error.py
File metadata and controls
192 lines (158 loc) Β· 6.83 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
Fix for ColumnNotFoundError in plotting
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def fix_plotting_error(df_train, target_column=None):
"""
Fix the plotting error by checking columns and handling target variable.
Args:
df_train: Your DataFrame (could be Polars or Pandas)
target_column: Name of the target column (if known)
Returns:
df_pandas: DataFrame ready for plotting
actual_target: Correct target column name
"""
# Convert Polars to Pandas if needed (for seaborn compatibility)
if hasattr(df_train, 'to_pandas'):
print("π Converting Polars DataFrame to Pandas for plotting...")
df_pandas = df_train.to_pandas()
else:
df_pandas = df_train.copy()
# Check available columns
print("π Available columns:")
for i, col in enumerate(df_pandas.columns):
print(f" {i}: {col}")
# Find the target column
if target_column and target_column in df_pandas.columns:
actual_target = target_column
print(f"β
Target column '{actual_target}' found")
else:
# Try common target column names
possible_targets = ['label', 'target', 'y', 'class', 'outcome']
actual_target = None
for possible in possible_targets:
if possible in df_pandas.columns:
actual_target = possible
print(f"β
Found target column: '{actual_target}'")
break
if actual_target is None:
# Use the last column as target (common ML convention)
actual_target = df_pandas.columns[-1]
print(f"β οΈ No standard target column found. Using last column: '{actual_target}'")
return df_pandas, actual_target
def create_safe_plots(df_pandas, target_column):
"""
Create plots safely with proper error handling.
Args:
df_pandas: Pandas DataFrame
target_column: Name of the target column
"""
print(f"π Creating plots for target column: '{target_column}'")
# Check if target column is numeric for appropriate plots
is_numeric = pd.api.types.is_numeric_dtype(df_pandas[target_column])
print(f"π Target column is numeric: {is_numeric}")
# Create figure
fig, ax = plt.subplots(2, 2, figsize=(15, 10))
ax = ax.flatten()
try:
# Plot 1: Box plot
if is_numeric:
sns.boxplot(x=df_pandas[target_column], ax=ax[0])
ax[0].set_title(f'Box Plot: {target_column}')
else:
# For categorical targets, use count plot
sns.countplot(data=df_pandas, x=target_column, ax=ax[0])
ax[0].set_title(f'Count Plot: {target_column}')
ax[0].tick_params(axis='x', rotation=45)
# Plot 2: Histogram/Distribution
if is_numeric:
sns.histplot(data=df_pandas, x=target_column, kde=True, ax=ax[1])
ax[1].set_title(f'Distribution: {target_column}')
else:
# For categorical, show value counts
value_counts = df_pandas[target_column].value_counts()
ax[1].bar(range(len(value_counts)), value_counts.values)
ax[1].set_xticks(range(len(value_counts)))
ax[1].set_xticklabels(value_counts.index, rotation=45)
ax[1].set_title(f'Value Counts: {target_column}')
# Plot 3: Statistics summary
ax[2].axis('off')
if is_numeric:
stats_text = f"""
Statistics for {target_column}:
Mean: {df_pandas[target_column].mean():.4f}
Median: {df_pandas[target_column].median():.4f}
Std: {df_pandas[target_column].std():.4f}
Min: {df_pandas[target_column].min():.4f}
Max: {df_pandas[target_column].max():.4f}
Missing: {df_pandas[target_column].isnull().sum()}
"""
else:
unique_vals = df_pandas[target_column].nunique()
most_common = df_pandas[target_column].mode()[0] if len(df_pandas[target_column].mode()) > 0 else "N/A"
stats_text = f"""
Statistics for {target_column}:
Unique values: {unique_vals}
Most common: {most_common}
Missing: {df_pandas[target_column].isnull().sum()}
Total records: {len(df_pandas)}
"""
ax[2].text(0.1, 0.5, stats_text, fontsize=12, verticalalignment='center')
ax[2].set_title('Statistics Summary')
# Plot 4: Missing values pattern (if any)
missing_count = df_pandas.isnull().sum().sum()
if missing_count > 0:
missing_by_col = df_pandas.isnull().sum()
missing_by_col = missing_by_col[missing_by_col > 0]
if len(missing_by_col) > 0:
ax[3].bar(range(len(missing_by_col)), missing_by_col.values)
ax[3].set_xticks(range(len(missing_by_col)))
ax[3].set_xticklabels(missing_by_col.index, rotation=45)
ax[3].set_title('Missing Values by Column')
ax[3].set_ylabel('Missing Count')
else:
ax[3].axis('off')
ax[3].text(0.5, 0.5, 'No Missing Values', ha='center', va='center')
else:
ax[3].axis('off')
ax[3].text(0.5, 0.5, 'No Missing Values', ha='center', va='center')
plt.tight_layout()
plt.show()
print("β
Plots created successfully!")
except Exception as e:
print(f"β Error creating plots: {e}")
print("π DataFrame info:")
print(f" Shape: {df_pandas.shape}")
print(f" Columns: {list(df_pandas.columns)}")
print(f" Target column type: {df_pandas[target_column].dtype}")
# Quick fix function for immediate use
def quick_fix_and_plot(df_train, target=None):
"""
Quick fix for the plotting error - use this directly.
Usage:
quick_fix_and_plot(df_train)
# or specify target:
quick_fix_and_plot(df_train, target='your_column_name')
"""
print("π§ Quick Fix for Plotting Error")
print("=" * 40)
# Fix the DataFrame and find target
df_fixed, actual_target = fix_plotting_error(df_train, target)
# Create the plots
create_safe_plots(df_fixed, actual_target)
return df_fixed, actual_target
if __name__ == "__main__":
# Example usage
print("π§ͺ Example fix for plotting error")
print("Replace 'df_train' and 'target' with your actual variables:")
print()
print("# Your original code that caused the error:")
print("# sns.boxplot(x=df_train[target], ax=ax[0])")
print()
print("# Fixed version:")
print("from fix_plotting_error import quick_fix_and_plot")
print("df_fixed, actual_target = quick_fix_and_plot(df_train)")