VectorV17 commited on
Commit
2c1d8f5
·
verified ·
1 Parent(s): 88e04fd

Delete train.py

Browse files
Files changed (1) hide show
  1. train.py +0 -102
train.py DELETED
@@ -1,102 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """train.ipynb
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1hMAC2s_7RstOym5W6rezL_47Ch3iwR5J
8
- """
9
-
10
- import pandas as pd
11
- import numpy as np
12
- import joblib
13
-
14
- from sklearn.preprocessing import StandardScaler, OneHotEncoder
15
- from sklearn.impute import KNNImputer, SimpleImputer
16
- from sklearn.compose import ColumnTransformer
17
- from sklearn.pipeline import Pipeline
18
-
19
- from sklearn.linear_model import LogisticRegression
20
- from sklearn.ensemble import RandomForestClassifier, StackingClassifier, VotingClassifier
21
- from xgboost import XGBClassifier
22
- from imblearn.over_sampling import SMOTE
23
- from imblearn.pipeline import Pipeline as ImbPipeline
24
-
25
- df = pd.read_csv('loan_dataset.csv')
26
- X = df.drop('BAD', axis=1)
27
- y = df['BAD']
28
-
29
- # Explicitly separate numeric and string columns to fix Pandas4Warnings
30
- num_cols = X.select_dtypes(include=['int64', 'float64']).columns.tolist()
31
- cat_cols = X.select_dtypes(include=['object', 'string']).columns.tolist()
32
-
33
- def cap_outliers_iqr(data, columns):
34
- data_clean = data.copy()
35
- for col in columns:
36
- Q1 = data_clean[col].quantile(0.25)
37
- Q3 = data_clean[col].quantile(0.75)
38
- IQR = Q3 - Q1
39
- if IQR > 0:
40
- lower_bound = Q1 - 1.5 * IQR
41
- upper_bound = Q3 + 1.5 * IQR
42
- data_clean[col] = np.where(data_clean[col] < lower_bound, lower_bound, data_clean[col])
43
- data_clean[col] = np.where(data_clean[col] > upper_bound, upper_bound, data_clean[col])
44
- return data_clean
45
-
46
- X_cleaned = cap_outliers_iqr(X, num_cols)
47
-
48
- # 3. Build Safe Transformation Layouts
49
- num_pipeline = Pipeline([
50
- ('imputer', KNNImputer(n_neighbors=5)),
51
- ('scaler', StandardScaler())
52
- ])
53
-
54
- # Fixed: Uses SimpleImputer on strings before feeding into OneHotEncoder to prevent ValueErrors
55
- cat_pipeline = Pipeline([
56
- ('imputer', SimpleImputer(strategy='most_frequent')),
57
- ('encoder', OneHotEncoder(handle_unknown='ignore', sparse_output=False))
58
- ])
59
-
60
- preprocessor = ColumnTransformer([
61
- ('num', num_pipeline, num_cols),
62
- ('cat', cat_pipeline, cat_cols)
63
- ])
64
-
65
- # 4. Streamlined High-Speed Base Learners
66
- rf_base = RandomForestClassifier(n_estimators=60, max_depth=8, random_state=42, n_jobs=-1)
67
- xgb_base = XGBClassifier(n_estimators=60, learning_rate=0.1, max_depth=4, eval_metric='logloss', random_state=42, n_jobs=-1)
68
- voting_base = VotingClassifier(estimators=[('rf_v', rf_base), ('xgb_v', xgb_base)], voting='soft')
69
-
70
- # Meta-Classifier Network
71
- stacking_ensemble = StackingClassifier(
72
- estimators=[('rf', rf_base), ('xgb', xgb_base), ('voting', voting_base)],
73
- final_estimator=LogisticRegression(max_iter=500, C=0.1),
74
- cv=2, # Reduced cross-validation folds to minimize training overhead
75
- n_jobs=-1
76
- )
77
-
78
- production_pipeline = ImbPipeline([
79
- ('preprocessing', preprocessor),
80
- ('smote', SMOTE(random_state=42, sampling_strategy=0.6)),
81
- ('ensemble_model', stacking_ensemble)
82
- ])
83
-
84
- # Fit and export to disk
85
- production_pipeline.fit(X_cleaned, y)
86
- joblib.dump(production_pipeline, 'loan_stacking_model.pkl')
87
- print("🎉 Pipeline trained successfully and saved as 'loan_stacking_model.pkl'!")
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-