-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
179 lines (139 loc) · 5.75 KB
/
Copy pathtrain_model.py
File metadata and controls
179 lines (139 loc) · 5.75 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
Train and evaluate loan approval prediction models
"""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import joblib
import matplotlib.pyplot as plt
import seaborn as sns
def load_and_preprocess_data(filepath='loan_data.csv'):
"""Load and preprocess the loan data"""
df = pd.read_csv(filepath)
print("Original Data Shape:", df.shape)
print("\nMissing Values:")
print(df.isnull().sum())
# Handle missing values (if any)
df = df.dropna()
# Encode categorical variables
label_encoders = {}
categorical_cols = ['Gender', 'Married', 'Dependents', 'Education',
'Self_Employed', 'Property_Area']
for col in categorical_cols:
le = LabelEncoder()
df[col] = le.fit_transform(df[col])
label_encoders[col] = le
# Save label encoders
joblib.dump(label_encoders, 'label_encoders.pkl')
# Prepare features and target
X = df.drop('Loan_Status', axis=1)
y = df['Loan_Status'].map({'Y': 1, 'N': 0})
return X, y, df
def train_models(X_train, X_test, y_train, y_test):
"""Train multiple classification models"""
models = {
'Decision Tree': DecisionTreeClassifier(random_state=42, max_depth=5),
'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
'SVM': SVC(kernel='rbf', random_state=42, probability=True)
}
results = {}
for name, model in models.items():
print(f"\n{'='*50}")
print(f"Training {name}...")
print('='*50)
# Train the model
model.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
# Evaluation
accuracy = accuracy_score(y_test, y_pred)
# Cross-validation score
cv_scores = cross_val_score(model, X_train, y_train, cv=5)
print(f"\nAccuracy: {accuracy:.4f}")
print(f"Cross-Validation Score: {cv_scores.mean():.4f} (+/- {cv_scores.std():.4f})")
print(f"\nClassification Report:")
print(classification_report(y_test, y_pred, target_names=['Not Approved', 'Approved']))
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
results[name] = {
'model': model,
'accuracy': accuracy,
'cv_score': cv_scores.mean(),
'predictions': y_pred,
'confusion_matrix': cm
}
return results
def plot_results(results, y_test):
"""Plot model comparison and confusion matrices"""
# Model Comparison
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Accuracy Comparison
model_names = list(results.keys())
accuracies = [results[name]['accuracy'] for name in model_names]
cv_scores = [results[name]['cv_score'] for name in model_names]
ax1 = axes[0, 0]
x_pos = np.arange(len(model_names))
ax1.bar(x_pos - 0.2, accuracies, 0.4, label='Test Accuracy', color='skyblue')
ax1.bar(x_pos + 0.2, cv_scores, 0.4, label='CV Score', color='lightcoral')
ax1.set_xlabel('Models')
ax1.set_ylabel('Score')
ax1.set_title('Model Performance Comparison')
ax1.set_xticks(x_pos)
ax1.set_xticklabels(model_names, rotation=45)
ax1.legend()
ax1.set_ylim([0, 1])
# Confusion Matrices
for idx, (name, result) in enumerate(results.items()):
if idx < 3:
row = (idx + 1) // 2
col = (idx + 1) % 2
ax = axes[row, col]
sns.heatmap(result['confusion_matrix'], annot=True, fmt='d',
cmap='Blues', ax=ax, cbar=False)
ax.set_title(f'{name} - Confusion Matrix')
ax.set_ylabel('Actual')
ax.set_xlabel('Predicted')
ax.set_xticklabels(['Not Approved', 'Approved'])
ax.set_yticklabels(['Not Approved', 'Approved'])
plt.tight_layout()
plt.savefig('model_comparison.png', dpi=300, bbox_inches='tight')
print("\n✓ Model comparison plot saved as 'model_comparison.png'")
plt.close()
def save_best_model(results):
"""Save the best performing model"""
best_model_name = max(results, key=lambda x: results[x]['accuracy'])
best_model = results[best_model_name]['model']
joblib.dump(best_model, 'loan_model.pkl')
with open('model_info.txt', 'w') as f:
f.write(f"Best Model: {best_model_name}\n")
f.write(f"Accuracy: {results[best_model_name]['accuracy']:.4f}\n")
f.write(f"CV Score: {results[best_model_name]['cv_score']:.4f}\n")
print(f"\n✓ Best model ({best_model_name}) saved as 'loan_model.pkl'")
print(f"✓ Accuracy: {results[best_model_name]['accuracy']:.4f}")
return best_model_name
if __name__ == "__main__":
print("="*60)
print("LOAN APPROVAL PREDICTION MODEL TRAINING")
print("="*60)
# Load and preprocess data
X, y, df = load_and_preprocess_data()
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print(f"\nTraining Set: {X_train.shape[0]} samples")
print(f"Test Set: {X_test.shape[0]} samples")
# Train models
results = train_models(X_train, X_test, y_train, y_test)
# Plot results
plot_results(results, y_test)
# Save best model
best_model_name = save_best_model(results)
print("\n" + "="*60)
print("TRAINING COMPLETED SUCCESSFULLY!")
print("="*60)