Kaggle competition: COVID19 Global Forecasting (Week 4)
del_col = []
for x in range(1851):
del_col.append('d_' + str(x+1))<drop_column>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf
df_eval = df_eval.drop(del_col, axis='columns' )<merge>
train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
df_eval = pd.merge(df_eval, df_cal, how='left', on='d' )<merge>
train['ConfirmedCases'] = train['ConfirmedCases'].apply(int)
 train['Fatalities'] = train['Fatalities'].apply(int)
 cases = train.ConfirmedCases
fatalities = train.Fatalities
del train['ConfirmedCases']
del train['Fatalities']
df_eval = pd.merge(df_eval, df_price, how='left', on=['item_id', 'wm_yr_wk', 'store_id'])
<filter>
train['Province_State'] = train['Province_State'].fillna("")
 test['Province_State'] = test['Province_State'].fillna("")
 train['geography'] = train['Country_Region']+train['Province_State']
test['geography'] = test['Country_Region']+test['Province_State']
le=LabelEncoder()
train['geography'] = le.fit_transform(train['geography'])
 test['geography'] = le.fit_transform(test['geography'] )
df_eval_test = df_eval.query('d == "d_1852"' )<drop_column>
train = train.drop(['Province_State','Country_Region'],axis=1)
 test = test.drop(['Province_State','Country_Region'],axis=1 )
df_eval_test = df_eval_test[['id', 'store_id', 'item_id', 'dept_id', 'cat_id', 'state_id', 'd', 'qty', 'sell_price']]<feature_engineering>
del train['Id']
del test['ForecastId']
df_eval_test['qty'] = df_eval_test['d'].apply(lambda x: int(x.replace(x, '0')) )<define_variables>
def create_features(df):
df['day'] = df['Date'].dt.day
df['month'] = df['Date'].dt.month
return df
tmp_df = df_eval_test<concatenate>
train['Date']=pd.to_datetime(train['Date'])
 test['Date']=pd.to_datetime(test['Date'])
 train = create_features(train)
 test = create_features(test)
 train = train.drop("Date",axis =1)
 test = test.drop("Date",axis =1)
for x in range(28):
df_eval_test = df_eval_test.append(tmp_df )<drop_column>
model_con = XGBRegressor(n_estimators = 3000 ,random_state = 0,max_depth = 20,learning_rate = 0.01
,objective = 'reg:squarederror',min_child_weight = 1,gamma = 0.7,n_thread = 4)
 model_con.fit(train, cases )
df_eval_test = df_eval_test.reset_index(drop=True )<define_variables>
lst_d = []
i = 0
lst_index = df_eval_test.index
for x in lst_index:
lst_d.append('d_' + str(((lst_index[i])// 30490)+ 1942))
i = i + 1<feature_engineering>
c = model_con.predict(test)
 for i in range(len(c)) :
if c[i]<0:
c[i] = 0
c[i] = int(c[i])
 c[i] = int(c[i])
df_eval_test['d'] = lst_d<merge>
con_pred = np.around(c,decimals = 0 )
df_eval_test = pd.merge(df_eval_test, df_cal, how='left', on='d' )<merge>
model_fal = XGBRegressor(n_estimators = 3000 ,random_state = 0,max_depth = 20,learning_rate = 0.01
,objective = 'reg:squarederror',min_child_weight = 1,gamma = 0.7,n_thread = 4)
 model_fal.fit(train,fatalities )
df_eval_test = pd.merge(df_eval_test, df_price, how='left', on=['item_id', 'wm_yr_wk', 'store_id'] )<set_options>
result_fal = model_fal.predict(test )
del tmp_df
gc.collect()<categorify>
for i in range(len(result_fal)) :
if result_fal[i]<0:
result_fal[i] = 0
result_fal[i] = int(result_fal[i])
 result_fal[i] = int(result_fal[i] )
df_eval = pd.get_dummies(data=df_eval, columns=['dept_id', 'cat_id', 'store_id', 'state_id'])
 df_eval_test = pd.get_dummies(data=df_eval_test, columns=['dept_id', 'cat_id', 'store_id', 'state_id'] )<drop_column>
fal_pre = np.around(result_fal,decimals = 0 )
df_eval_test = df_eval_test.drop(['sell_price_x', 'snap_CA', 'snap_TX', 'snap_WI'], axis='columns')
 df_eval_test = df_eval_test.rename(columns={'sell_price_y': 'sell_price'})
 df_eval = df_eval.drop(['snap_CA', 'snap_TX', 'snap_WI'], axis='columns' )<split>
submission['ConfirmedCases'] = con_pred
submission['Fatalities'] = fal_pre
target_col = 'qty'
exclude_cols = ['id', 'item_id', 'd', 'date', 'wm_yr_wk']
feature_cols = [col for col in df_eval.columns if col not in exclude_cols]
y = np.array(df_eval[target_col])
 X = np.array(df_eval[feature_cols])
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42 )<train_model>
submission.to_csv("submission.csv" , index = False )