forked from dataman-git/codes_for_articles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_model.py
109 lines (95 loc) · 3.83 KB
/
deploy_model.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
###############################
# This program lets you #
# - enter values in Streamlit #
# - get prediction #
###############################
import pickle
import pandas as pd
import streamlit as st
# loading the model
path = ''
modelname = path + '/toymodel.pkl'
loaded_model = pickle.load(open(modelname, 'rb'))
#############
# Main page #
#############
st.write("The model prediction")
LIVINGAPARTMENTS_AVG_MIN = 0.0
LIVINGAPARTMENTS_AVG_MAX = 1.0
APARTMENTS_AVG_MIN = 0.0
APARTMENTS_AVG_MAX = 0.11697126743049956
# Get input values - numeric variables
LIVINGAPARTMENTS_AVG = st.slider('Please enter the living apartments:',
min_value = LIVINGAPARTMENTS_AVG_MIN,
max_value = LIVINGAPARTMENTS_AVG_MAX
)
APARTMENTS_AVG = st.slider('Please enter the apartment average:',
min_value = APARTMENTS_AVG_MIN,
max_value = APARTMENTS_AVG_MAX
)
# Set dummy variables to zero
cat_list = ['Accountants', 'Cleaning_staff', 'Cooking_staff',
'Core_staff', 'Drivers', 'High_skill_tech_staff',
'Laborers', 'Managers', 'Medicine_staff',
'OTHER', 'Sales_staff', 'Security_staff']
for i in cat_list:
exec("%s = %d" % (i,0)) # The exec() command makes a value as the variable name
# Enter data for prediction
Occupation = st.selectbox('Please choose Your Occupation',
('Accountants',
'Cleaning_staff',
'Cooking_staff',
'Core_staff',
'Drivers',
'High_skill_tech_staff',
'Laborers',
'Managers',
'Medicine_staff',
'Sales_staff',
'Security_staff',
'OTHER')
)
if Occupation=='Accountants':
Accountants =1
elif Occupation=='Cleaning_staff':
Cleaning_staff =1
elif Occupation=='Cooking_staff':
Cooking_staff =1
elif Occupation=='Core_staff':
Core_staff =1
elif Occupation=='Drivers':
Drivers =1
elif Occupation=='High_skill_tech_staff':
High_skill_tech_staff =1
elif Occupation=='Laborers':
Laborers =1
elif Occupation=='Managers':
Managers =1
elif Occupation=='Medicine_staff':
Medicine_staff =1
elif Occupation=='Sales_staff':
Sales_staff =1
elif Occupation=='Security_staff':
Security_staff =1
else:
OTHER =1
# when 'Predict' is clicked, make the prediction and store it
if st.button("Get Your Prediction"):
X = pd.DataFrame({'APARTMENTS_AVG':[APARTMENTS_AVG],
'LIVINGAPARTMENTS_AVG':[LIVINGAPARTMENTS_AVG],
'Accountants':[Accountants],
'Cleaning_staff':[Cleaning_staff],
'Cooking_staff':[Cooking_staff],
'Core_staff':[Core_staff],
'Drivers':[Drivers],
'High_skill_tech_staff':[High_skill_tech_staff],
'Laborers':[Laborers],
'Managers':[Managers],
'Medicine_staff':[Medicine_staff],
'Sales_staff':[Sales_staff],
'Security_staff':[Security_staff],
'OTHER':[OTHER]
})
# Making predictions
prediction = loaded_model.predict_proba(X)[:,1] # The model produces (p0,p1), we want p1.
st.success('Your Target is {}'.format(prediction))