-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
73 lines (58 loc) · 2.65 KB
/
data.py
File metadata and controls
73 lines (58 loc) · 2.65 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
"""
data.py — Data loading and preprocessing
Dataset: Kaggle Credit Card Fraud Detection
Download: https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud
Place creditcard.csv in the same directory as app.py.
Split: 70 / 15 / 15 stratified by Class label.
Features: V1–V28 + Amount + Time (30 features, StandardScaler-normalized)
"""
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from pathlib import Path
def load_data(csv_path="creditcard.csv") -> pd.DataFrame:
path = Path(csv_path)
if not path.exists():
raise FileNotFoundError(
f"\ncreditcard.csv not found at '{csv_path}'.\n"
"Download: https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud\n"
)
# Try UTF-8 first, fall back to latin-1 (handles all single-byte encodings)
try:
df = pd.read_csv(path, encoding="utf-8")
except UnicodeDecodeError:
df = pd.read_csv(path, encoding="latin-1")
print(f"Loaded {len(df):,} transactions | "
f"Fraud: {df['Class'].sum()} ({df['Class'].mean()*100:.3f}%)")
return df
def make_splits(df, seed=42, val_size=0.15, test_size=0.15):
feature_cols = [c for c in df.columns if c != "Class"]
X = df[feature_cols].values.astype(np.float32)
y = df["Class"].values.astype(np.float32)
X_train, X_tmp, y_train, y_tmp = train_test_split(
X, y, test_size=val_size + test_size,
random_state=seed, stratify=y)
X_val, X_test, y_val, y_test = train_test_split(
X_tmp, y_tmp, test_size=test_size / (val_size + test_size),
random_state=seed, stratify=y_tmp)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train).astype(np.float32)
X_val = scaler.transform(X_val).astype(np.float32)
X_test = scaler.transform(X_test).astype(np.float32)
pos_weight = float((y_train == 0).sum() / (y_train == 1).sum())
print(f"Seed {seed} | Train: {len(X_train):,} (fraud: {int(y_train.sum())}) | "
f"Val: {len(X_val):,} | Test: {len(X_test):,} | "
f"pos_weight: {pos_weight:.1f}")
return (X_train, X_val, X_test,
y_train, y_val, y_test,
feature_cols, pos_weight, scaler)
def make_loaders(X_train, X_val, X_test,
y_train, y_val, y_test,
batch_size=2048):
def _ds(X, y, shuffle):
ds = TensorDataset(torch.tensor(X), torch.tensor(y))
return DataLoader(ds, batch_size=batch_size, shuffle=shuffle)
return _ds(X_train, y_train, True), _ds(X_val, y_val, False), _ds(X_test, y_test, False)