<jupyter_start><jupyter_text>Configuração<jupyter_code>!pip uninstall numpy
!pip install numpy

!pip install pmdarima
#Import modules
%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import math
from random import shuffle, choice
import datetime

import matplotlib
import statsmodels.api as sma
import statsmodels.graphics as smg
import statsmodels.tsa as smt
import statsmodels.tools as smtools

from pmdarima.arima import auto_arima
!git clone https://github.com/eitiyamamoto/MO826-projeto.git
%cd MO826-projeto
!ls
!git pull origin master
isolamento_sao_paulo_data = pd.read_csv('data/raw/20210420_dados_covid_municipios_sp_utf_8.csv', engine='python', sep=';')
isolamento_sao_paulo_data.info()
isolamento_sao_paulo_data.head(10)
booleans = isolamento_sao_paulo_data['codigo_ibge'] == 3509502
filter_campinas = pd.Series(booleans)
campinas_isolamento = isolamento_sao_paulo_data[filter_campinas]
campinas_isolamento.head(10)
cols_time_series = ['datahora', 'obitos']
campinas_isolamento_obito = campinas_isolamento[cols_time_series]
campinas_isolamento_obito['datahora'] = pd.to_datetime(campinas_isolamento_obito.loc[:, 'datahora'], format="%d/%m/%Y")
campinas_isolamento_obito['datahora'] = campinas_isolamento_obito.loc[:,'datahora'].astype('datetime64[ns]')
campinas_isolamento_obito = campinas_isolamento_obito.set_index("datahora")
campinas_isolamento_obito = campinas_isolamento_obito.sort_index().asfreq(freq='D', method='pad')
campinas_isolamento_obito.index<jupyter_output>/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  """Entry point for launching an IPython kernel.
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy<jupyter_text>Óbitos totais  Holt Winter<jupyter_code>campinas_isolamento_obito
case_train = campinas_isolamento_obito[:295]
case_test = campinas_isolamento_obito[295:]
holt_winter_model = smt.holtwinters.ExponentialSmoothing(case_train, trend="add", seasonal="add")

result_confirmed = holt_winter_model.fit()

print(result_confirmed.summary())
pred_train = result_confirmed.predict(start=case_train.index[0], end=case_train.index[-1])
pred_test = result_confirmed.predict(start=case_test.index[0], end=case_test.index[-1])
plt.plot(campinas_isolamento_obito.index, campinas_isolamento_obito, label="Real cases")
plt.plot(pred_train.index, pred_train, label='Holt Winters Train')
plt.plot(pred_test.index, pred_test, label='<NAME> Test')
plt.plot(figsize=(64,32))
plt.show()
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(case_test, pred_test)
mse<jupyter_output><empty_output><jupyter_text>Autor ARIMA<jupyter_code>model_auto = auto_arima(case_train['obitos'].values,start_p=0, start_q=0, 
                            max_p=6, max_q=6, d=1,seasonal=True, trace=True,
                            error_action='ignore', suppress_warnings=True, stepwise=False)
model_auto.fit(case_train['obitos'].values)
predict = model_auto.predict(n_periods= int(case_test.size))
plt.plot(case_test.index, case_test)
plt.plot(case_test.index, predict)
plt.gcf().set_size_inches((16,8))
plt.plot(case_train.index, case_train)
plt.plot(case_test.index, case_test)
plt.plot(case_test.index, predict)
plt.gcf().set_size_inches((16,8))
mse = mean_squared_error(predict, case_test)
mse<jupyter_output><empty_output><jupyter_text>process data<jupyter_code>campinas_isolamento.head(5)
campinas_isolamento['datahora'] = pd.to_datetime(campinas_isolamento.loc[:, 'datahora'], format="%d/%m/%Y")
campinas_isolamento['datahora'] = campinas_isolamento.loc[:,'datahora'].astype('datetime64[ns]')
campinas_isolamento = campinas_isolamento.set_index("datahora")
campinas_isolamento = campinas_isolamento.sort_index().asfreq(freq='D', method='pad')
campinas_isolamento.index
campinas_isolamento_lagged = campinas_isolamento_obito[:]
for i in range(1,7):
  campinas_obito_lagged = campinas_isolamento_obito.shift(i);
  campinas_obito_lagged = campinas_obito_lagged.rename(columns={'obitos': 'date_' + str(i)})
  campinas_isolamento_lagged = pd.merge(campinas_isolamento_lagged, campinas_obito_lagged, how='inner', left_on=['datahora'], right_on=['datahora'])
campinas_isolamento_lagged[50:].head(10)
obitos = campinas_isolamento_lagged.pop('obitos')
obitos
campinas_isolamento_lagged
campinas_isolamento_lagged = campinas_isolamento_lagged.fillna(0)
campinas_isolamento_lagged<jupyter_output><empty_output><jupyter_text>SVR<jupyter_code>case_train = campinas_isolamento_lagged[:294]
label_train = obitos[:294]

case_test = campinas_isolamento_lagged[294:]
label_test = obitos[294:]
from sklearn import svm
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
clf = make_pipeline(StandardScaler(),
                    svm.SVR(max_iter=5000, kernel='linear'))
clf.fit(case_train, label_train)
r = clf.score(case_test, label_test)
r
campinas_train_predict = clf.predict(case_train)
campinas_test_predict = clf.predict(case_test)
plt.plot(case_train.index, label_train, label="real")
plt.plot(case_train.index, campinas_train_predict, label="Predicted")
plt.plot(case_test.index, label_test, label="real test")
plt.plot(case_test.index, campinas_test_predict, label="Predicted test")
plt.show()
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(label_test, campinas_test_predict)
mse<jupyter_output><empty_output><jupyter_text>SGD<jupyter_code>case_train = campinas_isolamento_lagged[:294]
label_train = obitos[:294]

case_test = campinas_isolamento_lagged[294:]
label_test = obitos[294:]
from sklearn.linear_model import SGDRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
reg = make_pipeline(StandardScaler(),
                    SGDRegressor(max_iter=1000, tol=1e-3))
reg.fit(case_train, label_train)
r = reg.score(case_test, label_test)
r
campinas_train_predict = reg.predict(case_train)
campinas_test_predict = reg.predict(case_test)
plt.plot(case_train.index, label_train, label="real")
plt.plot(case_train.index, campinas_train_predict, label="Predicted")
plt.plot(case_test.index, label_test, label="real test")
plt.plot(case_test.index, campinas_test_predict, label="Predicted test")
plt.show()
plt.rcParams["figure.figsize"] = (60,20)
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(label_test, campinas_test_predict)
mse<jupyter_output><empty_output><jupyter_text>Ada boost<jupyter_code>from sklearn.ensemble import AdaBoostRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
case_train = campinas_isolamento_lagged[:294]
label_train = obitos[:294]

case_test = campinas_isolamento_lagged[294:]
label_test = obitos[294:]
reg = make_pipeline(StandardScaler(),
                    AdaBoostRegressor(random_state=0, n_estimators=200))
reg.fit(case_train, label_train)
r = reg.score(case_test, label_test)
r
campinas_train_predict = reg.predict(case_train)
campinas_test_predict = reg.predict(case_test)
plt.plot(case_train.index, label_train, label="real")
plt.plot(case_train.index, campinas_train_predict, label="Predicted")
plt.plot(case_test.index, label_test, label="real test")
plt.plot(case_test.index, campinas_test_predict, label="Predicted test")
plt.show()
plt.rcParams["figure.figsize"] = (60,20)
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(label_test, campinas_test_predict)
mse<jupyter_output><empty_output><jupyter_text>NN<jupyter_code>from sklearn.neural_network import MLPRegressor
reg = make_pipeline(StandardScaler(),
                    MLPRegressor(random_state=1, max_iter=8000, hidden_layer_sizes=(100,)))
reg.fit(case_train, label_train)
r = reg.score(case_test, label_test)
r
campinas_train_predict = reg.predict(case_train)
campinas_test_predict = reg.predict(case_test)
plt.plot(case_train.index, label_train, label="real")
plt.plot(case_train.index, campinas_train_predict, label="Predicted")
plt.plot(case_test.index, label_test, label="real test")
plt.plot(case_test.index, campinas_test_predict, label="Predicted test")
plt.rcParams["figure.figsize"] = (60,20)
plt.show()
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(label_test, campinas_test_predict)
mse<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1122552902568745, 'fraction_numerical': 0.015729319904945117, 'mean_word_length': 8.4524064171123, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '570668', 'license': 'MIT', 'n_tokens_mistral': 3335, 'n_tokens_neox': 3131, 'n_words': 491, 'path': 'notebooks/projeto_MO826_previsao.ipynb', 'repo_name': 'eitiyamamoto/MO826-projeto', 'size': '8843'}