-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_engineering.py
More file actions
36 lines (27 loc) · 1.6 KB
/
Copy pathfeature_engineering.py
File metadata and controls
36 lines (27 loc) · 1.6 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
import pandas as pd
def perform_feature_engineering(df):
"""
Takes the raw simulation data and creates ML-ready features.
"""
df_engineered = df.copy()
# Ratio: Discretionary vs Essential Spending
# Essential = Food + Bills
df_engineered['essential_spending'] = df_engineered['spending_food'] + df_engineered['spending_bills']
df_engineered['discretionary_spend_ratio'] = df_engineered['spending_entertainment'] / (df_engineered['essential_spending'] + 1)
# Utility Payment Delay Indicator (Categorical to Numeric)
df_engineered['utility_delay_indicator'] = df_engineered['utility_payment_timing'].apply(lambda x: 1 if x == "Late" else 0)
# Ratio: EMI to Salary
df_engineered['emi_to_salary_ratio'] = df_engineered['loan_emi_amount'] / (df_engineered['monthly_salary'] + 1)
# Ratio: ATM Withdrawals to Salary
df_engineered['atm_withdrawal_intensity'] = df_engineered['atm_withdrawals_amount'] / (df_engineered['monthly_salary'] + 1)
# Encode categorical purely in case (not strictly needed since we manually turn strings to numeric above)
df_engineered = pd.get_dummies(df_engineered, columns=['utility_payment_timing'], drop_first=True)
# Select columns to drop (identifiers)
cols_to_drop = ['customer_id', 'essential_spending']
df_engineered = df_engineered.drop(columns=cols_to_drop, errors='ignore')
return df_engineered
if __name__ == "__main__":
from data_generation import generate_synthetic_data
df = generate_synthetic_data(10)
df_features = perform_feature_engineering(df)
print(df_features.head())