Decision Tree Machine Learning Python Algo : Predict Loan Eligibility for Dream Housing Finance company

0 like 0 dislike
2.5k views

Predict Loan Eligibility for Dream Housing Finance company

Dream Housing Finance company deals in all kinds of home loans. They have presence across all urban, semi urban and rural areas. Customer first applies for home loan and after that company validates the customer eligibility for loan.

Company wants to automate the loan eligibility process (real time) based on customer detail provided while filling online application form. These details are Gender, Marital Status, Education, Number of Dependents, Income, Loan Amount, Credit History and others. To automate this process, they have provided a dataset to identify the customers segments that are eligible for loan amount so that they can specifically target these customers.

0 like 0 dislike
by (122 points)
0 like 0 dislike
by (130 points)
import pandas as pd

import numpy as np

import seaborn as sns

import matplotlib.pyplot as plt

import sklearn

df.shape

df.dtypes

df.corr()

a= df['Property_Area'].values
df.isnull().sum()
from sklearn.preprocessing import LabelEncoder

le=LabelEncoder()

df.Property_Area=le.fit_transform(df.Property_Area)

df.Loan_Status=le.fit_transform(df.Loan_Status)

newdf=df.replace(np.NAN,{'LoanAmount':100,'Loan_Amount_Term':360.0,'Credit_History':1.0})

newdf

newdf.isnull().sum()
sns.relplot(x='ApplicantIncome',y='LoanAmount',hue="Credit_History",data=newdf)

x=newdf.drop(['Loan_ID','Gender','Married','Dependents','Education','Self_Employed','Loan_Status'],axis='columns')

print(x)

y=newdf['Loan_Status']

print(y)

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)

print(len(x_train))

print(len(x_test))

from sklearn.tree import DecisionTreeClassifier

clf=DecisionTreeClassifier(random_state=5)

clf.fit(x_train,y_train)

y_pred=clf.predict(x_test)

y_pred

from sklearn.metrics import accuracy_score

Accuracy=accuracy_score(y_test,y_pred)

print("Accuracy is",Accuracy*100,'%')

from sklearn.metrics import confusion_matrix

cm=np.array(confusion_matrix(y_test,y_pred))

cm

from sklearn import tree

tree.plot_tree(clf)

plt.figure()

tree.plot_tree(clf,filled=True)

plt.savefig('tree.jpg',format='jpg',bbox_inches = "tight")

0 like 0 dislike
by (278 points)