CoolFace
Apppublic

MustafaErboga/open-credit-scoring

sourceHugging Faceapache-2.0updated 8mo agoView on Hugging Face
1likes
balanced_master.py70 linesDownload Raw Back to src
1import pandas as pd2import numpy as np3import re4import lightgbm as lgb5from sklearn.model_selection import train_test_split6import joblib7 8def train_balanced_model():9    df = pd.read_csv("data/train.csv", low_memory=False)10    11    def clean(val):12        res = re.findall(r"[-+]?\d*\.?\d+", str(val))13        return float(res[0]) if res else 0.014 15    features = [16        'Outstanding_Debt', 'Interest_Rate', 'Delay_from_due_date',17        'Num_of_Delayed_Payment', 'Credit_Mix', 'Annual_Income', 18        'Monthly_Balance', 'Num_Credit_Inquiries', 'Age'19    ]20 21    for col in ['Outstanding_Debt', 'Interest_Rate', 'Delay_from_due_date', 22                'Num_of_Delayed_Payment', 'Annual_Income', 'Monthly_Balance', 'Num_Credit_Inquiries', 'Age']:23        df[col] = df[col].apply(clean)24 25    # Credit_Mix: Bad=0, Standard=1, Good=226    df['Credit_Mix'] = df['Credit_Mix'].map({'Bad': 0, 'Standard': 1, 'Good': 2}).fillna(1)27    X = df[features].copy()28    y = df['Credit_Score'].map({'Poor': 0, 'Standard': 1, 'Good': 2})29 30    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)31 32    # FINE TUNING33    34    model = lgb.LGBMClassifier(35        n_estimators=1000,36        learning_rate=0.01,        37        subsample=0.8,             38        colsample_bytree=0.8,      39        class_weight={0: 2.5, 1: 1.0, 2: 4.0}, 40        max_depth=10,              41        num_leaves=64,42        random_state=42,43        verbose=-144    )45    model.fit(X_train, y_train)46 47    # SYSTEM CALIBRATION TEST48    scenarios = {49        "Zengin (Good)": [50.0, 3.0, 0, 0, 2, 200000.0, 8000.0, 0, 45],50        "Ortalama (Standard)": [1500.0, 15.0, 7, 4, 1, 55000.0, 1200.0, 5, 33],51        "Batık (Poor)": [10000.0, 35.0, 45, 20, 0, 12000.0, 10.0, 15, 20]52    }53 54    print("\n" + "="*45)55    print("SYSTEM CALIBRATION TEST")56    print("="*45)57 58    for name, vals in scenarios.items():59        test_df = pd.DataFrame([vals], columns=features)60        pred = model.predict(test_df)[0]61        label = {0: "Poor", 1: "Standard", 2: "Good"}[pred]62        print(f"Senaryo: {name.ljust(20)} -> TAHMİN: {label}")63    64    joblib.dump(model, "models/final_safe_model.joblib")65    joblib.dump(features, "models/final_features.joblib")66    print("="*45)67    print("✅ Model kaydedildi. LÜTFEN UVICORN'U RESTART ET!")68 69if __name__ == "__main__":70    train_balanced_model()