text_html_output_1.png text_plain_output_1.png
import numpy as np

import pandas as pd

import warnings

import gc
import numpy as np
import pandas as pd
from tqdm.notebook import tqdm
import time
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
from sklearn.svm import SVR as skSVR
from cuml.svm import SVR as cuSVR
def smape(y_true, y_pred, agg=True):
    smap = np.zeros(len(y_true))
    num = np.abs(y_true - y_pred)
    dem = (np.abs(y_true) + np.abs(y_pred)) / 2
    pos_ind = (y_true != 0) | (y_pred != 0)
    smap[pos_ind] = num[pos_ind] / dem[pos_ind]
    if agg == True:
        return 100 * np.mean(smap)
    else:
        return 100 * smap
BASE = '../input/godaddy-microbusiness-density-forecasting/'
VERSION = 29

COLS = ['GEO_ID', 'NAME', 'S0101_C01_026E']
df2017 = pd.read_csv('../input/census-data-for-godaddy/ACSST5Y2017.S0101-Data.csv', usecols=COLS)
df2017 = df2017.iloc[1:]
df2017['S0101_C01_026E'] = df2017['S0101_C01_026E'].astype('int')
df2018 = pd.read_csv('../input/census-data-for-godaddy/ACSST5Y2018.S0101-Data.csv', usecols=COLS)
df2018 = df2018.iloc[1:]
df2018['S0101_C01_026E'] = df2018['S0101_C01_026E'].astype('int')
df2019 = pd.read_csv('../input/census-data-for-godaddy/ACSST5Y2019.S0101-Data.csv', usecols=COLS)
df2019 = df2019.iloc[1:]
df2019['S0101_C01_026E'] = df2019['S0101_C01_026E'].astype('int')
df2020 = pd.read_csv('../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('../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')
df2017['cfips'] = df2017.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
df2018['cfips'] = df2018.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
df2019['cfips'] = df2019.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
df2020['cfips'] = df2020.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
df2021['cfips'] = df2021.GEO_ID.apply(lambda x: int(x.split('US')[-1]))
df2017['year'] = 2017 + 2
df2018['year'] = 2018 + 2
df2019['year'] = 2019 + 2
df2020['year'] = 2020 + 2
df2021['year'] = 2021 + 2
K = pd.concat((df2017, df2018, df2019, df2020, df2021))
del K['GEO_ID'], K['NAME']
K = K.rename(columns={'S0101_C01_026E': 'K'})
K

train = pd.read_csv(BASE + 'train.csv')
reaveal_test = pd.read_csv(BASE + 'revealed_test.csv')
train = pd.concat([train, reaveal_test]).sort_values(by=['cfips', 'first_day_of_month']).reset_index(drop=True)
test = pd.read_csv(BASE + 'test.csv')
drop_index = (test.first_day_of_month == '2022-11-01') | (test.first_day_of_month == '2022-12-01')
test = test.loc[~drop_index, :]
sub = pd.read_csv(BASE + 'sample_submission.csv')
train['istest'] = 0
test['istest'] = 1
raw = pd.concat((train, test)).sort_values(['cfips', 'row_id']).reset_index(drop=True)
coords = pd.read_csv('/kaggle/input/usa-counties-coordinates/cfips_location.csv')
raw = raw.merge(coords.drop('name', axis=1), on='cfips')
raw['first_day_of_month'] = pd.to_datetime(raw['first_day_of_month'])
raw['year'] = raw['first_day_of_month'].dt.year
raw['month'] = raw['first_day_of_month'].dt.month
raw['county'] = raw.groupby('cfips')['county'].ffill()
raw['state'] = raw.groupby('cfips')['state'].ffill()
raw['dcount'] = raw.groupby(['cfips'])['row_id'].cumcount()
raw['county_i'] = (raw['county'] + raw['state']).factorize()[0]
raw['state_i'] = raw['state'].factorize()[0]
raw = raw.merge(K, on=['cfips', 'year'], how='left')

raw['tmp'] = (100 * raw['active'] / raw['K']).round(decimals=4)
ind = raw['microbusiness_density'].notnull()
del raw['tmp']

for t in range(0, 7):
    raw[f'lag{t}'] = raw.groupby('cfips')['active'].shift(t)
    raw[f'lag{t}'] = raw.groupby('cfips')[f'lag{t}'].ffill()
raw.tail(10)