-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_forest_model.py
More file actions
236 lines (185 loc) · 8.42 KB
/
random_forest_model.py
File metadata and controls
236 lines (185 loc) · 8.42 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""
Random Forest Model for Loan Action Prediction
This script trains a Random Forest classifier on mortgage/loan data to predict loan actions.
"""
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
def load_and_explore_data(train_path, test_path, validation_path):
"""Load the datasets and perform basic exploration"""
print("Loading datasets...")
train_df = pd.read_csv(train_path)
test_df = pd.read_csv(test_path)
validation_df = pd.read_csv(validation_path)
print(f"Training set shape: {train_df.shape}")
print(f"Test set shape: {test_df.shape}")
print(f"Validation set shape: {validation_df.shape}")
print(f"\nTarget variable (action_taken) distribution in training set:")
print(train_df['action_taken'].value_counts().sort_index())
return train_df, test_df, validation_df
def preprocess_data(train_df, test_df, validation_df):
"""Preprocess the data for machine learning"""
print("\nPreprocessing data...")
# Combine all datasets for consistent preprocessing
all_data = pd.concat([train_df, test_df, validation_df], ignore_index=True)
# Identify the target variable
target_col = 'action_taken'
# Separate features and target for training set
X_train = train_df.drop(columns=[target_col])
y_train = train_df[target_col]
X_test = test_df.drop(columns=[target_col])
y_test = test_df[target_col]
X_val = validation_df.drop(columns=[target_col])
y_val = validation_df[target_col]
# Handle missing values and data types
print("Handling missing values and data types...")
# Identify numeric and categorical columns
numeric_cols = []
categorical_cols = []
for col in X_train.columns:
if X_train[col].dtype in ['int64', 'float64']:
# Check if it's actually categorical (like codes)
unique_vals = X_train[col].nunique()
if unique_vals <= 20 and col not in ['loan_amount', 'property_value', 'income',
'combined_loan_to_value_ratio', 'interest_rate']:
categorical_cols.append(col)
else:
numeric_cols.append(col)
else:
categorical_cols.append(col)
print(f"Numeric columns: {len(numeric_cols)}")
print(f"Categorical columns: {len(categorical_cols)}")
# Handle numeric columns
for col in numeric_cols:
# Fill missing values with median
median_val = X_train[col].median()
X_train[col] = X_train[col].fillna(median_val)
X_test[col] = X_test[col].fillna(median_val)
X_val[col] = X_val[col].fillna(median_val)
# Handle categorical columns
label_encoders = {}
for col in categorical_cols:
# Fill missing values with mode or 'Unknown'
mode_val = X_train[col].mode()
fill_val = mode_val[0] if len(mode_val) > 0 else 'Unknown'
X_train[col] = X_train[col].fillna(fill_val).astype(str)
X_test[col] = X_test[col].fillna(fill_val).astype(str)
X_val[col] = X_val[col].fillna(fill_val).astype(str)
# Label encode categorical variables
le = LabelEncoder()
# Fit on combined data to handle all possible categories
combined_col = pd.concat([X_train[col], X_test[col], X_val[col]])
le.fit(combined_col)
X_train[col] = le.transform(X_train[col])
X_test[col] = le.transform(X_test[col])
X_val[col] = le.transform(X_val[col])
label_encoders[col] = le
print(f"Final feature matrix shape: {X_train.shape}")
return X_train, X_test, X_val, y_train, y_test, y_val, label_encoders
def train_random_forest(X_train, y_train):
"""Train Random Forest model"""
print("\nTraining Random Forest model...")
# Initialize Random Forest with good default parameters
rf_model = RandomForestClassifier(
n_estimators=100, # Number of trees
max_depth=10, # Maximum depth to prevent overfitting
min_samples_split=5, # Minimum samples to split a node
min_samples_leaf=2, # Minimum samples in leaf node
random_state=42, # For reproducibility
n_jobs=-1 # Use all available cores
)
# Train the model
rf_model.fit(X_train, y_train)
print("Model training completed!")
# Display feature importance
feature_importance = pd.DataFrame({
'feature': X_train.columns,
'importance': rf_model.feature_importances_
}).sort_values('importance', ascending=False)
print("\nTop 10 Most Important Features:")
print(feature_importance.head(10))
return rf_model, feature_importance
def evaluate_model(model, X_test, y_test, X_val=None, y_val=None):
"""Evaluate the model performance"""
print("\nEvaluating model performance...")
# Predictions on test set
y_pred = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Test Accuracy: {accuracy:.4f}")
# Classification report
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
print("\nConfusion Matrix:")
print(cm)
# If validation set is provided, evaluate on it too
if X_val is not None and y_val is not None:
y_val_pred = model.predict(X_val)
val_accuracy = accuracy_score(y_val, y_val_pred)
print(f"\nValidation Accuracy: {val_accuracy:.4f}")
return y_pred, accuracy
def plot_feature_importance(feature_importance, top_n=15):
"""Plot feature importance"""
plt.figure(figsize=(10, 8))
top_features = feature_importance.head(top_n)
plt.barh(range(len(top_features)), top_features['importance'])
plt.yticks(range(len(top_features)), top_features['feature'])
plt.xlabel('Feature Importance')
plt.title(f'Top {top_n} Feature Importances - Random Forest')
plt.gca().invert_yaxis()
plt.tight_layout()
plt.savefig('feature_importance.png', dpi=300, bbox_inches='tight')
plt.show()
def main():
"""Main execution function"""
print("="*60)
print("RANDOM FOREST MODEL FOR LOAN ACTION PREDICTION")
print("="*60)
# File paths
train_path = "TrainingSet.csv"
test_path = "TestSet.csv"
validation_path = "ValidationSet.csv"
try:
# Load data
train_df, test_df, validation_df = load_and_explore_data(train_path, test_path, validation_path)
# Preprocess data
X_train, X_test, X_val, y_train, y_test, y_val, label_encoders = preprocess_data(
train_df, test_df, validation_df
)
# Train model
rf_model, feature_importance = train_random_forest(X_train, y_train)
# Evaluate model
y_pred, accuracy = evaluate_model(rf_model, X_test, y_test, X_val, y_val)
# Plot feature importance
plot_feature_importance(feature_importance)
# Cross-validation on training set
print("\nPerforming 5-fold cross-validation on training set...")
cv_scores = cross_val_score(rf_model, X_train, y_train, cv=5, scoring='accuracy')
print(f"Cross-validation scores: {cv_scores}")
print(f"Mean CV accuracy: {cv_scores.mean():.4f} (+/- {cv_scores.std() * 2:.4f})")
print("\n" + "="*60)
print("MODEL TRAINING AND EVALUATION COMPLETED!")
print("="*60)
# Save the model (optional)
import joblib
joblib.dump(rf_model, 'random_forest_model.pkl')
joblib.dump(label_encoders, 'label_encoders.pkl')
print("Model and encoders saved to files.")
return rf_model, feature_importance, accuracy
except FileNotFoundError as e:
print(f"Error: Could not find file. {e}")
print("Please make sure the CSV files are in the current directory.")
except Exception as e:
print(f"An error occurred: {e}")
return None, None, None
if __name__ == "__main__":
model, importance, acc = main()