Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<drop_column>
%matplotlib inline
new_train.drop("date", axis=1, inplace=True)
 new_test.drop("date", axis=1, inplace=True )<categorify>
train = pd.read_csv('.. /input/covid19-global-forecasting-week-1/train.csv')
 test = pd.read_csv('.. /input/covid19-global-forecasting-week-1/test.csv' )
new_train = pd.concat([new_train,pd.get_dummies(new_train['Province/State'], prefix='ps')],axis=1)
 new_train.drop(['Province/State'],axis=1, inplace=True)
 new_test = pd.concat([new_test,pd.get_dummies(new_test['Province/State'], prefix='ps')],axis=1)
 new_test.drop(['Province/State'],axis=1, inplace=True )<categorify>
train['Date']= pd.to_datetime(train['Date'])
 test['Date']= pd.to_datetime(test['Date'] )
new_train = pd.concat([new_train,pd.get_dummies(new_train['Country/Region'], prefix='cr')],axis=1)
 new_train.drop(['Country/Region'],axis=1, inplace=True)
 new_test = pd.concat([new_test,pd.get_dummies(new_test['Country/Region'], prefix='cr')],axis=1)
 new_test.drop(['Country/Region'],axis=1, inplace=True )<prepare_x_and_y>
new_train = train.set_index(['Date'])
 new_test = test.set_index(['Date'] )
y_train = new_train["Fatalities"]<drop_column>
new_train.isnull().sum()
X_train = new_train.drop(["Fatalities", "ConfirmedCases"], axis=1 )<import_modules>
new_test = new_test.drop(["ForecastId"], axis=1 )
import xgboost as xgb
from xgboost import plot_importance, plot_tree
from sklearn.metrics import mean_squared_error, mean_absolute_error<choose_model_class>
new_train = new_train.drop(["Id"], axis=1 )
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
new_train[['Province/State']] = new_train[['Province/State']].fillna('' )
reg.fit(X_train, y_train, verbose=True )<prepare_x_and_y>
new_train.isnull().sum()
y_train = train["ConfirmedCases"]<choose_model_class>
new_test[['Province/State']] = new_test[['Province/State']].fillna('' )
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
new_test.isnull().sum()
reg.fit(X_train, y_train, verbose=True )<groupby>
import plotly.express as px
y_train = train.groupby(["Country/Region"] ).ConfirmedCases.pct_change(periods=1 )<categorify>
confirmiedcases = pd.DataFrame(new_train.groupby('Country/Region')['ConfirmedCases'].sum())
 confirmiedcases['Country/Region'] = confirmiedcases.index
y_train = y_train.replace(np.nan, 0 )<define_variables>
confirmiedcases.index = np.arange(1,164 )
y_train = y_train.replace(np.inf, 0 )<choose_model_class>
global_confirmiedcases = confirmiedcases[['Country/Region','ConfirmedCases']]
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
formated_gdf = new_train.groupby(['Date', 'Country/Region'])['ConfirmedCases'].max()
formated_gdf = formated_gdf.reset_index()
formated_gdf['Date'] = pd.to_datetime(formated_gdf['Date'])
 formated_gdf['Date'] = formated_gdf['Date'].dt.strftime('%m/%d/%Y')
 formated_gdf['size'] = formated_gdf['ConfirmedCases'].pow(0.3)
 fig = px.scatter_geo(formated_gdf, locations="Country/Region", locationmode='country names',
color="ConfirmedCases", size='size', hover_name="Country/Region",
range_color= [0, 1500],
projection="natural earth", animation_frame="Date",
title='CORONA: Spread Over Time From Jan 2020 to Mar 2020', color_continuous_scale="portland")
 fig.show()
reg.fit(X_train, y_train, verbose=True )<train_model>
def create_time_features(df):

 df['date'] = df.index
df['hour'] = df['date'].dt.hour
df['dayofweek'] = df['date'].dt.dayofweek
df['quarter'] = df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear
df['dayofmonth'] = df['date'].dt.day
df['weekofyear'] = df['date'].dt.weekofyear
X = df[['hour','dayofweek','quarter','month','year',
'dayofyear','dayofmonth','weekofyear']]
return X
y_train = train["ConfirmedCases"]
confirmed_reg = xgb.XGBRegressor(n_estimators=1000)
 confirmed_reg.fit(X_train, y_train, verbose=True)
 preds = confirmed_reg.predict(new_test)
 preds = np.array(preds)
 preds[preds < 0] = 0
preds = np.round(preds, 0 )<prepare_output>
create_time_features(new_train)
 create_time_features(new_test )
preds = np.array(preds )<load_from_csv>
new_train.drop("date", axis=1, inplace=True)
 new_test.drop("date", axis=1, inplace=True )
submission_new = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv" )<prepare_output>
new_train = pd.concat([new_train,pd.get_dummies(new_train['Province/State'], prefix='ps')],axis=1)
 new_train.drop(['Province/State'],axis=1, inplace=True)
 new_test = pd.concat([new_test,pd.get_dummies(new_test['Province/State'], prefix='ps')],axis=1)
 new_test.drop(['Province/State'],axis=1, inplace=True )
