forked from uva-hydroinformatics/flood_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevision_questions.py
331 lines (169 loc) · 5.68 KB
/
revision_questions.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# coding: utf-8
# ## Intro
# There are a couple comments from the reviewers that I want to clear up in this notebook:
#
# 1. How sensitive are the RF results to different parameter values such as number of trees? (from reviewer 1)
# 2. Why not just use rainfall as a proxy for flood severity? Why go to all the work of building these more complex model if most of the information just comes from the rainfall?
# 3. Not what the reviewers asked for but for my own peace of mind, are the two models being tested on the same data?
# In[1]:
get_ipython().magic(u'matplotlib inline')
from hr_db_scripts.main_db_script import get_db_table_as_df
from db_scripts.main_db_script import db_filename
from results import make_results_df
import pandas as pd
# In[2]:
def get_tables(table_suffix):
rf_trn_tbl = 'rf{}train'.format(table_suffix)
rf_tst_tbl = 'rf{}test'.format(table_suffix)
ps_trn_tbl = 'poisson{}train'.format(table_suffix)
ps_tst_tbl = 'poisson{}test'.format(table_suffix)
rf_trn = get_db_table_as_df(rf_trn_tbl, dbfilename=db_filename)
rf_tst = get_db_table_as_df(rf_tst_tbl, dbfilename=db_filename)
ps_trn = get_db_table_as_df(ps_trn_tbl, dbfilename=db_filename)
ps_tst = get_db_table_as_df(ps_tst_tbl, dbfilename=db_filename)
return {'rf_trn': rf_trn, 'rf_tst': rf_tst, 'ps_trn': ps_trn, 'ps_tst': ps_tst}
# ### Question 3: are the models being tested on the same data?
# In[3]:
suffix = '_revisions_'
tables = get_tables(suffix)
# In[4]:
(tables['rf_trn']['all_trn'] != tables['ps_trn']['all_trn']).sum()
# In[5]:
(tables['rf_tst']['all_tst'] != tables['ps_tst']['all_tst']).sum()
# It looks like they weren't the same... Now we need to see how that affects the results
# ### Now the code has been refactored so they are the same
# In[6]:
suffix = '_revisions1_'
tables1 = get_tables(suffix)
# In[7]:
(tables1['rf_trn']['all_trn'] != tables1['ps_trn']['all_trn']).sum()
# In[8]:
(tables1['rf_tst']['all_tst'] != tables1['ps_tst']['all_tst']).sum()
# ### Question: do the results differ with the restructuring of the code?
# In[9]:
(tables1['rf_trn']['all_trn'] != tables['rf_trn']['all_trn']).sum()
# In[10]:
(tables1['rf_trn']['all_pred_trn'] != tables['rf_trn']['all_pred_trn']).sum()
# In[11]:
(tables1['rf_tst']['all_tst'] != tables['rf_tst']['all_tst']).sum()
# In[12]:
(tables1['ps_trn']['all_trn'] != tables['ps_trn']['all_trn']).sum()
# In[13]:
(tables1['ps_tst']['all_tst'] != tables['ps_tst']['all_tst']).sum()
# So now they are all the same
# ### Question #1 sensitivity to number of trees
# In[14]:
trees = [2, 5, 10, 17, 25, 35, 50, 100, 250, 350, 500, 650, 750, 1000, 2000]
# In[15]:
tables = {}
# In[16]:
for t in trees:
tables[t] = 'rf_{}'.format(t)
tables[500] = 'revisions1'
# In[17]:
dfs = []
for t in tables:
df = make_results_df(models=['rf'], suffix=tables[t])
df.index = [t]
dfs.append(df)
df_comb = pd.concat(dfs)
# In[18]:
df_comb.sort_index(inplace=True)
ax = df_comb.plot(style="o-", figsize = (9,7), logx=True, grid=True)
ax.legend(bbox_to_anchor=(1, 0.5))
ax.set_xlabel('Number of trees')
# In[19]:
ax = df_comb.rolling(5).mean().plot(style='o-', logx=True)
ax.legend(bbox_to_anchor=(1, 0.5))
ax.set_xlabel('Number of trees')
# ### Question #2 what if only rainfall
# In[20]:
suffixes = ['revisions1', 'only_rd', 'only_rain', 'no_rd', 'no_tides', 'top_5', 'top_2', 'revisions2', 'revisions2_no_tide']
# In[21]:
dfs = []
for s in suffixes:
df = make_results_df(suffix=s)
dfs.append(df)
df.index = ["{}_{}".format(i, s) for i in df.index]
df_vars = pd.concat(dfs)
# In[22]:
df_vars
# In[23]:
(21.41-17.82)/21.41
# In[24]:
(21.41-20.49)/21.41
# In[25]:
(24.95-21.41)/21.41
# In[26]:
ax = df_vars.plot.bar(figsize=(8,6))
ax.legend(bbox_to_anchor=(1, 0.5))
# ### Sensitivity to number of variables
# In[27]:
suffixes = [4, 6, 8, 10, 12, 14, 18, 20]
suffixes = ['rf_{}v'.format(i) for i in suffixes]
# In[28]:
dfs = []
for s in suffixes:
df = make_results_df(suffix=s, models=['rf'])
dfs.append(df)
df.index = ["{}_{}".format(i, s) for i in df.index]
df_nvars = pd.concat(dfs)
# In[29]:
df_nvars
# In[30]:
ax = df_nvars.plot(figsize=(8,6))
ax.legend(bbox_to_anchor=(1, 0.5))
# In[31]:
df_tune = get_db_table_as_df("tuning_mtry", dbfilename=db_filename)
# In[32]:
del df_tune['row_names']
# In[33]:
df_tune_piv = df_tune.pivot(columns="mtry", values="OOBError")
df_tune_piv.mean().plot.bar()
# In[34]:
j = 0
l = []
m = []
first_time = True
for i in df_tune.iterrows():
if i[1]['mtry'] == 1 and first_time == False:
sub_df = pd.DataFrame(m)
sub_df['num_run'] = j
sub_df['rank'] = sub_df['OOBError'].rank()
l.append(sub_df)
j += 1
m = []
first_time = False
m.append(i[1])
# In[35]:
df_tune_counts = pd.concat(l)
# In[36]:
df_tune_counts.head()
# In[37]:
df_tune_rank_pivot = df_tune_counts.pivot_table(values='rank', columns='num_run', index='mtry')
# In[38]:
df_tune_rank_pivot.head(6)
# In[39]:
(df_tune_rank_pivot == 1).sum(1).plot.bar()
# ## number of predictions outside 159 poisson
# In[40]:
suffix = '_revisions2_'
tables_rev2 = get_tables(suffix)
# In[41]:
tables_rev2
# In[42]:
ps_prd = tables_rev2['ps_tst']['all_pred_tst']
# Percent above 159
# In[43]:
(ps_prd > 159).sum()/float(len(ps_prd)) * 100
# Percent above 159 when prediction is at least 0.5 (rounds to 1)
# In[44]:
(ps_prd > 159).sum()/float((ps_prd>0.5).sum()) * 100
# ### predictions when true flooding is 31
# In[52]:
ps_tst = tables_rev2['ps_tst']
pred_31 = ps_tst[(ps_tst['all_tst']==31) & (ps_tst['all_pred_tst']<159)]['all_pred_tst']
print pred_31.mean()
print pred_31.max()
print pred_31.min()
# In[ ]: