text_html_output_1.png
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping

from keras.initializers import random_uniform

from keras.layers import Dense, Activation, Dropout

from keras.layers import GRU

from keras.models import Sequential

from keras.optimizers import Adagrad

from sklearn.metrics import mean_squared_log_error

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_squared_log_error
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers import GRU
from keras.initializers import random_uniform
from keras.optimizers import Adagrad
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from keras import backend as K
import tensorflow as tf
import datetime
import matplotlib.pyplot as plt
plt.style.use('ggplot')
font = {'family': 'meiryo'}
plt.rc('font', **font)

train_df = pd.read_csv('../input/covid19-global-forecasting-week-4/train.csv')
train_df = train_df.fillna('No State')
train_df

test_rate = 0.1
time_series_len = 18
train_date_count = len(set(train_df['Date']))
X, Y = ([], [])
scaler = StandardScaler()
train_df['ConfirmedCases_std'] = scaler.fit_transform(train_df['ConfirmedCases'].values.reshape(len(train_df['ConfirmedCases'].values), 1))
for state, country in train_df.groupby(['Province_State', 'Country_Region']).sum().index:
    df = train_df[(train_df['Country_Region'] == country) & (train_df['Province_State'] == state)]
    if df['ConfirmedCases'].sum() != 0:
        for i in range(len(df) - time_series_len):
            X.append(df[['ConfirmedCases_std']].iloc[i:i + time_series_len].values)
            Y.append(df[['ConfirmedCases_std']].iloc[i + time_series_len].values)
X = np.array(X)
Y = np.array(Y)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_rate, shuffle=True, random_state=0)

def huber_loss(y_true, y_pred, clip_delta=1.0):
    error = y_true - y_pred
    cond = tf.keras.backend.abs(error) < clip_delta
    squared_loss = 0.5 * tf.keras.backend.square(error)
    linear_loss = clip_delta * (tf.keras.backend.abs(error) - 0.5 * clip_delta)
    return tf.where(cond, squared_loss, linear_loss)
def huber_loss_mean(y_true, y_pred, clip_delta=1.0):
    return tf.keras.backend.mean(huber_loss(y_true, y_pred, clip_delta))

epochs_num = 20
n_in = 1
model = Sequential()
model.add(GRU(100, batch_input_shape=(None, time_series_len, n_in), kernel_initializer=random_uniform(seed=0), return_sequences=False))
model.add(Dense(50))
model.add(Dropout(0.15, seed=0))
model.add(Dense(n_in, kernel_initializer=random_uniform(seed=0)))
model.add(Activation('linear'))
opt = Adagrad(lr=0.01, epsilon=1e-08, decay=0.0001)
model.compile(loss=huber_loss_mean, optimizer=opt)

callbacks = [ReduceLROnPlateau(monitor='loss', patience=4, verbose=1, factor=0.6), EarlyStopping(monitor='loss', patience=10)]
hist = model.fit(X_train, Y_train, batch_size=20, epochs=epochs_num, callbacks=callbacks, shuffle=False)

predicted_std = model.predict(X_test)
result_std = pd.DataFrame(predicted_std)
result_std.columns = ['predict']
result_std['actual'] = Y_test

predicted = scaler.inverse_transform(predicted_std)
Y_test2 = scaler.inverse_transform(Y_test)

np.sqrt(mean_squared_log_error(predicted, Y_test2))