How to Predict Loan Approval Status (Approved or Not) Using Keras and Deep Learning?
Share
Condition for Predicting Loan Approval Status (Approved or Not) Using Keras with Deep Learning
Description: The provided code demonstrates a deep learning approach to predict loan approval status using a feedforward neural network. It processes a dataset by encoding categorical features, scaling numerical features, and splitting the data into training and testing sets. The neural network model is trained using a binary cross-entropy loss function, and performance is evaluated using classification metrics like accuracy,precision, recall, and F1-score.
Step-by-Step Process
Step1: Import the dataset using pd.read_csv() to load the loan approval dataset into a DataFrame.
Step2: Use isnull().sum() to check for any missing values in the dataset.
Step3: Apply LabelEncoder() to convert categorical columns into numerical values for model compatibility.
Step4: Clean column names by stripping leading or trailing spaces using df.columns.str.strip().
Step5: Normalize the feature set using StandardScaler() to standardize the numerical columns.
Step6: Split the dataset into features (X) by removing the target variable (loan_status), and the labels (y) as the target column.
Step7: Split the dataset into training and testing sets using train_test_split() with an 80-20 ratio.
Step8: Define a neural network model with an input layer, hidden layers with ReLU activation, and an output layer with sigmoid activation.
Step9: Compile the model using the Adam optimizer and binary cross-entropy loss function.
Step10: Train the model on the training data, predict the results on the test set,and evaluate performance using classification metrics such as accuracy,F1-score, and confusion matrix.
Sample Code
#Import Necessary Libraries
import pandas as pd
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Dense,Input,Dropout
from tensorflow.keras.models import Model
from sklearn.metrics import (classification_report,confusion_matrix,accuracy_score,
f1_score,recall_score,precision_score)
import warnings
warnings.filterwarnings("ignore")
df = pd.read_csv("/home/soft12/Downloads/sample_dataset/Website/Dataset/loan_approval_da taset.csv")
#check null values in dataset
df.isnull().sum()
label = LabelEncoder()
#convert categorical columns into numerical columns
for i in df.columns:
if df[i].dtype == 'object':
df[i] = label.fit_transform(df[i])
df.columns = df.columns.str.strip()
scaler = StandardScaler()
x = df.drop('loan_status',axis=1)
x = scaler.fit_transform(x)
y = df['loan_status']
#Split the train_test_data
X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=.2,random_state=42)
def ANN_model(input_shape):
# Input layer
inputs = Input(shape=(input_shape,))
# Hidden layers
layer1 = Dense(64, activation='relu')(inputs)
Dropout1 = Dropout(0.2)(layer1)
layer2 = Dense(32, activation='relu')(Dropout1)
Dropout2 = Dropout(0.2)(layer2)
# Output layer
output_layer = Dense(1, activation='sigmoid')(Dropout2)
# Build the model
ann_model = Model(inputs=inputs, outputs=output_layer)
# Compile the model with Adam optimizer and binary crossentropy loss function
ann_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return ann_model
model = ANN_model(X_train.shape[1])
model.fit(X_train,y_train,batch_size=2,epochs=10,validation_data=(X_test,y_test))
y_pred = model.predict(X_test)
y_pred = [1 if i>0.5 else 0 for i in y_pred]
print("___Performance_Metrics___\n")
print('Classification_Report:\n',classification_report(y_test, y_pred))
print('Confusion_Matrix:\n',confusion_matrix(y_test, y_pred))
print('Accuracy_Score: ',accuracy_score(y_test, y_pred))
print('F1_Score: ',f1_score(y_test, y_pred))
print('Recall_Score: ',recall_score(y_test, y_pred))
print('Precision_Score: ',precision_score(y_test, y_pred))