import pandas as pd from datetime import datetime def encode_payment_mode(mode): return {"Annual": 0, "Semi-Annual": 1, "Quarterly": 2, "Monthly": 3}.get(mode, -1) def calculate_months_since(date_str): try: delta = datetime.now() - datetime.strptime(date_str, "%Y-%m-%d") return delta.days // 30 except: return 0 def preprocess_input(data): return pd.DataFrame([{ "months_since_last_payment": calculate_months_since(data["last_premium_paid_date"]), "payment_mode_encoded": encode_payment_mode(data["payment_mode"]), "policy_term": data["policy_term"], "policy_age": data["policy_age"] }]) def preprocess_dataframe(df): df["months_since_last_payment"] = df["last_premium_paid_date"].apply(calculate_months_since) df["payment_mode_encoded"] = df["payment_mode"].apply(encode_payment_mode) X = df[["months_since_last_payment", "payment_mode_encoded", "policy_term", "policy_age"]] y = df["risk"] return X, y