Kaggle competition: COVID19 Global Forecasting (Week 4)
for i in range(20,29):
submission.loc[submission['id'].str.contains('CA_1'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_2'), 'F'+str(i)] *= 1.03
submission.loc[submission['id'].str.contains('CA_3'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_4'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('TX_1'), 'F'+str(i)] *= 1.02
submission.loc[submission['id'].str.contains('TX_2'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('TX_3'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('WI_1'), 'F'+str(i)] *= 1.03
submission.loc[submission['id'].str.contains('WI_2'), 'F'+str(i)] *= 1.04
submission.loc[submission['id'].str.contains('WI_3'), 'F'+str(i)] *= 1.03
print(submission['F'+str(i)].sum() )<save_to_csv>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
submission.to_csv('submission.csv', index=False )<set_options>
train_df['Province_State'].fillna("",inplace = True)
 test_df['Province_State'].fillna("",inplace = True)
 train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']
warnings.filterwarnings('ignore')
<load_from_csv>
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )
sales = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 sales.name = 'sales'
calendar = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 calendar.name = 'calendar'
prices = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv')
 prices.name = 'prices'<categorify>
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
for d in range(1942,1970):
col = 'd_' + str(d)
 sales[col] = 0
sales[col] = sales[col].astype(np.int16 )<define_variables>
train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']
sales_bd = np.round(sales.memory_usage().sum() /(1024*1024),1)
 calendar_bd = np.round(calendar.memory_usage().sum() /(1024*1024),1)
 prices_bd = np.round(prices.memory_usage().sum() /(1024*1024),1 )<data_type_conversions>
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
def downcast(df):
cols = df.dtypes.index.tolist()
types = df.dtypes.values.tolist()
for i,t in enumerate(types):
if 'int' in str(t):
if df[cols[i]].min() > np.iinfo(np.int8 ).min and df[cols[i]].max() < np.iinfo(np.int8 ).max:
df[cols[i]] = df[cols[i]].astype(np.int8)
 elif df[cols[i]].min() > np.iinfo(np.int16 ).min and df[cols[i]].max() < np.iinfo(np.int16 ).max:
df[cols[i]] = df[cols[i]].astype(np.int16)
 elif df[cols[i]].min() > np.iinfo(np.int32 ).min and df[cols[i]].max() < np.iinfo(np.int32 ).max:
df[cols[i]] = df[cols[i]].astype(np.int32)
 else:
df[cols[i]] = df[cols[i]].astype(np.int64)
 elif 'float' in str(t):
if df[cols[i]].min() > np.finfo(np.float16 ).min and df[cols[i]].max() < np.finfo(np.float16 ).max:
df[cols[i]] = df[cols[i]].astype(np.float16)
 elif df[cols[i]].min() > np.finfo(np.float32 ).min and df[cols[i]].max() < np.finfo(np.float32 ).max:
df[cols[i]] = df[cols[i]].astype(np.float32)
 else:
df[cols[i]] = df[cols[i]].astype(np.float64)
 elif t == np.object:
if cols[i] == 'date':
df[cols[i]] = pd.to_datetime(df[cols[i]], format='%Y-%m-%d')
 else:
df[cols[i]] = df[cols[i]].astype('category')
 return df
sales = downcast(sales)
 prices = downcast(prices)
 calendar = downcast(calendar )<define_variables>
test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']
<categorify><EOS> 
rf = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 rf.fit(x_train,cases)
 cases_pred = rf.predict(x_test)
 cases_pred = np.around(cases_pred,decimals = 0)
 x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]
x_train_cas = np.array(x_train_cas)
 rf = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 rf.fit(x_train_cas,fatalities)
 x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]
x_test_cas = np.array(x_test_cas)
 fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred = np.around(fatalities_pred,decimals = 0)
 submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred
submission.to_csv("submission.csv" , index = False )