-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
192 lines (148 loc) · 4.5 KB
/
app.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
from flask import Flask, render_template, request
# import plotly adalah library untuk membuat plot
import plotly
import plotly.graph_objs as go
# import pandas sebagai pengolah data
import numpy as np
import pandas as pd
import json
import pickle
# maemangiil flask sebagai aplikasi
app = Flask(__name__)
# Load Data
df = pd.read_csv('./static/train.csv')
# Function to plot Age distribution
def age_plot():
data = [
go.Box(x=df[df['Response'] == 1]['Age'], name='Potential'),
go.Box(x=df[df['Response'] == 0]['Age'], name='Non-Potential')
]
layout = go.Layout(
title = 'Age vs Response',
title_x = 0.5,
xaxis = dict(title='Age')
)
result = {"data": data, "layout": layout}
graphJSON = json.dumps(result, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
## Create route for Age Distribution exch
@app.route('/')
def index():
plot = age_plot()
return render_template(
'age_visualization.html',
plot = plot
)
@app.route('/age_visualization')
def age_visualization():
plot = age_plot()
return render_template(
'age_visualization.html',
plot = plot
)
##### Annual Premium #####
def anp_plot():
filtered_entries = np.array([True] * len(df))
col = 'Annual_Premium'
Q1 = df[col].quantile(0.25)
Q3 = df[col].quantile(0.75)
IQR = Q3 - Q1
low_limit = Q1 - (IQR * 1.5)
high_limit = Q3 + (IQR * 1.5)
filtered_entries = ((df[col] >= low_limit) & (df[col] <= high_limit)) & filtered_entries
df_no_out = df[filtered_entries]
data = [
go.Box(x=df['Annual_Premium'], name='With Outliers'),
go.Box(x=df_no_out['Annual_Premium'], name='No Outliers')
]
layout = go.Layout(
title = 'Effect on Outliers Removal on Annual Premiums Variable',
title_x = 0.5,
xaxis = dict(title='Annual Premium')
)
result = {"data": data, "layout": layout}
graphJSON = json.dumps(result, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
# Create route for anp_plot
@app.route('/anp_visualization')
def anp_visualization():
plot = anp_plot()
return render_template(
'anp_visualization.html',
plot = plot
)
####### Previously Insured #####
def pi_plot():
df_copy = df.copy()
df_copy['Response'] = df_copy['Response'].apply(str)
df1 = pd.crosstab(index=df_copy['Previously_Insured'], columns=df_copy['Response'])
pi = ['Not-Insured', 'Previously-Insured']
data = [
go.Bar(
name = "Not-Potential",
x = pi,
y = df1['0'],
offsetgroup = 0
),
go.Bar(
name = "Potential",
x = pi,
y = df1['1'],
offsetgroup = 1
)
]
layout = go.Layout(
title = 'Previously Insured vs Response',
# yaxis_title="Count of Customers",
title_x = 0.5
)
result = {"data": data, "layout": layout}
graphJSON = json.dumps(result, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
# Create route for PI Visualization
@app.route('/pi_visualization')
def pi_visualization():
plot = pi_plot()
return render_template(
'pi_visualization.html',
plot=plot
)
##### SHOW DATA ######
def show_data():
df2 = df[:100].set_index('id')
return df2
# Create route for data
@app.route('/data')
def data():
sample = show_data()
return render_template(
'data.html',
data = sample.to_html()
)
######## PREDICTION #########
@app.route('/predict', methods=['POST', 'GET'])
def predict():
return render_template('predict.html')
@app.route('/result', methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
input = request.form
df_predict = pd.DataFrame({
'Gender': [input['ge']],
'Age':[input['Age']],
'Driving_License':[input['dl']],
'Previously_Insured':[input['va']],
'Vehicle_Age':[input['va']],
'Vehicle_Damage':[input['vd']],
'Annual_Premium':[input['Annual Premium']],
'Vintage':[input['Vintage']]
})
prediksi = model.predict_proba(df_predict)[0][1]
prob_percent = (prediksi*100).round(2)
return render_template('result.html',
data=input, prob=prob_percent)
# jalankan fungsi ketika file py ini dipanggil
if __name__=='__main__':
filename = 'estimator_deploy.sav'
model = pickle.load(open(filename,'rb'))
app.run(debug=True, port=5000)