0% found this document useful (0 votes)
23 views7 pages

Loan Eligibility Prediction Model

The document outlines a task for predicting loan eligibility using logistic regression, detailing objectives, required tools, and a dataset with features such as income and credit score. It describes a step-by-step procedure for data preprocessing, feature engineering, and model building, including handling missing values, scaling features, and evaluating model accuracy. The provided Python code implements these steps, demonstrating a complete workflow from data preparation to model evaluation.

Uploaded by

Subramanian R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Loan Eligibility Prediction Model

The document outlines a task for predicting loan eligibility using logistic regression, detailing objectives, required tools, and a dataset with features such as income and credit score. It describes a step-by-step procedure for data preprocessing, feature engineering, and model building, including handling missing values, scaling features, and evaluating model accuracy. The provided Python code implements these steps, demonstrating a complete workflow from data preparation to model evaluation.

Uploaded by

Subramanian R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TASK : 01

LOAN ELIGIBILITY PREDICTION USING LOGISTIC REGRESSION

Objec ve:

 To understand the process of data preprocessing for machine learning.

 To implement feature engineering techniques.

 To build and evaluate a Logis c Regression model for loan eligibility predic on.

Tools/So ware Required:

 Python 3.x

 Pandas

 Scikit-learn (sklearn)

 Anaconda (recommended for environment management)

Dataset:

A simplified loan dataset is provided in the code. This dataset includes the following
features:

 income: Applicant's income.

 loan_amount: Requested loan amount.

 credit_score: Applicant's credit score.

 loan_approved: Loan approval status (1: Approved, 0: Not Approved).

Procedure:

 Data Preprocessing:

1. Load the Data:

o Create a Pandas DataFrame from the provided sample dataset.

o Print the original DataFrame to observe the raw data.

2. Handle Missing Values:

o Iden fy missing values in the 'income' column.

o Use SimpleImputer from scikit-learn to replace the missing values with


the mean of the 'income' column.
o Print the DataFrame a er imputa on to verify the changes.

3. Scale Numerical Features:

o Iden fy the numerical features ('income', 'loan_amount', 'credit_score').

o Use StandardScaler from scikit-learn to scale these features to have zero


mean and unit variance.

o Print the DataFrame a er scaling to observe the transformed data.

 Feature Engineering:

1. Create a New Feature (Total Risk):

o Create a new feature named 'total_risk' by calcula ng the ra o of


'loan_amount' to 'credit_score'.

o Print the DataFrame a er adding the new feature to verify its crea on.

 Machine Learning Model Building and Evalua on:

1. Prepare Data for Model:

o Define the features (X) as the 'income', 'loan_amount', 'credit_score', and


'total_risk' columns.

o Define the target variable (y) as the 'loan_approved' column.

2. Split Data:

o Use train_test_split from scikit-learn to divide the data into training and
tes ng sets. Allocate 80% of the data for training and 20% for tes ng. Set
random_state=42 for reproducibility.

o Print the shapes of the training and tes ng sets to confirm the split.

3. Train the Model:

o Create a Logis cRegression model from scikit-learn.

o Train the model using the training data (X_train, y_train).

4. Make Predic ons:

o Use the trained model to make predic ons on the tes ng data (X_test).

o Print the predic ons (y_pred).

5. Evaluate the Model:


o Use accuracy_score from scikit-learn to calculate the accuracy of the
model's predic ons by comparing them to the actual tes ng labels
(y_test).

o Print the calculated accuracy.

Program :

Python

import pandas as pd

from sklearn.model_selec on import train_test_split

from sklearn.linear_model import Logis cRegression

from [Link] import accuracy_score

from [Link] import StandardScaler

from [Link] import SimpleImputer

# 1. Sample Data

