-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_train_val.py
More file actions
48 lines (38 loc) · 1.37 KB
/
split_train_val.py
File metadata and controls
48 lines (38 loc) · 1.37 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
import pandas as pd
from sklearn.model_selection import train_test_split
import os
# --- Configuration ---
INPUT_FILE = 'data/transactions_v1.parquet'
OUTPUT_DIR = 'data/'
TRAIN_FILE = os.path.join(OUTPUT_DIR, 'train.parquet')
VAL_FILE = os.path.join(OUTPUT_DIR, 'val.parquet')
TEST_SIZE = 0.2
RANDOM_STATE = 42
TARGET_COLUMN = 'Class'
def split_data():
"""Reads the input data, splits it, and saves train/val sets."""
print(f"Reading data from {INPUT_FILE}...")
try:
df = pd.read_parquet(INPUT_FILE)
except FileNotFoundError:
print(f"Error: Input file not found at '{INPUT_FILE}'")
return
# Ensure the output directory exists
os.makedirs(OUTPUT_DIR, exist_ok=True)
print(f"Splitting data into train and validation sets (test_size={TEST_SIZE})...")
# Perform a stratified split to maintain the target distribution
train_df, val_df = train_test_split(
df,
test_size=TEST_SIZE,
random_state=RANDOM_STATE,
stratify=df[TARGET_COLUMN]
)
print(f"Train set shape: {train_df.shape}")
print(f"Validation set shape: {val_df.shape}")
# Save the splits
train_df.to_parquet(TRAIN_FILE, index=False)
val_df.to_parquet(VAL_FILE, index=False)
print(f"✅ Train data saved to: {TRAIN_FILE}")
print(f"✅ Validation data saved to: {VAL_FILE}")
if __name__ == "__main__":
split_data()