-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdengue_processing.py
More file actions
63 lines (49 loc) · 1.8 KB
/
Copy pathdengue_processing.py
File metadata and controls
63 lines (49 loc) · 1.8 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed May 24 21:36:40 2017
@author: Janaka
"""
#Negative binomial model
from statsmodels.tools import eval_measures
import statsmodels.formula.api as smf
import statsmodels.api as sm
import pandas as pd
import numpy as np
DEFAULT_MODEL = "total_cases ~ 1 + " \
"reanalysis_specific_humidity_g_per_kg + " \
"reanalysis_dew_point_temp_k + " \
"station_min_temp_c + " \
"station_avg_temp_c"
def getBMNegBinomailModel(train, test, model_formula=DEFAULT_MODEL):
# Step 1: specify the form of the model
grid = 10 ** np.arange(-8, -3, dtype=np.float64)
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
def shift(df,n):
df = df.shift(n)
df.fillna(method='bfill', inplace=True)
return df
def rolingMean(df,n):
df = df.rolling(window=1,center=n).mean()
df.fillna(method='bfill', inplace=True)
return df