-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
83 lines (73 loc) · 3.19 KB
/
Copy pathpreprocessing.py
File metadata and controls
83 lines (73 loc) · 3.19 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
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
class DataPreprocessor:
def __init__(self, data_path='data/credit.csv', random_state=42):
self.data_path = data_path
self.random_state = random_state
self.data = None
self.scaler = None
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None
def load_data(self):
print("Loading data...")
self.data = pd.read_csv(self.data_path)
print(f"✓ Loaded {len(self.data)} records with {len(self.data.columns)} features")
def handle_missing_values(self):
print("Handling missing values...")
imputer = SimpleImputer(strategy='median')
numeric_cols = self.data.select_dtypes(include=[np.number]).columns
self.data[numeric_cols] = imputer.fit_transform(self.data[numeric_cols])
print(f"✓ Missing values handled")
def encode_categorical(self):
print("Encoding categorical variables...")
categorical_cols = self.data.select_dtypes(include=['object']).columns
for col in categorical_cols:
if col != 'default':
encoder = LabelEncoder()
self.data[col] = encoder.fit_transform(self.data[col])
print(f"✓ Encoded {len(categorical_cols)} columns")
def engineer_features(self):
print("Creating new features...")
if 'debt' in self.data.columns and 'income' in self.data.columns:
self.data['debt_to_income_ratio'] = self.data['debt'] / self.data['income']
print(f"✓ Created DTI ratio (mean: {self.data['debt_to_income_ratio'].mean():.4f})")
else:
print("⚠ Debt/Income columns not found")
def split_data(self):
print("Splitting data into train/test...")
y = self.data['default'].astype(int)
X = self.data.drop('default', axis=1)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y,
test_size=0.33,
random_state=self.random_state,
stratify=y
)
print(f"✓ Train: {len(self.X_train)} | Test: {len(self.X_test)}")
print(f"✓ Train classes: {np.bincount(self.y_train.astype(int))}")
print(f"✓ Test classes: {np.bincount(self.y_test.astype(int))}")
def scale_features(self):
print("Scaling features...")
self.scaler = StandardScaler()
self.X_train = self.scaler.fit_transform(self.X_train)
self.X_test = self.scaler.transform(self.X_test)
print(f"✓ Features scaled")
def run(self):
"""Execute the full preprocessing pipeline"""
try:
self.load_data()
self.handle_missing_values()
self.encode_categorical()
self.engineer_features()
self.split_data()
self.scale_features()
print("\nPreprocessing complete!\n")
return self.X_train, self.X_test, self.y_train, self.y_test
except Exception as e:
print(f"Error: {e}")
raise