# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/KeshavAman/Bank-Loan-Default-Prediction/blob/main/Bank_Loan_Default_Predicition.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="dZpOVBWSGMbT"
# **Importing the libraries**

# + id="7Kz09-VAVGOB"
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas import plotting

# + [markdown] id="vBWRKABTGc51"
# **Importing dataset**

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="nKgH_7pcVYOO" outputId="8424a535-de13-4c62-b8ed-b8b669c61d60"
dataset = pd.read_csv("https://raw.githubusercontent.com/KeshavAman/file/main/Default_Fin.csv")
dataset.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="U8yhhfcBVifP" outputId="8bf8ac4a-0690-44b4-ddc8-399ecf717169"
dataset.describe().T

# + colab={"base_uri": "https://localhost:8080/"} id="IoWsnKQoVsPk" outputId="c7fc2cdb-f9cc-4256-da54-4eb9d4b0240f"
print("Rows and columns :",dataset.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="tlCqPrv6VzeZ" outputId="739ef43f-e11b-44e1-9849-afa456618fae"
dataset.isnull().sum

# + [markdown] id="QYVE_faUGhKG"
# **Checking and droping the null values**

# + colab={"base_uri": "https://localhost:8080/"} id="PO0I4sWoV-AI" outputId="9910175a-9e51-44c6-fcbe-c311a53611b7"
dataset = dataset.dropna(axis = 1)
dataset.shape

# + [markdown] id="i3XWeV-0GnIR"
# **Value count**

# + colab={"base_uri": "https://localhost:8080/"} id="jAywWse_WFtr" outputId="92ddab83-b638-4e07-de46-0752f1293b88"
dataset['Defaulted?'].value_counts()

# + [markdown] id="MWMMrb11GuXE"
# **Visualize the value count**

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="fDjFclCwWUte" outputId="dd7b452d-0637-4619-a8bf-27e466b0e124"
sns.countplot(dataset['Defaulted?'], label = 'count') 

# + colab={"base_uri": "https://localhost:8080/", "height": 350} id="99OboR85am-S" outputId="74f9c303-be2f-4faf-d154-eab92dbd4870"
sns.countplot(dataset['Employed'], label = 'count')

# + [markdown] id="VhyR0XwDGzLw"
# **Pairplot**

# + colab={"base_uri": "https://localhost:8080/", "height": 743} id="12CjBNEsWoGu" outputId="a81760a1-7151-433b-f1ec-46b761094641"
sns.pairplot(dataset, hue = 'Defaulted?')

# + [markdown] id="2qx5059xG207"
# **Heatmap**

# + colab={"base_uri": "https://localhost:8080/", "height": 449} id="ggtNiKN1XC0a" outputId="b5f6bdd1-7392-46d9-eec4-16c4c292560c"
plt.figure(figsize=(9,7))
sns.heatmap(dataset.corr(), annot = True)

# + [markdown] id="oy1z8aApG6TK"
# **Displot**

# + colab={"base_uri": "https://localhost:8080/", "height": 403} id="DqDoj7kJXTK3" outputId="fc1cffef-231c-431e-ee17-36137a67c9c0"
plt.figure(figsize=(10,8))
sns.displot(dataset, x='Bank Balance', col = 'Defaulted?', kde = True)

# + colab={"base_uri": "https://localhost:8080/", "height": 403} id="-SgxLko_YBd2" outputId="00be7d4c-1afa-49bd-b1a5-a661f2857528"
plt.figure(figsize=(10,8))
sns.displot(dataset, x = 'Annual Salary', col = 'Defaulted?', kde = True)

# + [markdown] id="roW5Nc2QG_He"
# **Andrews Curves**

# + colab={"base_uri": "https://localhost:8080/", "height": 483} id="Il89whSfYNnE" outputId="2b147395-97c2-439c-d3cc-0a7c3e2d698b"
fig = plt.figure(figsize = (12,8))
plotting.andrews_curves(dataset.drop('Index', axis =1),'Defaulted?')
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 483} id="7GdlACBRZ3Mu" outputId="ce9faeff-13cd-4b61-9a73-1b030b7ba968"
fig = plt.figure(figsize = (12,8))
plotting.andrews_curves(dataset.drop('Index', axis =1 ),'Employed')
plt.show()

# + [markdown] id="z3T4sv31HEKS"
# **Kdeplot**

# + colab={"base_uri": "https://localhost:8080/", "height": 561} id="dGReDzDM-0oq" outputId="6af3219d-8bcc-4f73-a9ef-4697108b111e"
fig = plt.figure(figsize=(10,8))

sns.kdeplot(dataset[dataset['Defaulted?']==1]['Bank Balance'])
sns.kdeplot(dataset[dataset['Defaulted?']==0]['Bank Balance'])
fig.legend(labels=['Defaulted','Not Defaulted'])
plt.title('Bank Balance')
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 561} id="7CoceI78Ap2P" outputId="b6e07596-94b5-4237-f27f-7e72b2d29630"
fig = plt.figure(figsize=(10,8))

sns.kdeplot(dataset[dataset['Defaulted?']==1]['Annual Salary'])
sns.kdeplot(dataset[dataset['Defaulted?']==0]['Annual Salary'])
fig.legend(labels=['Defaulted','Not Defaulted'])
plt.title('Bank Balance')
plt.show()

