-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdata_visualizing.py
More file actions
347 lines (303 loc) · 11.4 KB
/
data_visualizing.py
File metadata and controls
347 lines (303 loc) · 11.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from typing import Union, Optional
from spacy import displacy
from spacy.tokens import Doc
from IPython.display import HTML, display
from data_processing import DataProcessing
class DataPlotting:
"""A class to plot data."""
def plot_class_distribution(
df: pd.DataFrame,
label_col: str = 'Prediction',
class_names: list = ['Non-Prediction', 'Prediction'],
title: str = 'Class Distribution',
save_path: Optional[str] = None,
) -> None:
"""
Parameters
----------
df : pd.DataFrame
DataFrame containing the label column.
label_col : str, default 'Prediction'
Column name holding binary class labels (0 and 1).
class_names : list, default ['Non-Prediction', 'Prediction']
Human-readable names for [class_0, class_1].
title : str, default 'Class Distribution'
Title of the bar chart.
save_path : str, optional
If provided, saves the figure to this path.
Notes
-----
General-purpose bar chart for any binary label column.
Annotates each bar with count and percentage.
Returns
-------
None
"""
counts = df[label_col].value_counts().sort_index()
total = len(df)
fig, ax = plt.subplots(figsize=(7, 5))
bars = ax.bar(
[0, 1],
counts.values,
color=['#1f77b4', '#ff7f0e'],
edgecolor='black',
width=0.5,
)
# Annotate each bar with count and percentage
for bar, count in zip(bars, counts.values):
pct = (count / total) * 100
ax.text(
bar.get_x() + bar.get_width() / 2,
bar.get_height(),
f'n={count}\n({pct:.1f}%)',
ha='center', va='bottom',
fontsize=10, fontweight='bold',
)
ax.set_xticks([0, 1])
ax.set_xticklabels(class_names)
ax.set_ylabel('Count')
ax.set_title(title)
ax.set_ylim(0, max(counts.values) * 1.30)
ax.grid(axis='y', alpha=0.3)
plt.tight_layout()
if save_path:
DataProcessing.save_to_file(
None, save_path, 'class_distribution', 'png', include_version=True
)
plt.show()
def plot_balancedness(
X: np.ndarray,
y: np.ndarray,
method_name_to_balance,
sampling_strategy: str,
classes_names: list = ['Non-Prediction', 'Prediction'],
) -> None:
"""
Parameters
----------
X : np.ndarray of shape (n_samples, n_features)
Feature matrix. Only the first two features are used for plotting.
y : np.ndarray of shape (n_samples,)
Binary labels (0 and 1).
method_name_to_balance : callable
Resampler class from imblearn (e.g., SMOTE, RandomOverSampler).
sampling_strategy : str or dict or float
Sampling strategy passed to the resampler.
classes_names : list, default ['Non-Prediction', 'Prediction']
Human-readable names for [class_0, class_1].
Notes
-----
Scatter plot of the first two features before and after resampling.
Returns
-------
None
"""
class_0_name, class_1_name = classes_names
plt.figure(figsize=(12, 5))
# --- Before resampling ---
plt.subplot(1, 2, 1)
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1],
label=f"Class 0: {class_0_name}", alpha=0.5, edgecolor='k')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1],
label=f"Class 1: {class_1_name}", alpha=0.5, edgecolor='k')
plt.title(f"Original Dataset\n(Class 0: {sum(y==0)}, Class 1: {sum(y==1)})")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.grid(alpha=0.3)
# Apply resampling
resampler = method_name_to_balance(sampling_strategy=sampling_strategy, random_state=42)
X_over, y_over = resampler.fit_resample(X, y)
# --- After resampling ---
plt.subplot(1, 2, 2)
plt.scatter(X_over[y_over == 0][:, 0], X_over[y_over == 0][:, 1],
label=f"Class 0: {class_0_name}", alpha=0.5, edgecolor='k')
plt.scatter(X_over[y_over == 1][:, 0], X_over[y_over == 1][:, 1],
label=f"Class 1: {class_1_name}", alpha=0.5, edgecolor='k')
plt.title(
f"After {method_name_to_balance.__name__}\n"
f"(Class 0: {sum(y_over==0)}, Class 1: {sum(y_over==1)})"
)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()
def plot_balancedness_before_after(
df_before: pd.DataFrame,
df_after: pd.DataFrame,
label_col: str = 'Prediction',
class_names: list = ['Non-Prediction', 'Prediction'],
feature_cols: list = ['Feature_1', 'Feature_2'],
method_name: str = 'Resampling',
) -> None:
"""
Parameters
----------
df_before : pd.DataFrame
DataFrame before resampling.
df_after : pd.DataFrame
DataFrame after resampling.
label_col : str, default 'Prediction'
Column name holding binary labels.
class_names : list, default ['Non-Prediction', 'Prediction']
Human-readable names for [class_0, class_1].
feature_cols : list, default ['Feature_1', 'Feature_2']
Two feature column names used for the scatter plot.
method_name : str, default 'Resampling'
Name of the resampling method (used in titles).
Notes
-----
2x2 grid: bar charts (top) and scatter plots (bottom),
before and after resampling.
Returns
-------
None
"""
fig = plt.figure(figsize=(14, 10))
# ---- Top row: bar charts ----------------------------------------
for col_idx, (df, stage) in enumerate(
[(df_before, 'Before'), (df_after, 'After')], start=1
):
ax = plt.subplot(2, 2, col_idx)
counts = df[label_col].value_counts().sort_index()
total = len(df)
bars = ax.bar(
[0, 1], counts.values,
color=['#1f77b4', '#ff7f0e'],
edgecolor='black', width=0.6,
)
for bar, count in zip(bars, counts.values):
pct = (count / total) * 100
ax.text(
bar.get_x() + bar.get_width() / 2,
bar.get_height(),
f'n={count}\n({pct:.1f}%)',
ha='center', va='bottom',
fontsize=10, fontweight='bold',
)
ax.set_xticks([0, 1])
ax.set_xticklabels(class_names)
ax.set_ylabel('Count')
ax.set_title(f'{stage} {method_name} – Class Distribution')
ax.set_ylim(0, max(counts.values) * 1.30)
ax.grid(axis='y', alpha=0.3)
# ---- Bottom row: scatter plots ----------------------------------
for col_idx, (df, stage) in enumerate(
[(df_before, 'Before'), (df_after, 'After')], start=3
):
ax = plt.subplot(2, 2, col_idx)
X = df[feature_cols].values
y = df[label_col].values
ax.scatter(X[y == 0][:, 0], X[y == 0][:, 1],
label=class_names[0], alpha=0.5, edgecolor='k')
ax.scatter(X[y == 1][:, 0], X[y == 1][:, 1],
label=class_names[1], alpha=0.5, edgecolor='k')
ax.set_title(f'{stage} {method_name} – Feature Space\n(n={len(df)})')
ax.set_xlabel(feature_cols[0])
ax.set_ylabel(feature_cols[1])
ax.legend()
ax.grid(alpha=0.3)
plt.tight_layout()
plt.show()
def visualize_confusion_matrix(
confusion_mat: np.ndarray,
model_name: str,
save_path: str,
class_names: list = ['Non-Prediction', 'Prediction'],
include_version: bool = False,
) -> None:
"""
Parameters
----------
confusion_mat : np.ndarray
2x2 confusion matrix.
model_name : str
Name of the model (used in title and filename).
save_path : str
Directory path to save the PNG file.
class_names : list, default ['Non-Prediction', 'Prediction']
Human-readable names for [class_0, class_1].
include_version : bool, default False
If True, appends a version suffix (-v1, -v2, …) to the filename.
Notes
-----
Saves a colored heatmap of the confusion matrix.
Returns
-------
None
"""
plt.figure(figsize=(8, 6))
sns.heatmap(
confusion_mat,
annot=True,
fmt='d',
cmap='Blues',
cbar=True,
square=True,
xticklabels=class_names,
yticklabels=class_names,
)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title(f'Confusion Matrix – {model_name}')
plt.tight_layout()
DataProcessing.save_to_file(
None,
save_path,
f'confusion_matrix_{model_name}',
'png',
include_version=include_version,
)
plt.show()
class DataVisualizing:
"""A class to visualize spaCy NLP outputs."""
def _ensure_doc(text_or_doc: Union[str, Doc], nlp) -> Doc:
"""Return a spaCy Doc – parse if string, pass through if already a Doc."""
return nlp(text_or_doc) if isinstance(text_or_doc, str) else text_or_doc
def spacy_pos_dep(sentence: Union[str, Doc], spacy_nlp_model) -> None:
"""Render the dependency parse for a sentence."""
doc = DataVisualizing._ensure_doc(sentence, spacy_nlp_model)
html = displacy.render(doc, style='dep', jupyter=False)
display(HTML(html))
def spacy_ner_ent(sentence: Union[str, Doc], spacy_nlp_model) -> None:
"""Render the named-entity visualisation for a sentence."""
doc = DataVisualizing._ensure_doc(sentence, spacy_nlp_model)
html = displacy.render(doc, style='ent', jupyter=False)
display(HTML(html))
def spacy_dep_ent(
sentence: Union[str, Doc],
spacy_nlp_model,
mode: str = 'both',
) -> None:
"""
Parameters
----------
sentence : str or Doc
Input sentence or spaCy Doc.
spacy_nlp_model : spacy.Language
Loaded spaCy model.
mode : {'pos_dep', 'ner_ent', 'both'}, default 'both'
Which visualisation to render.
Notes
-----
Thin dispatcher that calls the appropriate renderer(s).
Returns
-------
None
"""
mode = mode.lower()
if mode == 'pos_dep':
DataVisualizing.spacy_pos_dep(sentence, spacy_nlp_model)
elif mode == 'ner_ent':
DataVisualizing.spacy_ner_ent(sentence, spacy_nlp_model)
else:
# Default: render both dependency and entity visualisations
DataVisualizing.spacy_pos_dep(sentence, spacy_nlp_model)
DataVisualizing.spacy_ner_ent(sentence, spacy_nlp_model)