text_plain_output_1.png
from tqdm.notebook import tqdm

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)

COLS = ['GEO_ID', 'NAME', 'S0101_C01_026E']
df2020 = pd.read_csv('/kaggle/input/census-data-for-godaddy/ACSST5Y2020.S0101-Data.csv', usecols=COLS)
df2020 = df2020.iloc[1:]
df2020['S0101_C01_026E'] = df2020['S0101_C01_026E'].astype('int')

df2021 = pd.read_csv('/kaggle/input/census-data-for-godaddy/ACSST5Y2021.S0101-Data.csv', usecols=COLS)
df2021 = df2021.iloc[1:]
df2021['S0101_C01_026E'] = df2021['S0101_C01_026E'].astype('int')
df2020['cfips'] = df2020.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
adult2020 = df2020.set_index('cfips').S0101_C01_026E.to_dict()
df2021['cfips'] = df2021.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
adult2021 = df2021.set_index('cfips').S0101_C01_026E.to_dict()

zero_cfips = list(train[train.microbusiness_density == 0].cfips.unique())
for cfips in zero_cfips:
    df = train[train.cfips == cfips].copy()
    df['microbusiness_density_imputation'] = df.iloc[-1][CFG.target_field]
    train.loc[train.cfips == cfips, CFG.target_field] = df['microbusiness_density_imputation']
train['active'].replace(0, 1, inplace=True)

for cfips in tqdm(test.cfips.unique()):
    test.loc[test.cfips == cfips, CFG.target_field] = train[train.cfips == cfips].iloc[-1][CFG.target_field]
test['adult2020'] = test.cfips.map(adult2020)
test['adult2021'] = test.cfips.map(adult2021)
test.microbusiness_density = test.microbusiness_density * test.adult2020 / test.adult2021
test[['row_id', CFG.target_field]].to_csv('submission.csv', index=False)