7.Design and implement a Random Forest Classification model to predict if a loan will get approved or not for a bank customer dataset. Estimate the accuracy of the model. Also write the program to visualize insights of the dataset.

PHOTO EMBED

Sun Nov 03 2024 13:00:23 GMT+0000 (Coordinated Universal Time)

Saved by @varuntej #python

import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
from sklearn.model_selection import train_test_split 
from sklearn.ensemble import RandomForestClassifier 
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix 
from sklearn.preprocessing import StandardScaler 
# Load the dataset 
df = pd.read_csv('bank_loans.csv') 
# Display the first few rows to understand the structure (optional) 
print(df.head()) 
print(df.info()) 
# Check for missing values (optional) 
print("Missing values:\n", df.isnull().sum()) 
# Handle missing values if any (optional, assuming numerical columns filled with mean) 
df.fillna(df.mean(), inplace=True) 
# Encode categorical variables (assuming 'Gender', 'Married', etc. as example categorical features) 
df = pd.get_dummies(df, drop_first=True) 
# Separate features and target variable 
X = df.drop(columns=['Loan_Status'])  # Assuming 'Loan_Status' is the target column (1 = Approved, 0 = Not Approved) 
y = df['Loan_Status'] 
# Split the data into training and testing sets 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 
# Standardize the features 
scaler = StandardScaler() 
X_train_scaled = scaler.fit_transform(X_train) 
X_test_scaled = scaler.transform(X_test) 
# Initialize and train the Random Forest Classifier 
rf_model = RandomForestClassifier(n_estimators=100, random_state=42) 
rf_model.fit(X_train_scaled, y_train) 
# Predict on the test set 
y_pred = rf_model.predict(X_test_scaled) 
# Calculate accuracy and other performance metrics 
accuracy = accuracy_score(y_test, y_pred) 
print("Random Forest Model Accuracy:", accuracy) 
print("\nClassification Report:\n", classification_report(y_test, y_pred)) 
# Plot confusion matrix for better insight into model performance 
conf_matrix = confusion_matrix(y_test, y_pred) 
plt.figure(figsize=(6, 4)) 
sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=['Not Approved', 'Approved'], 
yticklabels=['Not Approved', 'Approved']) 
plt.xlabel("Predicted") 
plt.ylabel("Actual") 
plt.title("Confusion Matrix") 
plt.show() 
# Feature importance visualization 
feature_importances = rf_model.feature_importances_ 
features = X.columns 
# Create a dataframe for feature importances 
feature_df = pd.DataFrame({'Feature': features, 'Importance': feature_importances}) 
feature_df = feature_df.sort_values(by='Importance', ascending=False) 
# Plot feature importances 
plt.figure(figsize=(10, 6)) 
sns.barplot(x='Importance', y='Feature', data=feature_df, palette="viridis") 
plt.title("Feature Importances in Random Forest Model") 
plt.xlabel("Importance") 
plt.ylabel("Feature") 
plt.show()
content_copyCOPY