-
Notifications
You must be signed in to change notification settings - Fork 0
/
denguAI.py
276 lines (207 loc) · 8.87 KB
/
denguAI.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 29 14:19:25 2019
@author: HP
"""
import re
import time
import datetime
import operator
import numpy as np
import pandas as pd
import collections
import unicodedata
import collections
import seaborn as sns
import collections
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
from sklearn.preprocessing import Imputer
from sklearn.ensemble import RandomForestRegressor
from tqdm import tqdm
from collections import Counter
from datetime import datetime, date, timedelta
from IPython.display import Image
features_train = pd.read_csv("data/dengue_features_train.csv")
features_test = pd.read_csv("data/dengue_features_test.csv")
labels_train = pd.read_csv("data/dengue_labels_train.csv")
#merge the training features with labels
train = pd.merge(labels_train, features_train, on=['city','year','weekofyear'])
#check whether there is any duplicate labels
#print(np.sum(train.duplicated()))
'''sns.set(style="ticks", palette="colorblind")
g = sns.FacetGrid(train, col="city",aspect=2)
g.map(sns.distplot, "total_cases")
axes = g.axes
axes[0,0].set_ylim(0,0.090)
axes[0,1].set_ylim(0,0.090)
train.groupby('city').mean().total_cases
sns.set(style="ticks", palette="colorblind")
fig = sns.FacetGrid(train, hue='city', aspect=4)
fig.map(sns.pointplot,'weekofyear','total_cases')
max_x = train.weekofyear.max()
min_x = train.weekofyear.min()
fig.set(xlim=(min_x,max_x))
fig.set(ylim=(0, 80))
fig.add_legend()'''
train.isnull().sum()
features_test.isnull().sum()
train.drop('year', axis=1, inplace=True)
train.drop('week_start_date', axis=1, inplace=True)
train_sj = train[train.city == 'sj'].copy()
train_iq = train[train.city == 'iq'].copy()
#x_train_sj = features_train[features_train.city == 'sj'].copy()
#x_train_iq = features_train[features_train.city == 'iq'].copy()
test_sj = features_test[features_test.city == 'sj'].copy()
test_iq = features_test[features_test.city == 'iq'].copy()
train_sj.isnull().sum()
features = ['ndvi_ne', 'ndvi_nw','ndvi_se', 'ndvi_sw', 'precipitation_amt_mm', 'reanalysis_air_temp_k',
'reanalysis_avg_temp_k', 'reanalysis_dew_point_temp_k',
'reanalysis_max_air_temp_k', 'reanalysis_min_air_temp_k',
'reanalysis_precip_amt_kg_per_m2',
'reanalysis_relative_humidity_percent', 'reanalysis_sat_precip_amt_mm',
'reanalysis_specific_humidity_g_per_kg', 'reanalysis_tdtr_k',
'station_avg_temp_c', 'station_diur_temp_rng_c', 'station_max_temp_c',
'station_min_temp_c', 'station_precip_mm']
def fillMissingValues(df, imputer):
imputer.fit(df[features])
df[features] = imputer.transform(df[features])
return df
imputer_sj = Imputer(strategy = 'mean')
train_sj = fillMissingValues(train_sj, imputer_sj)
test_sj = fillMissingValues(test_sj, imputer_sj)
imputer_iq = Imputer(strategy = 'mean')
train_iq = fillMissingValues(train_iq, imputer_iq)
test_iq = fillMissingValues(test_iq, imputer_iq)
def transformTemperatureValues(df):
df['reanalysis_air_temp_k'] = df.reanalysis_air_temp_k -273.15
df['reanalysis_avg_temp_k'] = df.reanalysis_avg_temp_k-273.15
df['reanalysis_dew_point_temp_k'] = df.reanalysis_dew_point_temp_k-273.15
df['reanalysis_max_air_temp_k'] = df.reanalysis_max_air_temp_k-273.15
df['reanalysis_min_air_temp_k'] = df.reanalysis_min_air_temp_k-273.15
return df
train_sj = transformTemperatureValues(train_sj)
train_iq = transformTemperatureValues(train_iq)
test_sj = transformTemperatureValues(test_sj)
test_iq = transformTemperatureValues(test_iq)
sj_correlations = train_sj.corr()
iq_correlations = train_iq.corr()
import matplotlib.cm as cm
from matplotlib import cm
cmap = cmap=sns.diverging_palette(5, 250, as_cmap = True)
def magnify():
return [dict(selector="th",
props=[("font-size", "7pt")]),
dict(selector="td",
props=[('padding', "0em 0em")]),
dict(selector="th:hover",
props=[("font-size", "12pt")]),
dict(selector="tr:hover td:hover",
props=[('max-width', '200px'),
('font-size', '12pt')])
]
sj_correlations.style.background_gradient(cmap, axis=1)\
.set_properties(**{'max-width': '80px', 'font-size': '10pt'})\
.set_caption("Hover to magify")\
.set_precision(2)\
.set_table_styles(magnify())
(sj_correlations
.total_cases
.drop('total_cases')
.sort_values(ascending = False)
.plot
.barh())
sns.set(style="ticks", palette= "colorblind")
'''iq_correlations.style.background_gradient(cmap, axis=1)\
.set_properties(**{'max-width': '80px', 'font-size': '10pt'})\
.set_caption("Hover to magify")\
.set_precision(2)\
.set_table_styles(magnify())
(iq_correlations
.total_cases
.drop('total_cases')
.sort_values(ascending = False)
.plot
.barh())
sns.set(style="ticks", palette= "colorblind")'''
importantFeature = ['reanalysis_specific_humidity_g_per_kg',
'reanalysis_dew_point_temp_k',
'reanalysis_min_air_temp_k',
'station_min_temp_c',
'station_max_temp_c',
'station_avg_temp_c']
dropFeatures = list(set(features) - set(importantFeature))
def droppingFeatures(df):
df.drop(dropFeatures, axis=1, inplace=True)
return df
train_sj = droppingFeatures(train_sj)
train_iq = droppingFeatures(train_iq)
test_sj = droppingFeatures(test_sj)
test_iq = droppingFeatures(test_iq)
def normalizeData(feature):
return (feature - feature.mean()) / feature.std()
train_sj[importantFeature] = train_sj[importantFeature].apply(normalizeData, axis=0)
train_iq[importantFeature] = train_iq[importantFeature].apply(normalizeData, axis=0)
test_sj[importantFeature] = test_sj[importantFeature].apply(normalizeData, axis=0)
test_iq[importantFeature] = test_iq[importantFeature].apply(normalizeData, axis=0)
sj_train_subtrain = train_sj.head(800)
sj_train_subtest = train_sj.tail(train_sj.shape[0] - 800)
iq_train_subtrain = train_iq.head(400)
iq_train_subtest = train_iq.tail(train_iq.shape[0] - 400)
from statsmodels.tools import eval_measures
import statsmodels.formula.api as smf
import statsmodels.api as sm
from statsmodels.tools import eval_measures
import statsmodels.formula.api as smf
def get_best_model(train, test):
# Step 1: specify the form of the model
model_formula = "total_cases ~ 1 + " \
"reanalysis_specific_humidity_g_per_kg + " \
"reanalysis_dew_point_temp_k + " \
"reanalysis_min_air_temp_k + " \
"station_min_temp_c + " \
"station_max_temp_c + " \
"station_avg_temp_c"
grid = 10 ** np.arange(-8, -3, dtype=np.float64)
print(grid)
best_alpha = []
best_score = 1000
# Step 2: Find the best hyper parameter, alpha
for alpha in grid:
model = smf.glm(formula=model_formula,
data=train,
family=sm.families.NegativeBinomial(alpha=alpha))
results = model.fit()
predictions = results.predict(test).astype(int)
score = eval_measures.meanabs(predictions, test.total_cases)
if score < best_score:
best_alpha = alpha
best_score = score
print('best alpha = ', best_alpha)
print('best score = ', best_score)
# Step 3: refit on entire dataset
full_dataset = pd.concat([train, test])
model = smf.glm(formula=model_formula,
data=full_dataset,
family=sm.families.NegativeBinomial(alpha=best_alpha))
fitted_model = model.fit()
return fitted_model
sj_best_model = get_best_model(sj_train_subtrain, sj_train_subtest)
iq_best_model = get_best_model(iq_train_subtrain, iq_train_subtest)
figs, axes = plt.subplots(nrows=2, ncols=1)
# plot sj
train_sj['fitted'] = sj_best_model.fittedvalues
train_sj.fitted.plot(ax=axes[0], label="Predictions")
train_sj.total_cases.plot(ax=axes[0], label="Actual")
# plot iq
train_iq['fitted'] = iq_best_model.fittedvalues
train_iq.fitted.plot(ax=axes[1], label="Predictions")
train_iq.total_cases.plot(ax=axes[1], label="Actual")
plt.suptitle("Dengue Predicted Cases vs. Actual Cases")
plt.legend()
sj_predictions = sj_best_model.predict(test_sj).astype(int)
iq_predictions = iq_best_model.predict(test_iq).astype(int)
'''submission = pd.read_csv("data/submission_format.csv",
index_col=[0, 1, 2])
submission.total_cases = np.concatenate([sj_predictions, iq_predictions])
submission.to_csv("data/Model_2.csv")'''