-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoan_Prediction.py
317 lines (241 loc) · 13.9 KB
/
Loan_Prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import pickle
import streamlit as st
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.offline as pyo
import plotly.graph_objs as go
import matplotlib.pyplot as plt
import plotly.express as px
from streamlit_option_menu import option_menu
import streamlit.components.v1 as html
import numpy as np
import plotly.express as px
import io
# # Import db_fxn
# from db_fxn import create_usertable, add_userdata, login_user, view_all_users
# DB Management
import sqlite3
conn = sqlite3.connect('data.db')
c = conn.cursor()
def create_usertable():
c.execute('CREATE TABLE IF NOT EXISTS userstable(username TEXT,password TEXT)')
def add_userdata(username,password):
c.execute('INSERT INTO userstable(username,password) VALUES (?,?)',(username,password))
conn.commit()
def login_user(username,password):
c.execute('SELECT * FROM userstable WHERE username =? AND password = ?',(username,password))
data = c.fetchall()
return data
def view_all_users():
c.execute('SELECT * FROM userstable')
data = c.fetchall()
return data
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_score
loan_dataset =pd.read_csv(r'loan.csv')
loan_dataset = loan_dataset.dropna()
loan_dataset.replace({"Loan_Status":{'N':0,'Y':1}},inplace=True)
loan_dataset = loan_dataset.replace(to_replace='3+', value=4)
loan_dataset.replace({'Married':{'No':0,'Yes':1},'Gender':{'Male':1,'Female':0},'Self_Employed':{'No':0,'Yes':1},
'Property_Area':{'Rural':0,'Semiurban':1,'Urban':2},'Education':{'Graduate':1,'Not Graduate':0}},inplace=True)
X = loan_dataset[['Gender', 'Married', 'ApplicantIncome', 'LoanAmount', 'Credit_History']]
y = loan_dataset.Loan_Status
from sklearn.model_selection import train_test_split
x_train, x_cv, y_train, y_cv = train_test_split(X,y, test_size = 0.2, random_state = 10)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(max_depth=4, random_state = 10)
model.fit(x_train, y_train)
from sklearn.metrics import accuracy_score
pred_cv = model.predict(x_cv)
accuracy_score(y_cv,pred_cv)
pred_train = model.predict(x_train)
accuracy_score(y_train,pred_train)
pickle_out = open(r"C:\Python_GUI\Loan_Prediction_Machine\classifier.pkl", mode = "wb")
pickle.dump(model, pickle_out)
pickle_out.close()
# loading the trained model
pickle_in = open('classifier.pkl', 'rb')
classifier = pickle.load(pickle_in)
@st.cache()
# defining the function which will make the prediction using the data which the user inputs
def prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History):
# Pre-processing user input
if Gender == "Male":
Gender = 0
else:
Gender = 1
if Married == "Single":
Married = 0
else:
Married = 1
if Credit_History == "Outstanding Debts":
Credit_History = 0
else:
Credit_History = 1
# if ApplicantIncome <= 20000.00 & ApplicantIncome >= 250000.00:
# ApplicantIncome = 0
# else:
# ApplicantIncome = 1
LoanAmount = LoanAmount / 1000
# Making predictions
prediction = classifier.predict(
[[Gender, Married, ApplicantIncome, LoanAmount, Credit_History]])
if prediction == 0:
pred = 'Rejected'
else:
pred = 'Approved'
return pred
# this is the main function that is define the webpage
def main():
st.write('')
# Add Option Menu to Sidebar
# st.title('App information')
menu = ["Home","About", "login", "Contact"]
choose = st.sidebar.selectbox("Menu", menu)
# Create App Pages
if choose == "Home":
st.title('Welcome')
st.write("Thank you for visiting, my name is Jonathan Pollyn, and this web application is inspired by my capstone project for my master's degree program with Grand Canyon University. I currently work as a data engineer and am passionate about databases, Business Intelligence, and data science.")
st.write(' I have developed enterprise-level Business Intelligence solutions for various industries, focusing on performance optimization, pattern recognition, efficient analysis of business processes, and interactive visualizations. As part of my program with GCU, I have been exposed to various learning tools that have strengthened my knowledge and helped me to understand the future of data science.')
# st.write('If you have any questions, do not hesitate to get in touch with me via email at j.pollyn@gmail.com')
# About page
elif choose == "About":
st.title('Loan prediction system')
st.write(('This project aims to create a "Data science product" such as a "Loan prediction system" for the bank. The product will automate the loan approval process as it focuses on reducing the manual time and effort involved in the loan approval process. This project evaluation measure or acceptance criteria are accurate and reduce false positives. Approving a loan for an applicant without eligibility and the capability to repay the loan will pose a severe challenge for the bank.'))
st.write(('The main acceptance criteria for this data science project are to increase accuracy and reduce the false positive rate. In addition to the final product, the project includes a user manual to work on this system. This manual is helpful for the users to understand the product thoroughly, how it works, and eligible input and expected output from the data product.'))
st.write(( 'The main focus of the loan prediction system is to predict whether an applicant can repay the loan amount. To predict that, it must process an applicantloan application. Machine Learning predictive model will process the application of an applicant. Data from the application will be passed as input to the model.'))
elif choose == "login" :
st.subheader('Login')
username = st.sidebar.text_input('User Name')
password = st.sidebar.text_input('Password', type='password')
if st.sidebar.checkbox('Login'):
# if password == '12345':
create_usertable()
result = login_user(username, password)
if result:
st.success('Logged in as {}'.format(username))
task = st.selectbox('Task', ['Predict a load Application' , 'Data Exploration'])
if task == 'Predict a load Application':
st.subheader('Predict your loan eligibility')
def main():
# front end elements of the web page
html_temp = """
<div style ="background-color:blue;padding:13px">
<h1 style ="color:black;text-align:center;">Loan Prediction system</h1>
</div>
"""
# display the front end aspect
st.markdown(html_temp, unsafe_allow_html = True)
# following lines create boxes in which user can enter data required to make prediction
Gender = st.selectbox('Gender',("Male","Female"))
Married = st.selectbox('Marital Status',("Single","Married"))
ApplicantIncome = st.number_input("Applicants monthly income")
LoanAmount = st.number_input("Total loan amount")
Credit_History = st.selectbox('Credit_History',("Outstanding Debts","No Outstanding Debts"))
result =""
# when 'Predict' is clicked, make the prediction and store it
if st.button("Predict"):
result = prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History)
st.success('Your loan is {}'.format(result))
print(LoanAmount)
elif task == 'Data Exploration':
admin_user = 'admin1'
if admin_user:
if username == 'admin1':
st.subheader('Perform your analysis')
st.title("Data Exploration for Loan Application")
st.markdown('This section of the code is designated for performing various exploratory data analysis. You can upload your own file for the analysis. Also check below for more options of analysis')
loan_file = st.file_uploader('Select Your Local Loan historical CSV (default provided)')
if loan_file is not None:
loan_df = pd.read_csv(loan_file)
st.write(loan_dataset.head())
st.write(loan_dataset.describe())
else:
loan_df= pd.read_csv('loan.csv')
st.write(loan_dataset.head())
st.write(loan_dataset.describe())
st.subheader('Plotly Histogram Chart')
selected_x_var = st.selectbox('What do want the x variable to be?', ['Gender','Self_Employed','Education'])
fig = px.histogram(loan_df[selected_x_var])
st.plotly_chart(fig)
st.subheader('Plotly Bar Chart')
selected_x_bar = st.selectbox('Select from the avaiable x variable: ', ['Married','Property_Area'])
selected_y_bar = st.selectbox('Select from the avaiable y variable', ['Dependents','ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term'])
data = [go.Bar(
x=loan_df[selected_x_bar],
y=loan_df[selected_y_bar]
)]
layout = go.Layout(
title="Bar chart for: {},{}".format(selected_x_bar,selected_y_bar)
)
fig = go.Figure(data=data, layout=layout)
st.plotly_chart(fig)
else:
st.warning('Sorry, you are not permitted to view this page. Only admin users can perform data exploration. Please get in touch with Jonathan Pollyn for more information.')
else:
st.warning('Incorrect Username or Password')
# elif choose == 'SignUp':
# new_user = st.text_input('Username')
# new_password = st.text_input('Password', type='password')
# if st.button('Signup'):
# create_usertable()
# add_userdata(new_user, new_password)
# st.success('Congratulation, you have successfully created an account')
# st.info('Go to the Login Menu to login')
elif choose == 'Contact':
st.write('If you do not have a valid login username and password, please get in touch with the developer of this application by email at j.pollyn@gmail.com. Jonathan Pollyn will give secure login credentials that you can use to log in and access the application.')
# elif choose == 'Data Exploration':
# st.title("Data Exploration for Loan Application")
# st.markdown('This section of the code is designated for performing various exploratory data analysis. You can upload your own file for the analysis. Also check below for more options of analysis')
# loan_file = st.file_uploader('Select Your Local Loan historical CSV (default provided)')
# if loan_file is not None:
# loan_df = pd.read_csv(loan_file)
# st.write(loan_dataset.head())
# st.write(loan_dataset.describe())
# else:
# loan_df= pd.read_csv('loan.csv')
# st.write(loan_dataset.head())
# st.write(loan_dataset.describe())
# st.subheader('Plotly Histogram Chart')
# selected_x_var = st.selectbox('What do want the x variable to be?', ['Gender','Self_Employed','Education'])
# fig = px.histogram(loan_df[selected_x_var])
# st.plotly_chart(fig)
# st.subheader('Plotly Bar Chart')
# selected_x_bar = st.selectbox('Select from the avaiable x variable: ', ['Married','Property_Area'])
# selected_y_bar = st.selectbox('Select from the avaiable y variable', ['Dependents','ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term'])
# data = [go.Bar(
# x=loan_df[selected_x_bar],
# y=loan_df[selected_y_bar]
# )]
# layout = go.Layout(
# title="Bar chart for: {},{}".format(selected_x_bar,selected_y_bar)
# )
# fig = go.Figure(data=data, layout=layout)
# st.plotly_chart(fig)
# elif choose == 'Predict a Loan':
# # this is the main function that is define the webpage
# def main():
# # front end elements of the web page
# html_temp = """
# <div style ="background-color:blue;padding:13px">
# <h1 style ="color:black;text-align:center;">Loan Prediction system</h1>
# </div>
# """
# # display the front end aspect
# st.markdown(html_temp, unsafe_allow_html = True)
# # following lines create boxes in which user can enter data required to make prediction
# Gender = st.selectbox('Gender',("Male","Female"))
# Married = st.selectbox('Marital Status',("Single","Married"))
# ApplicantIncome = st.number_input("Applicants monthly income")
# LoanAmount = st.number_input("Total loan amount")
# Credit_History = st.selectbox('Credit_History',("Outstanding Debts","No Outstanding Debts"))
# result =""
# # when 'Predict' is clicked, make the prediction and store it
# if st.button("Predict"):
# result = prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History)
# st.success('Your loan is {}'.format(result))
# print(LoanAmount)
if __name__=='__main__':
main()