# Loan Eligibility Prediction using Machine Learning Models in Python

Predicting loan eligibility is a crucial part of the banking and finance sector. It is used by financial institutions, especially banks, to determine whether to approve a loan application. A number of variables are taken into consideration, including the applicant's income, credit history, loan amount, education, and employment situation.

In this post, we will demonstrate how to predict loan eligibility using Python and its machine learning modules. We'll introduce some machine learning models, going over their fundamental ideas and demonstrating how they can be used to generate predictions.

## Step 1: Understand the Problem

Predicting whether a loan will be accepted or not is the objective here. Therefore, we must divide this classification problem into two classes: Loan Approved and Loan Not Approved.

## Step 2: Data Preparation

Several criteria, including the applicant's gender, marital status, education, number of dependents, income, loan amount, credit history, and others, will be included in the dataset that we will be accessing from an open-source repository.

import pandas as pd

# Display the first 5 rows of the dataframe


The data is then cleaned, handled for missing values, transformed into numerical variables, and divided into feature (X) and target (y) datasets.

## Step 3: Implement Machine Learning Models

We will use the Logistic Regression, Decision Tree, and Random Forest machine learning models in this step.

### Example 1: Logistic Regression

A statistical approach for binary classification issues is logistic regression. The logistic function is used to model the likelihood of a particular class or occurrence.

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Split the data into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Create a Logistic Regression model
model = LogisticRegression()

# Train the model
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
print('Accuracy: ', accuracy_score(y_test, y_pred))


### Example 2: Decision Tree

An internal node represents a feature (or property), a branch represents a decision rule, and each leaf node indicates the outcome in a decision tree, which resembles a flowchart.

from sklearn.tree import DecisionTreeClassifier

# Create a Decision Tree model
model = DecisionTreeClassifier()

# Train the model
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
print('Accuracy: ', accuracy_score(y_test, y_pred))


### Example 3: Random Forest

A classification technique called Random Forest builds several decision trees during the training phase and outputs the class that corresponds to the categorization of the individual trees' modes.

from sklearn.ensemble import RandomForestClassifier

# Create a Random Forest model
model = RandomForestClassifier(n_estimators=100)

# Train the model
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
print('Accuracy: ', accuracy_score(y_test, y_pred))


## Step 4: Evaluate the Models

In this instance, accuracy serves as our evaluation metric. The proportion of accurate predictions to all input samples is shown below. Nevertheless, depending on the problem context, other measures including precision, recall, and F1 score could also be utilised.

## Conclusion

One typical use case in the banking and finance sector is loan eligibility prediction. In this article, we looked at how to forecast loan eligibility using Python and machine learning models. We put the Logistic Regression, Decision Tree, and Random Forest models into practise and assessed how well they worked.

Remember that analysing the data and selecting the appropriate model and assessment metric are the keys to developing a robust machine learning model. Continue to investigate more models and methods to enhance the forecast.

Updated on: 18-Jul-2023

343 Views 