application_vnd.jupyter.stderr_output_1.png
import numpy as np

import pandas as pd

import tensorflow as tf

train = pd.read_csv('/kaggle/input/godaddy-microbusiness-density-forecasting/train.csv')
test = pd.read_csv('/kaggle/input/godaddy-microbusiness-density-forecasting/test.csv')
revealed_test = pd.read_csv('/kaggle/input/godaddy-microbusiness-density-forecasting/revealed_test.csv')
census = pd.read_csv('/kaggle/input/godaddy-microbusiness-density-forecasting/census_starter.csv')
revealed_test_dates = revealed_test.first_day_of_month.unique()
train = pd.concat([train, revealed_test])
train.index = np.arange(0, len(train))

dates = train.first_day_of_month.unique()
dates

unique_cfips = list(train.cfips.unique())

def smape(y_true, y_pred):
    denominator = (y_true + tf.abs(y_pred)) / 200.0
    diff = tf.abs(y_true - y_pred) / denominator
    diff = tf.where(denominator == 0, 0.0, diff)
    return tf.reduce_mean(diff)
def cauclate_smape(item):
    cfips = item.iloc[0].cfips
    y_true = tf.constant(item['microbusiness_density'], dtype=tf.float64)
    y_pred = tf.constant(item['prediction'], dtype=tf.float64)
    return smape(y_true, y_pred).numpy()

train['prediction'] = 0
for i in range(len(dates) - 5):
    date = dates[i]
    df = train[train.first_day_of_month == date].copy()
    last_value_dict = dict()
    for j in range(len(df)):
        last_value_dict[df.iloc[j].cfips] = df.iloc[j].microbusiness_density
    validate_dates = dates[i + 3:i + 6]
    for date in validate_dates:
        df = train[train.first_day_of_month == date].copy()
        train.loc[train.first_day_of_month == date, 'prediction'] = df.cfips.apply(lambda cfips: last_value_dict[cfips])
    smapes = train[train.first_day_of_month.isin(validate_dates)].groupby('first_day_of_month').apply(cauclate_smape)
    print(f'Last Value Date:{date} Validation Date:{validate_dates}')
    print(smapes)
    print('Validation Score')
    print(np.mean(smapes))