CoolFace
Apppublic

Arunsalla/tabular-ml-classification-project

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
preprocess.py40 linesDownload Raw Back to root
1import pandas as pd
2import numpy as np
3import os
4
5# Path to dataset
6DATA_FILE = "data/dataset.csv"
7
8# Check if dataset exists
9if not os.path.exists(DATA_FILE):
10    print(f"Error: {DATA_FILE} not found!")
11    exit()
12
13# Load dataset
14df = pd.read_csv(DATA_FILE)
15
16# Ensure only the 5 features + target exist
17expected_cols = ["age", "bp", "cholesterol", "glucose", "heart_rate", "target"]
18df = df[expected_cols]
19
20# Fill missing values with median
21for col in expected_cols[:-1]:
22    df[col] = df[col].fillna(df[col].median())
23
24# Convert all to numeric
25df[expected_cols] = df[expected_cols].apply(pd.to_numeric, errors='coerce')
26
27# Drop rows with remaining NaNs
28df = df.dropna()
29
30# Save cleaned dataset
31df.to_csv(DATA_FILE, index=False)
32print(f"Dataset preprocessed and saved to {DATA_FILE}")
33
34# Save feature and label arrays
35X = df[expected_cols[:-1]].values
36y = df["target"].values
37np.save("data/X.npy", X)
38np.save("data/y.npy", y)
39print("Feature and label arrays saved: X.npy, y.npy")
40