Spaces:
Build error
Build error
Particial code
Browse files
app.py
CHANGED
@@ -1,4 +1,73 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Imports
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from sklearn.linear_model import LogisticRegression
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.model_selection import GridSearchCV
|
8 |
|
9 |
+
## Load Dataset
|
10 |
+
data = pd.read_csv('/content/drive/MyDrive/LoanApprovalPrediction.csv')
|
11 |
+
|
12 |
+
## Data Preprocessing
|
13 |
+
# Replace NaN with a estimate value accordingly
|
14 |
+
# Fill empty values of Dependant,Loan Amount,Loan_Amount_Term as it's numeric and float dtype
|
15 |
+
data['Dependents'].fillna(data['Dependents'].median(),inplace=True)
|
16 |
+
data['LoanAmount'].fillna(data['LoanAmount'].median(),inplace=True)
|
17 |
+
data['Loan_Amount_Term'].fillna(data['Loan_Amount_Term'].median(),inplace=True)
|
18 |
+
|
19 |
+
# Fill Empty Credit History with mode as its categorical
|
20 |
+
data['Credit_History'].fillna(data['Credit_History'].mode()[0],inplace=True)
|
21 |
+
|
22 |
+
# Dropping UnWanted Loan_ID column
|
23 |
+
data.drop(['Loan_ID'],axis=1,inplace=True)
|
24 |
+
|
25 |
+
# Changing Data Types of Columns
|
26 |
+
data['Dependents']=data['Dependents'].astype(int)
|
27 |
+
data['ApplicantIncome']=data['ApplicantIncome'].astype(int)
|
28 |
+
data['CoapplicantIncome']=data['CoapplicantIncome'].astype(int)
|
29 |
+
data['LoanAmount']=data['LoanAmount'].astype(int)
|
30 |
+
data['Loan_Amount_Term']=data['Loan_Amount_Term'].astype(int)
|
31 |
+
data['Credit_History'] = data['Credit_History'].astype(int)
|
32 |
+
|
33 |
+
# Categorical to Numerical Value Conversion
|
34 |
+
data['Gender']=data.Gender.apply(lambda x:1 if x=='Male' else 0 )
|
35 |
+
data['Education'] = data.Education.apply(lambda x:0 if x=='Graduate' else 1)
|
36 |
+
data['Married'] = data.Education.apply(lambda x:0 if x=='Yes' else 1)
|
37 |
+
data['Self_Employed'] = data.Education.apply(lambda x:0 if x=='Yes' else 1)
|
38 |
+
Prop_area = {'Urban':0,'Semiurban':1,'Rural':2}
|
39 |
+
data['Property_Area'] = data['Property_Area'].map(Prop_area)
|
40 |
+
|
41 |
+
## Train Test Split
|
42 |
+
X=data.drop('Loan_Status',1)
|
43 |
+
y=data.Loan_Status.apply(lambda x:0 if x=='Y' else 1) # Can use pd.get_dummies to reduce code
|
44 |
+
X_train,X_test,y_train,y_test = train_test_split(X,y,train_size=0.7,test_size=0.3,random_state=42)
|
45 |
+
|
46 |
+
## Parameter Efficient Logestic Regression Model Training By GridSearchCV
|
47 |
+
LG = LogisticRegression()
|
48 |
+
parameter = {'penalty':['l1','l2','elasticnet'],'C':[1,2,5,10,20,25,30,40,50],'max_iter':[100,150,200,250]}
|
49 |
+
Eff_log_reg=GridSearchCV(estimator=LG,param_grid=parameter,scoring='accuracy',cv=5)
|
50 |
+
Log_Model = Eff_log_reg.fit(X_train,y_train)
|
51 |
+
|
52 |
+
## Gradio App
|
53 |
+
def input(gender,married,dependents,education,self_employed,app_income,co_app_income,loan_amount,Loan_term,credit,area):
|
54 |
+
input = [gender,married,dependents,education,self_employed,app_income,co_app_income,loan_amount,Loan_term,credit,area]
|
55 |
+
output = lm.predict([input])
|
56 |
+
return int(output)
|
57 |
+
|
58 |
+
demo = gr.Interface(
|
59 |
+
input,
|
60 |
+
[
|
61 |
+
gradio.Checkbox(['Male','Female'],label=Gender,max_choice=1),
|
62 |
+
gr.Slider(minimum=600, maximum=7000, randomize=True, step = 1,label="Living Area"),
|
63 |
+
gr.Slider(minimum=1, maximum=8, randomize=True,step = 1, label="Number of Bedrooms"),
|
64 |
+
gr.Slider(minimum=1, maximum=5, randomize=True,step = 1, label="Number of Bathrooms"),
|
65 |
+
gr.Slider(minimum=1,maximum=3.5,randomize=True,step=0.5,label="Number of stories/Floors")
|
66 |
+
],
|
67 |
+
"number",
|
68 |
+
examples=[
|
69 |
+
[1000, 600, 1, 1, 1],
|
70 |
+
[2000,1200,2,3,1],
|
71 |
+
[4000,1900,2,3,2],
|
72 |
+
[28000,3000,5,3,3],
|
73 |
+
],
|