Repository navigation
Expand file tree
/
Copy pathanalysis_advanced.py
More file actions
346 lines (294 loc) · 10.6 KB
/
Copy pathanalysis_advanced.py
File metadata and controls
346 lines (294 loc) · 10.6 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
"""Advanced data analysis utilities.
This module extends the basic profiling in `analysis.py` with:
- Outlier detection (IQR and Z-score methods)
- Correlation analysis with significance filtering
- Time series decomposition and trend detection
- Distribution classification (normal, skewed, uniform, etc.)
- Automated anomaly flagging across columns
"""
from __future__ import annotations
import math
from dataclasses import dataclass
from typing import Any
import numpy as np
import pandas as pd
# ---------------------------------------------------------------------------
# Outlier Detection
# ---------------------------------------------------------------------------
@dataclass
class OutlierReport:
"""Results from outlier detection on a single numeric column."""
column: str
method: str
total_rows: int
outlier_count: int
outlier_pct: float
lower_bound: float | None
upper_bound: float | None
outlier_indices: list[int]
def detect_outliers_iqr(
df: pd.DataFrame,
column: str,
factor: float = 1.5,
) -> OutlierReport:
"""Detect outliers using the Inter-Quartile Range method.
Values below Q1 - factor*IQR or above Q3 + factor*IQR are flagged.
"""
series = pd.to_numeric(df[column], errors="coerce").dropna()
q1 = float(series.quantile(0.25))
q3 = float(series.quantile(0.75))
iqr = q3 - q1
lower = q1 - factor * iqr
upper = q3 + factor * iqr
mask = (series < lower) | (series > upper)
indices = series[mask].index.tolist()
return OutlierReport(
column=column,
method="iqr",
total_rows=len(series),
outlier_count=len(indices),
outlier_pct=round(len(indices) / max(len(series), 1) * 100, 2),
lower_bound=lower,
upper_bound=upper,
outlier_indices=indices,
)
def detect_outliers_zscore(
df: pd.DataFrame,
column: str,
threshold: float = 3.0,
) -> OutlierReport:
"""Detect outliers using the Z-score method.
Values with |z| > threshold are flagged.
"""
series = pd.to_numeric(df[column], errors="coerce").dropna()
mean = float(series.mean())
std = float(series.std())
if std == 0:
return OutlierReport(
column=column, method="zscore", total_rows=len(series),
outlier_count=0, outlier_pct=0.0, lower_bound=mean, upper_bound=mean,
outlier_indices=[],
)
z = ((series - mean) / std).abs()
mask = z > threshold
indices = series[mask].index.tolist()
return OutlierReport(
column=column,
method="zscore",
total_rows=len(series),
outlier_count=len(indices),
outlier_pct=round(len(indices) / max(len(series), 1) * 100, 2),
lower_bound=mean - threshold * std,
upper_bound=mean + threshold * std,
outlier_indices=indices,
)
def detect_all_outliers(
df: pd.DataFrame,
method: str = "iqr",
**kwargs: Any,
) -> list[OutlierReport]:
"""Run outlier detection on every numeric column in the DataFrame."""
fn = detect_outliers_iqr if method == "iqr" else detect_outliers_zscore
reports = []
for col in df.select_dtypes(include="number").columns:
reports.append(fn(df, col, **kwargs))
return reports
# ---------------------------------------------------------------------------
# Correlation Analysis
# ---------------------------------------------------------------------------
@dataclass
class CorrelationResult:
"""Filtered correlation pairs above a significance threshold."""
pairs: list[dict[str, Any]]
method: str
threshold: float
def correlation_analysis(
df: pd.DataFrame,
method: str = "pearson",
threshold: float = 0.5,
) -> CorrelationResult:
"""Compute pairwise correlations and return pairs above the threshold.
method: 'pearson', 'spearman', or 'kendall'
threshold: minimum absolute correlation to include (0-1)
"""
numeric = df.select_dtypes(include="number")
if numeric.shape[1] < 2:
return CorrelationResult(pairs=[], method=method, threshold=threshold)
corr = numeric.corr(method=method)
pairs: list[dict[str, Any]] = []
seen: set = set()
for i, col_a in enumerate(corr.columns):
for j, col_b in enumerate(corr.columns):
if i >= j:
continue
val = float(corr.iloc[i, j])
if abs(val) >= threshold and not math.isnan(val):
key = tuple(sorted([col_a, col_b]))
if key not in seen:
seen.add(key)
pairs.append({
"column_a": col_a,
"column_b": col_b,
"correlation": round(val, 4),
"strength": _strength_label(abs(val)),
})
pairs.sort(key=lambda p: abs(p["correlation"]), reverse=True)
return CorrelationResult(pairs=pairs, method=method, threshold=threshold)
def _strength_label(r: float) -> str:
if r >= 0.9:
return "very_strong"
if r >= 0.7:
return "strong"
if r >= 0.5:
return "moderate"
if r >= 0.3:
return "weak"
return "negligible"
# ---------------------------------------------------------------------------
# Time Series Helpers
# ---------------------------------------------------------------------------
@dataclass
class TimeSeriesSummary:
"""Summary statistics for a time-indexed numeric series."""
date_column: str
value_column: str
start: str
end: str
count: int
trend_direction: str # "increasing", "decreasing", "flat"
trend_slope: float
mean: float
std: float
monthly_counts: dict[str, int]
def time_series_summary(
df: pd.DataFrame,
date_column: str,
value_column: str,
) -> TimeSeriesSummary:
"""Compute a lightweight time series summary with trend detection.
Uses a simple linear regression on ordinal dates to estimate trend.
"""
tmp = df[[date_column, value_column]].copy()
tmp[date_column] = pd.to_datetime(tmp[date_column], errors="coerce")
tmp = tmp.dropna()
if tmp.empty:
return TimeSeriesSummary(
date_column=date_column, value_column=value_column,
start="", end="", count=0, trend_direction="flat",
trend_slope=0.0, mean=0.0, std=0.0, monthly_counts={},
)
tmp = tmp.sort_values(date_column).reset_index(drop=True)
dates = tmp[date_column]
values = pd.to_numeric(tmp[value_column], errors="coerce").fillna(0)
# ordinal representation for linear fit
ordinals = np.array([(d - dates.min()).days for d in dates], dtype=float)
if ordinals.max() == 0:
slope = 0.0
else:
# simple least-squares slope
x_mean = ordinals.mean()
y_mean = values.mean()
num = ((ordinals - x_mean) * (values - y_mean)).sum()
den = ((ordinals - x_mean) ** 2).sum()
slope = float(num / den) if den != 0 else 0.0
if slope > 0.001:
direction = "increasing"
elif slope < -0.001:
direction = "decreasing"
else:
direction = "flat"
# monthly aggregation
monthly = tmp.set_index(date_column).resample("ME").size()
monthly_counts = {str(k.date()): int(v) for k, v in monthly.items()}
return TimeSeriesSummary(
date_column=date_column,
value_column=value_column,
start=str(dates.min().date()),
end=str(dates.max().date()),
count=len(tmp),
trend_direction=direction,
trend_slope=round(slope, 6),
mean=round(float(values.mean()), 4),
std=round(float(values.std()), 4),
monthly_counts=monthly_counts,
)
# ---------------------------------------------------------------------------
# Distribution Classification
# ---------------------------------------------------------------------------
@dataclass
class DistributionInfo:
"""Classification of a numeric column's distribution shape."""
column: str
skewness: float
kurtosis: float
classification: str # "normal", "right_skewed", "left_skewed", "heavy_tailed", "uniform", "sparse"
unique_ratio: float
sample_size: int
def classify_distribution(df: pd.DataFrame, column: str) -> DistributionInfo:
"""Classify the distribution shape of a numeric column."""
series = pd.to_numeric(df[column], errors="coerce").dropna()
n = len(series)
if n < 5:
return DistributionInfo(
column=column, skewness=0.0, kurtosis=0.0,
classification="sparse", unique_ratio=0.0, sample_size=n,
)
skew = float(series.skew())
kurt = float(series.kurtosis()) # excess kurtosis (Fisher)
unique_ratio = series.nunique() / max(n, 1)
if unique_ratio < 0.02:
classification = "sparse"
elif abs(skew) < 0.5 and abs(kurt) < 1.0:
classification = "normal"
elif skew > 1.0:
classification = "right_skewed"
elif skew < -1.0:
classification = "left_skewed"
elif kurt > 3.0:
classification = "heavy_tailed"
elif abs(kurt) < 0.5 and unique_ratio > 0.5:
classification = "uniform"
else:
classification = "normal"
return DistributionInfo(
column=column,
skewness=round(skew, 4),
kurtosis=round(kurt, 4),
classification=classification,
unique_ratio=round(unique_ratio, 4),
sample_size=n,
)
def classify_all_distributions(df: pd.DataFrame) -> list[DistributionInfo]:
"""Classify distributions for all numeric columns."""
return [classify_distribution(df, col) for col in df.select_dtypes(include="number").columns]
# ---------------------------------------------------------------------------
# Anomaly Flagging
# ---------------------------------------------------------------------------
@dataclass
class AnomalyReport:
"""Summary of anomalies detected across the entire DataFrame."""
total_rows: int
flagged_rows: int
flagged_pct: float
column_reports: list[OutlierReport]
def flag_anomalies(
df: pd.DataFrame,
method: str = "iqr",
**kwargs: Any,
) -> tuple[pd.DataFrame, AnomalyReport]:
"""Add an `_anomaly` boolean column and return a summary report.
A row is flagged as anomalous if it is an outlier in any numeric column.
"""
reports = detect_all_outliers(df, method=method, **kwargs)
flagged = set()
for r in reports:
flagged.update(r.outlier_indices)
out = df.copy()
out["_anomaly"] = out.index.isin(flagged)
report = AnomalyReport(
total_rows=len(df),
flagged_rows=len(flagged),
flagged_pct=round(len(flagged) / max(len(df), 1) * 100, 2),
column_reports=reports,
)
return out, report