-
Notifications
You must be signed in to change notification settings - Fork 0
/
unified.py
153 lines (125 loc) · 6.22 KB
/
unified.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
# -*- coding: utf-8 -*-
import pickle
import functools
import gzip
import dash
import dash_core_components as dcc
from dash.dependencies import Input, Output
import dash_html_components as html
import numpy as np
import pandas as pd
# get the external stylesheets, images, names
EXTERNAL_STYLESHEETS = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
predictions = pd.DataFrame(columns=['Name', 'Likelihood'])
#load model into cache
@functools.lru_cache()
def get_model():
with gzip.open('Fish_Data/fat_model.pkl.gz', 'rb') as pickle_file:
return pickle.load(pickle_file)
app = dash.Dash(__name__, external_stylesheets=EXTERNAL_STYLESHEETS)
app.title = 'WhatTheFish'
app.layout = html.Div([
html.Div([
dcc.Tabs(id="tabs", children=[
dcc.Tab(label='What the Fish?', children=[
html.Label('Which sub-region will you fish in?'),
dcc.Dropdown(id='sub_reg',
options=[{'label':'North Atlantic (ME; NH; MA; RI; CT)', 'value':0},
{'label': ' Mid-Atlantic (NY; NJ; DE; MD; VA)', 'value':1},
{'label': ' South Atlantic (NC; SC; GA; East FL)', 'value':2},
{'label': 'Gulf of Mexico (WFL; AL; MS; LA)', 'value':3}
],
value=0),
html.Label('How far from shore will you fish?'),
dcc.Dropdown(id='area_x',
options=[{'label':'<=3miles', 'value':0},
{'label':'>3miles', 'value':1},
{'label':'Inland', 'value':4},
{'label':'<=10 miles (West Florida only)', 'value':2},
{'label':'>10 miles (West Florida only)', 'value':3}],
value=0),
html.Label('Will you fish from land or sea?'),
dcc.Dropdown(id='mode',
options=[
{'label':'Shore', 'value':0},
{'label':'Headboat', 'value':1},
{'label':'Charter Boat', 'value':2},
{'label':'Private/Rental', 'value':3}],
value=0),
html.Label('In what type of habitat will you fish?'),
dcc.Dropdown(id='hab',
options=[
{'label':'Open water', 'value':0},
{'label':'Sound', 'value':1},
{'label':'River', 'value':2},
{'label':'Bay', 'value':3},
{'label':'Other', 'value':4}],
value=0),
html.Label('During which month will you fish?'),
dcc.Dropdown(id='month',
options=[
{'label':'January', 'value':0},
{'label':'February', 'value':1},
{'label':'March', 'value': 2},
{'label': 'April', 'value':3},
{'label': 'May', 'value':4},
{'label': 'June', 'value':5},
{'label': 'July', 'value':6},
{'label': 'August', 'value':7},
{'label': 'September', 'value':8},
{'label': 'October', 'value':9},
{'label': 'November', 'value':10},
{'label': 'December', 'value':11}],
value=0),
html.Label('How many hours will you be out in a boat?'),
dcc.Dropdown(id='bthrs',
options=[{'label':x, 'value':x} for x in range(1, 25)]+\
[{'label':'UNKNOWN', 'value':99}],
value=1),
html.Label('Number of people who will fish together'),
dcc.Dropdown(id='contributors',
options=[{'label':x, 'value':x} for x in range(1, 20)],
value=1),
html.Label('How many days have you been fishing in the past year?'),
dcc.Dropdown(id='ffdys12',
options=[{'label':x, 'value':x} for x in range(1, 365)]+\
[{'label':'UNKNOWN', 'value':999}],
value=1)
]),
])
], style={'display':'inline-block', 'width':'40%', 'marginLeft':'10%',
'marginRight':'10%'}),
html.Div(children=[
html.H1('Predicted Fish:'),
html.Div(id='my-div')], style={'display':'inline-block', 'width':'30%'})
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input(component_id='contributors', component_property='value'),
Input(component_id='area_x', component_property='value'),
Input(component_id='mode', component_property='value'),
Input(component_id='sub_reg', component_property='value'),
Input(component_id='hab', component_property='value'),
Input(component_id='bthrs', component_property='value'),
Input(component_id='ffdys12', component_property='value'),
Input(component_id='month', component_property='value')])
def predictor(contributors, area_x, mode, sub_reg, hab,
bthrs, ffdys12, month):
X = np.array([area_x, mode, sub_reg, hab, bthrs,
contributors, ffdys12, month])
x = X.reshape(1, -1)
m = get_model()
test = m.predict_proba(x)
predictions['Likelihood'] = test[0]
predictions['Name'] = m.classes_
tops = predictions.sort_values(by='Likelihood', ascending=False)
ret = round(tops.head(3), 2)
return html.Table(
#header
[html.Tr([html.Th(col) for col in ret.columns])] +
#body
[html.Tr([
html.Td(ret.iloc[i][col]) for col in ret.columns]) for i in range(min(len(ret), 3))]
)
if __name__ == '__main__':
app.run_server(debug=True)