submission_new["ConfirmedCases"]=pd.Series(preds )<predict_on_test>
new_train = pd.concat([new_train,pd.get_dummies(new_train['Country/Region'], prefix='cr')],axis=1)
 new_train.drop(['Country/Region'],axis=1, inplace=True)
 new_test = pd.concat([new_test,pd.get_dummies(new_test['Country/Region'], prefix='cr')],axis=1)
 new_test.drop(['Country/Region'],axis=1, inplace=True )
y_train = train["Fatalities"]
confirmed_reg = xgb.XGBRegressor(n_estimators=1000)
 confirmed_reg.fit(X_train, y_train, verbose=True)
 preds = confirmed_reg.predict(new_test)
 preds = np.array(preds)
 preds[preds < 0] = 0
preds = np.round(preds, 0)
 submission_new["Fatalities"]=pd.Series(preds )<save_to_csv>
y_train = new_train["Fatalities"]
submission_new.to_csv('submission.csv',index=False )<import_modules>
X_train = new_train.drop(["Fatalities", "ConfirmedCases"], axis=1 )
from fastai.tabular import *<define_variables>
import xgboost as xgb
from xgboost import plot_importance, plot_tree
from sklearn.metrics import mean_squared_error, mean_absolute_error
path = Path('/kaggle/input/covid19-global-forecasting-week-1/')
 path.ls()<load_from_csv>
reg = xgb.XGBRegressor(n_estimators=1000 )
sample_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv")
 train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv" )<merge>
reg.fit(X_train, y_train, verbose=True )
Full_data = pd.merge(test, train, on=['Lat','Long','Date','Country/Region','Province/State'])
 Full_data.rename(columns={'Province/State':'Province'}, inplace=True)
 Full_data.rename(columns={'Country/Region':'Country'}, inplace=True)
 Full_data.rename(columns={'ConfirmedCases':'Confirmed'}, inplace=True)
 Full_data.head()<rename_columns>
y_train = train["ConfirmedCases"]
train.rename(columns={'Province/State':'Province'}, inplace=True)
 train.rename(columns={'Country/Region':'Country'}, inplace=True)
 train.rename(columns={'ConfirmedCases':'Confirmed'}, inplace=True )<rename_columns>
reg = xgb.XGBRegressor(n_estimators=1000 )
test.rename(columns={'Province/State':'Province'}, inplace=True)
 test.rename(columns={'Country/Region':'Country'}, inplace=True )<categorify>
reg.fit(X_train, y_train, verbose=True )
bridge_types =('Lat', 'Date', 'Province', 'Country', 'Long', 'Confirmed',
'ForecastId', 'Id')
 countries = pd.DataFrame(train, columns=['Country'])
 labelencoder = LabelEncoder()
train['Countries'] = labelencoder.fit_transform(train['Country'])
 train['Countries'].head() 
<feature_engineering>
y_train = train.groupby(["Country/Region"] ).ConfirmedCases.pct_change(periods=1 )
<data_type_conversions>
y_train = y_train.replace(np.nan, 0 )
train['Date']= pd.to_datetime(train['Date'])
 test['Date']= pd.to_datetime(test['Date'] )<drop_column>
y_train = y_train.replace(np.inf, 0 )
train = train.set_index(['Date'])
 test = test.set_index(['Date'] )<feature_engineering>
reg = xgb.XGBRegressor(n_estimators=1000 )
def create_time_features(df):

 df['date'] = df.index
df['hour'] = df['date'].dt.hour
df['dayofweek'] = df['date'].dt.dayofweek
df['quarter'] = df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear
df['dayofmonth'] = df['date'].dt.day
df['weekofyear'] = df['date'].dt.weekofyear
X = df[['hour','dayofweek','quarter','month','year',
'dayofyear','dayofmonth','weekofyear']]
return X<drop_column>
reg.fit(X_train, y_train, verbose=True )
train.drop("date", axis=1, inplace=True)
 test.drop("date", axis=1, inplace=True )<categorify>
y_train = train["ConfirmedCases"]
confirmed_reg = xgb.XGBRegressor(n_estimators=1000)
 confirmed_reg.fit(X_train, y_train, verbose=True)
 preds = confirmed_reg.predict(new_test)
 preds = np.array(preds)
 preds[preds < 0] = 0
preds = np.round(preds, 0 )
test['Countries'] = labelencoder.fit_transform(test['Country'])
<count_missing_values>
preds = np.array(preds )
train.isnull().sum()<count_missing_values>
submission_new = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv" )
train.isnull().sum()<drop_column>
submission_new["ConfirmedCases"]=pd.Series(preds )
train.drop(['Country'], axis=1, inplace=True)
 train.drop(['Province'], axis=1, inplace=True )<drop_column>
y_train = train["Fatalities"]
confirmed_reg = xgb.XGBRegressor(n_estimators=1000)
 confirmed_reg.fit(X_train, y_train, verbose=True)
 preds = confirmed_reg.predict(new_test)
 preds = np.array(preds)
 preds[preds < 0] = 0
preds = np.round(preds, 0)
 submission_new["Fatalities"]=pd.Series(preds )
<define_variables><EOS> 
submission_new.to_csv('submission.csv',index=False )