-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
61 lines (46 loc) · 2 KB
/
Copy pathdata_utils.py
File metadata and controls
61 lines (46 loc) · 2 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
import numpy as np
# -------------------- Normalization / Scaling --------------------
def minmax_scale(X, feature_range=(0.0, 1.0), eps=1e-12):
"""
Scale each feature of X to [min, max] range (default [0,1]).
Returns (X_scaled, X_min, X_max) so you can invert later.
"""
X = np.asarray(X, dtype=float)
X_min = X.min(axis=0)
X_max = X.max(axis=0)
scale = (feature_range[1] - feature_range[0]) / np.maximum(X_max - X_min, eps)
X_scaled = feature_range[0] + (X - X_min) * scale
return X_scaled, X_min, X_max
def minmax_inverse(X_scaled, X_min, X_max, feature_range=(0.0, 1.0), eps=1e-12):
"""
Inverse of minmax_scale() — returns data in original feature scale.
"""
scale = (X_max - X_min) / np.maximum(feature_range[1] - feature_range[0], eps)
return X_min + (X_scaled - feature_range[0]) * scale
def zscore_normalize(X, eps=1e-12):
"""
Normalize each feature to zero mean and unit variance (Z-score normalization).
Returns (X_norm, mean, std).
"""
X = np.asarray(X, dtype=float)
mean = X.mean(axis=0)
std = np.maximum(X.std(axis=0), eps)
return (X - mean) / std, mean, std
def zscore_inverse(X_norm, mean, std, eps=1e-12):
return mean + X_norm * np.maximum(std, eps)
def save_original_with_labels(instance_ids, X_raw, labels, out_csv, header):
import csv
import numpy as np
if len(instance_ids) != len(X_raw) or len(labels) != len(X_raw):
raise ValueError(
f"Length mismatch: instance_ids={len(instance_ids)}, X_raw={len(X_raw)}, labels={len(labels)}"
)
with open(out_csv, "w", newline="") as f:
w = csv.writer(f)
w.writerow(header)
for inst, row, lab in zip(instance_ids, X_raw, labels):
x1 = float(row[0]) # IPC
x2 = int(np.rint(row[1])) # TOT_INS -> force integer, remove .0000001 noise
# Optional: round IPC for nicer CSV
x1 = round(x1, 6)
w.writerow([int(inst), x1, x2, int(lab)])