<jupyter_start><jupyter_text>**Importing the libraries**<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas import plotting<jupyter_output><empty_output><jupyter_text>**Importing dataset**<jupyter_code>dataset = pd.read_csv("https://raw.githubusercontent.com/KeshavAman/file/main/Default_Fin.csv")
dataset.head()
dataset.describe().T
print("Rows and columns :",dataset.shape)
dataset.isnull().sum<jupyter_output><empty_output><jupyter_text>**Checking and droping the null values**<jupyter_code>dataset = dataset.dropna(axis = 1)
dataset.shape<jupyter_output><empty_output><jupyter_text>**Value count**<jupyter_code>dataset['Defaulted?'].value_counts()<jupyter_output><empty_output><jupyter_text>**Visualize the value count**<jupyter_code>sns.countplot(dataset['Defaulted?'], label = 'count') 
sns.countplot(dataset['Employed'], label = 'count')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>**Pairplot**<jupyter_code>sns.pairplot(dataset, hue = 'Defaulted?')<jupyter_output><empty_output><jupyter_text>**Heatmap**<jupyter_code>plt.figure(figsize=(9,7))
sns.heatmap(dataset.corr(), annot = True)<jupyter_output><empty_output><jupyter_text>**Displot**<jupyter_code>plt.figure(figsize=(10,8))
sns.displot(dataset, x='Bank Balance', col = 'Defaulted?', kde = True)
plt.figure(figsize=(10,8))
sns.displot(dataset, x = 'Annual Salary', col = 'Defaulted?', kde = True)<jupyter_output><empty_output><jupyter_text>**Andrews Curves**<jupyter_code>fig = plt.figure(figsize = (12,8))
plotting.andrews_curves(dataset.drop('Index', axis =1),'Defaulted?')
plt.show()
fig = plt.figure(figsize = (12,8))
plotting.andrews_curves(dataset.drop('Index', axis =1 ),'Employed')
plt.show()<jupyter_output><empty_output><jupyter_text>**Kdeplot**<jupyter_code>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()
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()<jupyter_output><empty_output><jupyter_text>**Preprocessing of dataset**<jupyter_code>from sklearn.preprocessing import MinMaxScaler

feature_cols = ['Employed', 'Bank Balance', 'Annual Salary']
sc = MinMaxScaler()
dataset[feature_cols] = sc.fit_transform(dataset[feature_cols])<jupyter_output><empty_output><jupyter_text>**Splitting of dataset**<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>**Dimensions**<jupyter_code>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)<jupyter_output>X_train shape :  (8000, 3)
X_test shape :  (2000, 3)
y_train shape :  (8000,)
y_test shape :  (2000,)<jupyter_text>**Using KNN**<jupyter_code>from sklearn.neighbors import KNeighborsClassifier

KNN = KNeighborsClassifier(n_neighbors = 5)
KNN.fit(X_train, y_train)
y_pred = KNN.predict(X_test)
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')<jupyter_output>Accuracy Score : 97.05
Precision Score : 59.523809523809526
Recall Score : 37.3134328358209
F1 Score : 45.87155963302752
Confusion Matrix :
[[1916   17]
 [  42   25]]<jupyter_text>**Using Logistic Regression**<jupyter_code>from sklearn.linear_model import LogisticRegression

LR = LogisticRegression()
LR.fit(X_train, y_train)
y_pred = LR.predict(X_test)
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')<jupyter_output>Accuracy Score : 97.39999999999999
Precision Score : 89.47368421052632
Recall Score : 25.37313432835821
F1 Score : 39.53488372093024
Confusion Matrix :
[[1931    2]
 [  50   17]]<jupyter_text>**Using Random Forest Classifier**<jupyter_code>from sklearn.ensemble import RandomForestClassifier

RFC = RandomForestClassifier(n_estimators = 100)
RFC.fit(X_train,y_train)
y_pred = RFC.predict(X_test)
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')<jupyter_output>Accuracy Score : 97.2
Precision Score : 62.7906976744186
Recall Score : 40.298507462686565
F1 Score : 49.090909090909086
Confusion Matrix :
[[1917   16]
 [  40   27]]<jupyter_text>**Using Naive Bayes ( GaussianNB )**<jupyter_code>from sklearn.naive_bayes import GaussianNB

NB = GaussianNB()
NB.fit(X_train,y_train)
y_pred = NB.predict(X_test)
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')<jupyter_output>Accuracy Score : 97.1
Precision Score : 65.51724137931035
Recall Score : 28.35820895522388
F1 Score : 39.583333333333336
Confusion Matrix :
[[1923   10]
 [  48   19]]<jupyter_text>**Using Decision Tree Classifier**<jupyter_code>from sklearn.tree import DecisionTreeClassifier

DTC = DecisionTreeClassifier(criterion = 'entropy',random_state = 0)
DTC.fit(X_train,y_train)
y_pred = DTC.predict(X_test)
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')<jupyter_output>Accuracy Score : 96.0
Precision Score : 40.298507462686565
Recall Score : 40.298507462686565
F1 Score : 40.298507462686565
Confusion Matrix :
[[1893   40]
 [  40   27]]
{'fraction_non_alphanumeric': 0.1420332618025751, 'fraction_numerical': 0.06209763948497854, 'mean_word_length': 9.145578231292516, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 66, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '190276', 'license': 'Apache-2.0', 'n_tokens_mistral': 3113, 'n_tokens_neox': 2755, 'n_words': 489, 'path': 'Bank_Loan_Default_Predicition.ipynb', 'repo_name': 'KeshavAman/Bank-Loan-Default-Prediction', 'size': '7456'}