data = {

'income': [5000, 6000, None, 7000, 4500],

'loan_amount': [100, 150, 80, 200, 120],

'credit_score': [700, 750, 650, 800, 680],

'loan_approved': [1, 1, 0, 1, 0]

df = [Link](data)

print("Original DataFrame:\n", df)

# 2. Data Preprocessing

imputer = SimpleImputer(strategy='mean')

df['income'] = imputer.fit_transform(df[['income']])

print("\nDataFrame a er Impu ng Missing Income:\n", df)

scaler = StandardScaler()
numerical_cols = ['income', 'loan_amount', 'credit_score']

df[numerical_cols] = scaler.fit_transform(df[numerical_cols])

print("\nDataFrame a er Scaling Numerical Columns:\n", df)

# 3. Feature Engineering

df['total_risk'] = df['loan_amount'] / df['credit_score']

print("\nDataFrame a er Feature Engineering (total_risk):\n", df)

# 4. Model Building and Evalua on

X = df[['income', 'loan_amount', 'credit_score', 'total_risk']]

y = df['loan_approved']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print("\nTraining Data (X_train):\n", X_train)

print("\nTes ng Data (X_test):\n", X_test)

print("\nTraining Labels (y_train):\n", y_train)

print("\nTes ng Labels (y_test):\n", y_test)

model = Logis cRegression()

model.fit(X_train, y_train)

y_pred = [Link](X_test)

print("\nPredic ons (y_pred):\n", y_pred)

accuracy = accuracy_score(y_test, y_pred)

print("\nAccuracy:", accuracy)
Output :

Original DataFrame:

income loan_amount credit_score loan_approved

0 5000.0 100 700 1

1 6000.0 150 750 1

2 NaN 80 650 0

3 7000.0 200 800 1

4 4500.0 120 680 0

DataFrame a er Impu ng Missing Income:

income loan_amount credit_score loan_approved

0 5000.0 100 700 1

1 6000.0 150 750 1

2 5625.0 80 650 0

3 7000.0 200 800 1

4 4500.0 120 680 0

DataFrame a er Scaling Numerical Columns:

income loan_amount credit_score loan_approved

0 -0.727778 -0.715097 -0.301084 1

1 0.436667 0.476731 0.639803 1

2 0.000000 -1.191828 -1.241971 0

3 1.601112 1.668560 1.580691 1

4 -1.310001 -0.238366 -0.677439 0


DataFrame a er Feature Engineering (total_risk):

income loan_amount credit_score loan_approved total_risk

0 -0.727778 -0.715097 -0.301084 1 2.375075

1 0.436667 0.476731 0.639803 1 0.745121

2 0.000000 -1.191828 -1.241971 0 0.959626

3 1.601112 1.668560 1.580691 1 1.055589

4 -1.310001 -0.238366 -0.677439 0 0.351863

Training Data (X_train):

Income loan_amount credit_score total_risk

4 -1.310001 -0.238366 -0.677439 0.351863

2 0.000000 -1.191828 -1.241971 0.959626

0 -0.727778 -0.715097 -0.301084 2.375075

3 1.601112 1.668560 1.580691 1.055589

Tes ng Data (X_test):

income loan_amount credit_score total_risk

1 0.436667 0.476731 0.639803 0.745121

Training Labels (y_train):

4 0

2 0

0 1

3 1

Name: loan_approved, dtype: int64


Tes ng Labels (y_test):

1 1

Name: loan_approved, dtype: int64

Predic ons (y_pred):

[1]

Accuracy: 1.0

Results:

 Record the original DataFrame.

 Record the DataFrame a er each preprocessing step.

 Record the shapes of the training and tes ng sets.

 Record the predic ons made by the model.

 Record the accuracy of the model's predic ons.

Common questions

Powered by AI

Using a small dataset can lead to overfitting, where the model learns noise rather than the signal, impacting its generalization. It might also result in poor estimates of model parameters. Techniques like cross-validation, using regularization, or augmenting the dataset through synthetic data generation can mitigate these issues .

The essential steps in data preprocessing include loading the data into a Pandas DataFrame, identifying and handling missing values using SimpleImputer to replace them with the mean of the column, and scaling numerical features ('income', 'loan_amount', 'credit_score') using StandardScaler to have zero mean and unit variance .

Enhancing the model can involve techniques such as expanding features with domain-driven transformations, applying regularization to mitigate overfitting, ensemble methods like bagging for robustness, and exploring non-linear transformations or interactions among numerical features to capture complex patterns that linear models might miss .

Logistic regression is effective for binary classification tasks like loan eligibility due to its simplicity and interpretability. It provides an accuracy measure, which, in the provided scenario, was perfect (1.0) on the test data; however, this might reflect overfitting due to small dataset size. Its coefficients offer insights into feature importance, beneficial for explanatory tasks .

Feature engineering enhances model performance by creating new, informative features. In this context, a new feature 'total_risk' is created as a ratio of 'loan_amount' to 'credit_score', potentially capturing a more nuanced risk profile of the applicant that aids in better loan eligibility prediction .

The train-test split allows for a clear distinction between the dataset used for training and evaluation, ensuring that the model’s performance is validated against an unseen dataset. Allocating 80% for training and 20% for testing helps in estimating the model's generalization ability and prevents overfitting on the training data .

Scaling numerical features using StandardScaler ensures that features like 'income', 'loan_amount', and 'credit_score' have the same influence on the logistic model, as it scales them to have a mean of zero and unit variance. This process prevents features with larger magnitudes from disproportionately affecting the model and improves convergence and model performance .

Logistic Regression predicts loan approval status by modeling the probability of an outcome (e.g., loan approval) given input features such as 'income', 'loan_amount', 'credit_score', and 'total_risk'. It estimates coefficients for these features and applies the logistic function to predict binary outcomes, in this case, approval (1) or non-approval (0).

Imputing missing values helps in maintaining data integrity and consistency, essential for model accuracy. Without imputation, missing values could lead to biased results or could even prevent certain data algorithms from executing properly. SimpleImputer is used to replace missing 'income' values with the mean, ensuring no loss of information and stable model training .

The 'total risk' ratio reflects the burden of the loan relative to the applicant's creditworthiness. A higher ratio might indicate higher risk, while a lower ratio could suggest a safer loan. This derived feature gives deeper insight into applicant risks beyond standalone numerical values, contributing significantly to prediction accuracy .

You might also like