Spaces:
Sleeping
Sleeping
File size: 2,685 Bytes
22de844 | 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import pandas as pd
import numpy as np
import pickle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score
# =====================
# Load dataset
# =====================
df = pd.read_csv("loan_data.csv") # upload CSV in same folder
print(df.head())
# Drop ID column
df.drop(columns=['Loan_ID'], inplace=True)
# Encode target
df['Loan_Status'] = df['Loan_Status'].map({'Y': 1, 'N': 0})
# =====================
# Target and features
# =====================
X = df.drop('Loan_Status', axis=1)
y = df['Loan_Status']
# =====================
# Column split
# =====================
numeric_features = X.select_dtypes(include=['int64', 'float64']).columns
categorical_features = X.select_dtypes(include=['object']).columns
# =====================
# Preprocessing
# =====================
num_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
])
cat_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('encoder', OneHotEncoder(handle_unknown='ignore'))
])
preprocessor = ColumnTransformer(transformers=[
('num', num_transformer, numeric_features),
('cat', cat_transformer, categorical_features)
])
# =====================
# Random Forest Model
# =====================
rf_model = RandomForestClassifier(
n_estimators=200,
max_depth=10,
min_samples_split=2,
random_state=42,
n_jobs=-1
)
# =====================
# Full Pipeline
# =====================
rf_pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('model', rf_model)
])
# =====================
# Train-test split
# =====================
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# =====================
# Train
# =====================
rf_pipeline.fit(X_train, y_train)
# =====================
# Evaluation
# =====================
y_pred = rf_pipeline.predict(X_test)
acc = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print(f"Accuracy: {acc:.4f}")
print(f"F1 Score: {f1:.4f}")
# =====================
# Save model
# =====================
with open("loan_rf_pipeline.pkl", "wb") as f:
pickle.dump(rf_pipeline, f)
print("✅ Loan Random Forest pipeline saved as loan_rf_pipeline.pkl")
|