Пример #1
0
def qqplot(predicted, real, city, state, look_back, all_predict_n=False):
    """
    Plot QQPlot for prediction values
    :param predicted: Predicted matrix
    :param real: Array of target_col values used in the prediction
    :param city: Geocode of the target city predicted
    :param state: State containing the city
    :param look_back: Look-back time window length used by the model
    :param all_predict_n: If True, plot the qqplot for every value predicted
    :return:
    """
    data_full = get_alerta_table(city, state)
    data_full = data_full[['casos_est', 'SE']].reset_index(drop=True)
    data_full.SE = [str(i)[-2:] for i in data_full.SE]

    rvs = create_rvs(data_full)

    if all_predict_n:
        fig, axs = P.subplots(4, 1, figsize=[6, 20])
        weeks = list(range(predicted.shape[1]))
    else:
        fig, axs = P.subplots(1, 1, figsize=[6, 5])
        weeks = [0]

    for week in weeks:
        data = data_full.iloc[look_back + week:len(real) + look_back + week]
        data['preds'] = predicted[:, week]
        data['real'] = real[:, week]

        data = data.merge(rvs, on='SE', how='left')
        data['p_preds'] = data.apply(create_px, axis=1)
        data['q_real'] = data.apply(create_qy, axis=1)

        train = int(len(real) * 0.7)
        preds_max = int(data['p_preds'].max())

        axs.plot(list(range(preds_max)), list(range(preds_max)), color='black')
        data[train:].plot(x='p_preds',
                          y='q_real',
                          kind='scatter',
                          ax=axs,
                          color='b',
                          legend=True)
        data[:train].plot(x='p_preds',
                          y='q_real',
                          kind='scatter',
                          ax=axs,
                          color='r',
                          legend=True)

        P.savefig('{}/qqplot_{}'.format(FIG_PATH, city), dpi=300)
        # P.show()
    return data
Пример #2
0
import numpy as np
import pandas as pd
import pyflux as pf
from datetime import datetime
import matplotlib.pyplot as plt
from infodenguepredict.data.infodengue import get_alerta_table


def build_model(data, ar=2, sc=4, family=pf.families.Poisson, target=None):
    model = pf.GASLLEV(data=data, family=family(), target=target)
    return model


if __name__ == "__main__":
    prediction_window = 5  # weeks
    data = get_alerta_table(3304557)  # Nova Iguaçu: 3303609
    data.casos.plot()
    model = build_model(data, ar=2, sc=6, target='casos')
    fit = model.fit()  #'BBVI',iterations=1000,optimizer='RMSProp')
    print(fit.summary())
    model.plot_fit()
    model.plot_z(figsize=(15, 5))
    plt.savefig('GASLLEV_in_sample.png')
    data.casos.plot(style='ko')
    model.plot_predict(h=10, past_values=52)
    plt.savefig('GASLLEV_prediction.png')
    # plt.show()
Пример #3
0
def get_cities_from_state(state):
    alerta_table = get_alerta_table(state=state)
    cities_list = alerta_table.municipio_geocodigo.unique()
    return cities_list
Пример #4
0
from infodenguepredict.data.infodengue import get_alerta_table, build_multicity_dataset
import seaborn


def build_model(data, lag_order, window_type):
    data.index = pd.DatetimeIndex(data.index)
    model = DynamicVAR(data, lag_order=lag_order, window=12, window_type=window_type)
    return model


if __name__ == "__main__":
    prediction_window = 6  # weeks
    # scenario = 'local'
    scenario = 'global'
    if scenario == 'local':
        data = get_alerta_table(3303500)  # Nova Iguaçu: 3303500
        data = data[['casos', 'p_inc100k','nivel']]
    else:
        data = build_multicity_dataset('RJ')
        data = data[[col for col in data.columns if col.startswith('casos') and not col.startswith('casos_est')][:3]]
        data = data.diff()
    print(data.info())
    #TODO: Apply Seasonal differencing to series
    # data.casos.plot(title="Series")
    model = build_model(data, 12, 'expanding')
    # fit = model.(maxlags=11, ic='aic') # 4 lags
    # print(model.coefs.minor_xs('casos_3303500').info())



    forecast = model.forecast(prediction_window)
Пример #5
0
 def test_alerta_state(self):
     df = get_alerta_table(state='RJ')
     self.assertIsInstance(df, pd.DataFrame)
     self.assertGreater(df.size, 0)
     assert (df.municipio_geocodigo.values > 3300000).all()
     assert (df.municipio_geocodigo.values < 4000000).all()
Пример #6
0
 def test_alerta_all_munic(self):
     df = get_alerta_table()
     self.assertIsInstance(df, pd.DataFrame)
     self.assertGreater(df.size, 0)
     self.assertGreater(df.municipio_geocodigo.value_counts().size, 1)
Пример #7
0
 def test_alerta_one_munic(self):
     df = get_alerta_table(3303609)
     self.assertIsInstance(df, pd.DataFrame)
     self.assertGreater(df.size, 0)
     self.assertGreater(df.casos.sum(), 0)
     self.assertEquals(df.municipio_geocodigo.value_counts().size, 1)