# + [markdown] id="ofKOv68HHIHX"
# **Preprocessing of dataset**

# + id="97pVyCxNFOKt"
from sklearn.preprocessing import MinMaxScaler

feature_cols = ['Employed', 'Bank Balance', 'Annual Salary']
sc = MinMaxScaler()
dataset[feature_cols] = sc.fit_transform(dataset[feature_cols])

# + [markdown] id="jR9oHDRuHSbM"
# **Splitting of dataset**

# + id="kL6wttFBclJM"
from sklearn.model_selection import train_test_split

X = dataset[feature_cols]
y = dataset['Defaulted?']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state =4,stratify=y)

# + [markdown] id="tK9dwYgaHXEV"
# **Dimensions**

# + colab={"base_uri": "https://localhost:8080/"} id="GRz5FllNeV_d" outputId="91b049df-07af-472d-caac-8c95232d0a05"
print('X_train shape : ',X_train.shape)
print('X_test shape : ',X_test.shape)
print('y_train shape : ',y_train.shape)
print('y_test shape : ',y_test.shape)

# + [markdown] id="YSSSJaGiHayk"
# **Using KNN**

# + id="-XZQtNFKfZvS"
from sklearn.neighbors import KNeighborsClassifier

KNN = KNeighborsClassifier(n_neighbors = 5)
KNN.fit(X_train, y_train)
y_pred = KNN.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="6XLn_So-gUsj" outputId="ecde6cc3-d2ba-4dc1-dd11-95b0056555d2"
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score, accuracy_score

print('Accuracy Score :',accuracy_score(y_test, y_pred)*100)
print('Precision Score :',precision_score(y_test, y_pred)*100)
print('Recall Score :',recall_score(y_test, y_pred)*100)
print('F1 Score :',f1_score(y_test, y_pred)*100)
print('Confusion Matrix :',confusion_matrix(y_test, y_pred), sep='\n')

# + [markdown] id="p566jYVnHfPK"
# **Using Logistic Regression**

# + id="rk8lhij0hgoN"
from sklearn.linear_model import LogisticRegression

LR = LogisticRegression()
LR.fit(X_train, y_train)
y_pred = LR.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="D7hTiXM6D0n0" outputId="ac0d9717-0cae-4571-cdbc-5b8f60fe6576"
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score, accuracy_score

print('Accuracy Score :',accuracy_score(y_test, y_pred)*100)
print('Precision Score :',precision_score(y_test, y_pred)*100)
print('Recall Score :',recall_score(y_test, y_pred)*100)
print('F1 Score :',f1_score(y_test, y_pred)*100)
print('Confusion Matrix :',confusion_matrix(y_test, y_pred), sep='\n')

# + [markdown] id="Yoq1XJKXHmiy"
# **Using Random Forest Classifier**

# + id="h32MlBMvD7md"
from sklearn.ensemble import RandomForestClassifier

RFC = RandomForestClassifier(n_estimators = 100)
RFC.fit(X_train,y_train)
y_pred = RFC.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="oAfQXYQREi4z" outputId="d5acb984-513c-4c29-ccc8-f3500d50d974"
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score, accuracy_score

print('Accuracy Score :',accuracy_score(y_test, y_pred)*100)
print('Precision Score :',precision_score(y_test, y_pred)*100)
print('Recall Score :',recall_score(y_test, y_pred)*100)
print('F1 Score :',f1_score(y_test, y_pred)*100)
print('Confusion Matrix :',confusion_matrix(y_test, y_pred), sep='\n')

# + [markdown] id="nY0yWKtIHvEQ"
# **Using Naive Bayes ( GaussianNB )**

# + id="Lq0pPvQsEk4W"
from sklearn.naive_bayes import GaussianNB

NB = GaussianNB()
NB.fit(X_train,y_train)
y_pred = NB.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="4AuAAYv_FKBC" outputId="c065499f-c21e-4fa9-b74b-425611c9ae3c"
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score, accuracy_score

print('Accuracy Score :',accuracy_score(y_test, y_pred)*100)
print('Precision Score :',precision_score(y_test, y_pred)*100)
print('Recall Score :',recall_score(y_test, y_pred)*100)
print('F1 Score :',f1_score(y_test, y_pred)*100)
print('Confusion Matrix :',confusion_matrix(y_test, y_pred), sep='\n')

# + [markdown] id="OQq0uG97H5lz"
# **Using Decision Tree Classifier**

# + id="ErkjauLGFLpM"
from sklearn.tree import DecisionTreeClassifier

DTC = DecisionTreeClassifier(criterion = 'entropy',random_state = 0)
DTC.fit(X_train,y_train)
y_pred = DTC.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="oFRYcMZoGGqh" outputId="a4610718-5927-41e9-c080-812eb398cf1e"
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score, accuracy_score

print('Accuracy Score :',accuracy_score(y_test, y_pred)*100)
print('Precision Score :',precision_score(y_test, y_pred)*100)
print('Recall Score :',recall_score(y_test, y_pred)*100)
print('F1 Score :',f1_score(y_test, y_pred)*100)
print('Confusion Matrix :',confusion_matrix(y_test, y_pred), sep='\n')

StarcoderdataJupyterScriptsDedupFiltered