Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Untitled32.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1fKN0jOoDSOaUCMAAoxNUnSUd-HPcRXNZ
|
8 |
+
"""
|
9 |
+
from sklearn.tree import DecisionTreeClassifier
|
10 |
+
from sklearn.ensemble import RandomForestClassifier
|
11 |
+
from lightgbm import LGBMClassifier
|
12 |
+
from sklearn.model_selection import train_test_split
|
13 |
+
|
14 |
+
import streamlit as st
|
15 |
+
import pandas as pd
|
16 |
+
import matplotlib.pyplot as plt
|
17 |
+
import numpy as np
|
18 |
+
|
19 |
+
data = pd.read_csv('archive (8).zip')
|
20 |
+
data.head()
|
21 |
+
|
22 |
+
to_use_cols = ['as_of_year', 'county_name', 'applicant_sex_name', 'action_taken_name', 'loan_amount_000s', 'applicant_income_000s', 'state_name', 'property_type_name', 'loan_type_name']
|
23 |
+
|
24 |
+
data_reduced = data[to_use_cols]
|
25 |
+
data_reduced.head()
|
26 |
+
|
27 |
+
data_reduced.shape
|
28 |
+
|
29 |
+
data_no_na = data_reduced.dropna()
|
30 |
+
|
31 |
+
data_no_na.shape
|
32 |
+
|
33 |
+
data_no_na.action_taken_name.value_counts()
|
34 |
+
|
35 |
+
succeeded = ['Loan originated', 'Loan purchased by the institution', ]
|
36 |
+
failed = ['Application approved but not accepted', 'Preapproval request denied by financial institution', 'Application denied by financial institution', 'Preapproval request approved but not accepted']
|
37 |
+
user_error = ['File closed for incompleteness', 'Application withdrawn by applicant', ]
|
38 |
+
|
39 |
+
mapped = {tuple(succeeded): 1, tuple(failed): 2, tuple(user_error): 3}
|
40 |
+
|
41 |
+
def mapped(x):
|
42 |
+
if x in succeeded:
|
43 |
+
return 1
|
44 |
+
elif x in failed:
|
45 |
+
return 0
|
46 |
+
else:
|
47 |
+
return 2
|
48 |
+
|
49 |
+
data_no_na.action_taken_name = data_no_na.action_taken_name.apply(mapped)
|
50 |
+
|
51 |
+
data_no_na.loan_type_name.value_counts()
|
52 |
+
|
53 |
+
mapped_type = {
|
54 |
+
'Conventional': 0,
|
55 |
+
'FHA-insured': 1,
|
56 |
+
'VA-guaranteed':2,
|
57 |
+
'FSA/RHS-guaranteed':3
|
58 |
+
}
|
59 |
+
|
60 |
+
data_no_na.loan_type_name.apply(lambda x: mapped_type[x])
|
61 |
+
|
62 |
+
data_no_na['loan_encoded'] = data_no_na.loan_type_name.apply(lambda x: mapped_type[x])
|
63 |
+
|
64 |
+
data_no_na.property_type_name.value_counts()
|
65 |
+
|
66 |
+
data_no_na['property_encoded'] = data_no_na.property_type_name.apply(lambda x: 1 if x == 'Manufactured housing' else 0)
|
67 |
+
|
68 |
+
data_no_na.state_name.value_counts()
|
69 |
+
|
70 |
+
data_no_na.county_name.value_counts()
|
71 |
+
|
72 |
+
data_no_na.county_name.unique()
|
73 |
+
|
74 |
+
code = {}
|
75 |
+
i = 0
|
76 |
+
for county in data_no_na.county_name.unique():
|
77 |
+
code[county] = i
|
78 |
+
i += 1
|
79 |
+
|
80 |
+
data_no_na['county_code']= data_no_na.county_name.map(code)
|
81 |
+
|
82 |
+
data_no_na.head(2)
|
83 |
+
|
84 |
+
data_no_na['sex_encoded'] = data_no_na.applicant_sex_name.apply(lambda x: 1 if x == 'Male' else 0)
|
85 |
+
|
86 |
+
data_no_na.head(2)
|
87 |
+
|
88 |
+
cols = ['county_code', 'sex_encoded', 'property_encoded', 'loan_encoded', 'applicant_income_000s', 'loan_amount_000s', 'action_taken_name']
|
89 |
+
|
90 |
+
train = data_no_na[cols]
|
91 |
+
|
92 |
+
X_train, X_test, y_train, y_test = train_test_split(train, train.action_taken_name, test_size=0.3, random_state=42)
|
93 |
+
|
94 |
+
|
95 |
+
gbm = LGBMClassifier(n_estimators=200)
|
96 |
+
random = RandomForestClassifier(n_estimators=200)
|
97 |
+
tree = DecisionTreeClassifier()
|
98 |
+
|
99 |
+
gbm.fit(X_train, y_train)
|
100 |
+
random.fit(X_train, y_train)
|
101 |
+
tree.fit(X_train, y_train)
|
102 |
+
|
103 |
+
def mapping(**kwargs):
|
104 |
+
kwargs['sex'] = 1 if kwargs['sex'] == 'Male' else 0
|
105 |
+
kwargs['property_type'] = 1 if kwargs['property_type'] == 'Manufactured housing' else 0
|
106 |
+
kwargs['loan'] = float(kwargs['loan'])
|
107 |
+
kwargs['income'] = float(kwargs['income'])
|
108 |
+
kwargs['loan_amount'] = float(kwargs['loan_amount'])
|
109 |
+
kwargs['county'] = code[kwargs['county']]
|
110 |
+
return kwargs
|
111 |
+
|
112 |
+
|
113 |
+
st.title('Loan Approval for Washington House')
|
114 |
+
|
115 |
+
col1, col2 = st.columns([3, 1])
|
116 |
+
with col1:
|
117 |
+
st.text('Please, fill this form')
|
118 |
+
|
119 |
+
with st.form("my_form"):
|
120 |
+
county = st.selectbox('County', data_no_na.county_name.unique().tolist())
|
121 |
+
sex = st.selectbox('Sex', ['Male', 'Female'])
|
122 |
+
property_type = st.selectbox('Property Type', data_no_na.property_type_name.unique().tolist())
|
123 |
+
loan = st.selectbox('Loan Type', data_no_na.loan_type_name.unique().tolist())
|
124 |
+
income = st.number_input('Your Yearly income (in 000$)')
|
125 |
+
loan_amount = st.number_input('Loan Amount')
|
126 |
+
|
127 |
+
model_choice = col2.selectbox('Choose model', ['RandomForest', 'Tree', 'LGBM'])
|
128 |
+
# Every form must have a submit button.
|
129 |
+
submitted = st.form_submit_button("Submit")
|
130 |
+
if submitted:
|
131 |
+
st.info('Predicting')
|
132 |
+
new_demand = np.array(mapping(county=county, sex=sex, property_type=property_type, loan=loan, income=income, loan_amount=loan_amount))
|
133 |
+
if model_choice == 'Tree':
|
134 |
+
st.text(tree.predict(new_demand))
|
135 |
+
elif model_choice == 'RandomForest':
|
136 |
+
st.text(random.predict(new_demand))
|
137 |
+
else:
|
138 |
+
st.text(gbm.predict(new_demand))
|
139 |
+
|
140 |
+
|