Skip to content

Commit

Permalink
Merge pull request #689 from sanskritiagr/master
Browse files Browse the repository at this point in the history
Loan Prediction Project
  • Loading branch information
Niketkumardheeryan authored Jun 2, 2024
2 parents e780451 + b7922a2 commit 205728c
Show file tree
Hide file tree
Showing 7 changed files with 3,861 additions and 0 deletions.
2,742 changes: 2,742 additions & 0 deletions Loan-Prediction-main/Loan-Prediction-main/LoanPreds.ipynb

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions Loan-Prediction-main/Loan-Prediction-main/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Loan-Prediction
A machine learning model designed to predict whether a loan application will be approved or rejected based on the applicant's personal information and credit history. The model utilizes a dataset containing historical loan application data to train and validate its predictions.
## Dataset:
The dataset used for training and testing the model consists of various features that include:
1. Personal information such as gender, marital status, dependents, education, self-employed, and property area.
2. Financial information such as applicant income, co-applicant income, and credit history.
3. Loan-specific details such as loan amount, and loan term.
4. Target variable indicating whether the loan was approved or rejected.
## Dependencies:
Libraries used: Numpy, Pandas, Matplotlib, missingno, seaborn, scipy, sklearn.
## Visualization:
To gain insights into the dataset before building the model, I tried to visualize the model using some plots like:</br>
BarPlot(): I have plotted the crosstab for each class with the loan prediction class(output). </br>
Histplot(): Showing the distribution of the dataset. </br>
Heatmap(): Checked if our attributes are codependent on one another. If were so, we could have simply used any one of them but all our attributes are independent.</br>
Crosstab(): We observed that loan status is not dependent on gender since we have an almost equal ratio for male and female.</br>
Boxplot(): We observed that we have outliers that need to be taken care of.</br>
## Data Preprocessing
Treating NaN values: For the categorical attributes I used 'mode' to fill in the empty cells and for the numerical values, 'mean' was used.</br>
Treating Outliers: We simply dropped the values that were not in the range. </br>
SMOTE(): Increasing the data for the minority class.</br>
MinMaxScaler: Linearly scales them down into a fixed range. </br>
## Model Training
1. Linear Regression: Accuracy- 92.59%
2. KNN: Best Accuracy- 90.74%
3. SVC: Accuracy- 92.59%
4. Decision Tree: Best Accuracy- 92.59%
5. Random Forest: Best accuracy- 92.59%
6. Gradient Boosting: Best Accuracy- 85.19%
#### Finally I used MaxVoting to ensemble all the models and got final accuracy of 92.59%.

## After running app.py file
![image](https://github.com/sanskritiagr/ML-CaPsule/assets/96240350/71ff508e-7277-40b2-82e0-e7854b7c7b34)

53 changes: 53 additions & 0 deletions Loan-Prediction-main/Loan-Prediction-main/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# from collections.abc import Buffer
# from typing_extensions import Buffer
import numpy as np
from flask import Flask, request, render_template
import pickle

#Create an app object using the Flask class.
app = Flask(__name__)

#Load the trained model. (Pickle file)
model = pickle.load(open('models/model.pkl', 'rb'))

#Define the route to be home.
#The decorator below links the relative route of the URL to the function it is decorating.
#Here, home function is with '/', our root directory.
#Running the app sends us to index.html.
#Note that render_template means it looks for the file in the templates folder.

#use the route() decorator to tell Flask what URL should trigger our function.
@app.route('/')
def home():
return render_template('index.html')

#You can use the methods argument of the route() decorator to handle different HTTP methods.
#GET: A GET message is send, and the server returns data
#POST: Used to send HTML form data to the server.
#Add Post method to the decorator to allow for form submission.
#Redirect to /predict page with the output
@app.route('/predict',methods=['POST'])
def predict():

int_features = [float(x) for x in request.form.values()] #Convert string inputs to float.
features = [np.array(int_features)] #Convert to the form [[a, b]] for input to the model
prediction = model.predict(features) # features Must be in the form [[a, b]]
if(prediction[0] == 0):
output = "Loan Rejected"
else:
output = "Loan Approved"
# output = round(prediction[0], 2)

return render_template('index.html', prediction_text='Prediction: {}'.format(output))


#When the Python interpreter reads a source file, it first defines a few special variables.
#For now, we care about the __name__ variable.
#If we execute our code in the main program, like in our case here, it assigns
# __main__ as the name (__name__).
#So if we want to run our code right here, we can check if __name__ == __main__
#if so, execute it here.
#If we import this file (module) to another file then __name__ == app (which is the name of this python file).

if __name__ == "__main__":
app.run()
Loading

0 comments on commit 205728c

Please sign in to comment.