Kaggle competition: COVID19 Global Forecasting (Week 4)
test_id = test_set['Id']<drop_column>
PATH_WEEK4 = '/kaggle/input/covid19-global-forecasting-week-4'
df_Train = pd.read_csv(f'{PATH_WEEK4}/train.csv', parse_dates=["Date"], engine='python')
 df_Test = pd.read_csv(f'{PATH_WEEK4}/test.csv' )
del test_set
del train
del match
del tmatch
del feature_list
del keep_list<import_modules>
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
<split>
df_Train.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_Test.rename(columns={'Country_Region':'Country'}, inplace=True)
 EMPTY_VAL = "EMPTY_VAL"
df_Train.rename(columns={'Province_State':'State'}, inplace=True)
 df_Train['State'].fillna(EMPTY_VAL, inplace=True)
 df_Train['State'] = df_Train.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 df_Test.rename(columns={'Province_State':'State'}, inplace=True)
 df_Test['State'].fillna(EMPTY_VAL, inplace=True)
 df_Test['State'] = df_Test.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 df_Test['Date'] = pd.to_datetime(df_Test['Date'], infer_datetime_format=True)
<import_modules>
MIN_TEST_DATE = df_Test.Date.min()
df_train = df_Train.copy()
y1_Train = df_train.iloc[:, -2]
y2_Train = df_train.iloc[:, -1]
import lightgbm as lgb<choose_model_class>
def extractDate(df, colName = 'Date'):

 assert colName in df.columns
df = df.assign(
 day = df.loc[:, colName].dt.day,
month = df.loc[:, colName].dt.month,
dayofyear = df.loc[:, colName].dt.dayofyear)
 return df
my_model = lgb.LGBMRegressor(n_estimators = 10000,
objective= 'xentropy',
metric='l1',
learning_rate = 0.05,
bagging_fraction = 0.9,
colsample_bytree = 0.8,
num_leaves=31 )<train_model>
def createNewDataset(df):

 df_New = df.copy()
df_New = extractDate(df_New)
 df_New.drop(columns=['Date'], axis=1, inplace=True)
 return df_New
my_model.fit(X, y,
eval_set=[(X, y)],
eval_metric= 'l1',
early_stopping_rounds=15,
)<compute_test_metric>
X_Train = createNewDataset(df_train)
 X_Test = createNewDataset(df_Test )
<predict_on_test>
def RMSLE(pred, actual):
return np.sqrt(np.mean(np.power(( np.log(pred + 1)- np.log(actual + 1)) , 2)) )
test_pred = my_model.predict(test_pred )<feature_engineering>
filterwarnings('ignore' )
test_pred[test_pred > 1] = 1
test_pred[test_pred < 0] = 0<create_dataframe>
def modelPredict(model, X_Test):
return model.predict(X_Test )
df = pd.DataFrame(test_pred)
 df.columns = ['winPlacePerc']<concatenate>
def modelFit(model, X_Train, y_Train):
model.fit(X_Train, y_Train )
df = pd.concat([test_id,df],axis=1 )<save_to_csv>
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 df_Test_Out = pd.DataFrame()
LEncoder = LabelEncoder()
pfC = PolynomialFeatures(degree = 3)
 pfF = PolynomialFeatures(degree = 3)
 modelC = LinearRegression()
modelF = LinearRegression()
countries = X_Train.Country.unique().tolist()
for country in countries:
states = X_Train.loc[X_Train.Country == country, :].State.unique().tolist()
for state in states:
categoricalFeatures = ['Country', 'State']
df_Test_Updated = pd.DataFrame()
X_Train_CS = X_Train.loc[(X_Train.Country == country)&(X_Train.State == state), :]
yC_Train_CS = X_Train_CS.loc[:, 'ConfirmedCases'].to_frame()
yF_Train_CS = X_Train_CS.loc[:, 'Fatalities'].to_frame()
X_Train_CS.drop(columns=['Id', 'ConfirmedCases', 'Fatalities', 'day', 'month'], axis=1, inplace=True)
 X_Train_CS.loc[:, 'Country'] = LEncoder.fit_transform(X_Train_CS.loc[:, 'Country'])
 X_Train_CS.loc[:, 'State'] = LEncoder.fit_transform(X_Train_CS.loc[:, 'State'])
 X_Test_CS = X_Test.loc[(X_Test.Country == country)&(X_Test.State == state), :]
df_Test_Updated = df_Test_Updated.append(X_Test_CS)
 X_Test_CS_Id = X_Test_CS.loc[:, 'ForecastId']
X_Test_CS.drop(columns=['ForecastId', 'day', 'month'], axis=1, inplace=True)
 X_Test_CS.loc[:, 'Country'] = LEncoder.fit_transform(X_Test_CS.loc[:, 'Country'])
 X_Test_CS.loc[:, 'State'] = LEncoder.fit_transform(X_Test_CS.loc[:, 'State'])
 scTrain = StandardScaler()
X_Train_CS_SS = scTrain.fit_transform(X_Train_CS)
 X_Test_CS_SS = scTrain.transform(X_Test_CS)
 scC = StandardScaler()
yC_Train_CS_SS = scC.fit_transform(yC_Train_CS)
 scF = StandardScaler()
yF_Train_CS_SS = scF.fit_transform(yF_Train_CS)
 modelC.fit(pfC.fit_transform(X_Train_CS_SS), yC_Train_CS_SS)
 yC_Pred = scC.inverse_transform(modelC.predict(pfC.fit_transform(X_Test_CS_SS)))
 modelF.fit(pfF.fit_transform(X_Train_CS_SS), yF_Train_CS_SS)
 yF_Pred = scF.inverse_transform(modelF.predict(pfF.fit_transform(X_Test_CS_SS)))
 yC_Pred[yC_Pred < 0] = yC_Pred[yC_Pred < 0] * -1
yF_Pred[yF_Pred < 0] = yF_Pred[yF_Pred < 0] * -1
df = pd.DataFrame({'ForecastId': X_Test_CS_Id, 'ConfirmedCases': yC_Pred.flatten() , 'Fatalities': yF_Pred.flatten() })
 df_out = pd.concat([df_out, df], axis=0)
 df_Test_Updated['ConfirmedCases'] = yC_Pred
df_Test_Updated['Fatalities'] = yF_Pred
df_Test_Out = df_Test_Out.append(df_Test_Updated)
df.to_csv('xen_retry_V8.csv', index=False )<set_options>
df_out.ForecastId = df_out.ForecastId.astype('int' )
%matplotlib inline
%pylab inline
print(os.listdir(".. /input"))
warnings.filterwarnings(action='ignore' )<load_from_csv>
df_out.to_csv('submission.csv', index=False )
df_train = pd.read_csv('.. /input/train_V2.csv')
 df = reduce_mem_usage(df_train)
 df.info()<import_modules>
df_Test_Out.loc[df_Test_Out.Country == 'India', :]
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor
from IPython.display import display
from sklearn import metrics
from scipy.cluster import hierarchy as hc
from pdpbox import pdp
from plotnine import *
from fastai.imports import *
from fastai.structured import *<filter>
df_Test_Out['Date'] = df_Test_Out.apply(lambda x : '2020-' + str(x.month)+ '-' + str(x.day), axis=1)
 df_Test_Out['Date'] = pd.to_datetime(df_Test_Out['Date'], infer_datetime_format=True )