Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<feature_engineering>
sns.set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_scale=1, color_codes=True)
train_df.loc[(train_df.building_id.isin(site_0_bids)) &(train_df.meter==0), 'meter_reading'] = train_df[(train_df.building_id.isin(site_0_bids)) &(train_df.meter==0)]['meter_reading'] * 0.2931<feature_engineering>
data= pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv")
 data["Date"] = data["Date"].apply(lambda x: x.replace("-",""))
data["Date"] = data["Date"].astype(int)
train_df['date'] = train_df['timestamp'].dt.date
train_df['meter_reading_log1p'] = np.log1p(train_df['meter_reading'] )<feature_engineering>
data = data.drop(['Province/State'],axis=1)
 data = data.dropna()
data.isnull().sum()
def preprocess(df):
df["hour"] = df["timestamp"].dt.hour
df["day"] = df["timestamp"].dt.day
df["weekend"] = df["timestamp"].dt.weekday
df["month"] = df["timestamp"].dt.month
df["dayofweek"] = df["timestamp"].dt.dayofweek 
<categorify>
test = pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv")
 test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int)
 test["Lat"] = test["Lat"].fillna(12.5211)
 test["Long"] = test["Long"].fillna(69.9683)
 test.isnull().sum()
preprocess(train_df )<sort_values>
x =data[['Lat', 'Long', 'Date']]
y1 = data[['ConfirmedCases']]
y2 = data[['Fatalities']]
x_test = test[['Lat', 'Long', 'Date']]
if use_ucf:
train_df = train_df.sort_values('month')
 train_df = train_df.reset_index()<categorify>
model = RandomForestClassifier(n_estimators=200 )
<count_missing_values>
model.fit(x,y1)
 pred1 = model.predict(x_test)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]
weather_train_df.isna().sum()<groupby>
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
n_estimators=150, random_state=None, n_jobs=1, verbose=0 )
weather_train_df.groupby('site_id' ).apply(lambda group: group.isna().sum() )<groupby>
model.fit(x,y2)
 pred2 = model.predict(x_test)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"]
weather_train_df = weather_train_df.groupby('site_id' ).apply(lambda group: group.interpolate(limit_direction='both'))<groupby>
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Sub.columns
sub_new = Sub[["ForecastId"]]
weather_train_df.groupby('site_id' ).apply(lambda group: group.isna().sum() )<data_type_conversions>
OP = pd.concat([pred1,pred2,sub_new],axis=1)
 OP.head()
OP.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
OP = OP[['ForecastId','ConfirmedCases', 'Fatalities']]
def add_lag_feature(weather_df, window=3):
group_df = weather_df.groupby('site_id')
 cols = ['air_temperature', 'cloud_coverage', 'dew_temperature', 'precip_depth_1_hr', 'sea_level_pressure', 'wind_direction', 'wind_speed']
rolled = group_df[cols].rolling(window=window, min_periods=0)
 lag_mean = rolled.mean().reset_index().astype(np.float16)
 lag_max = rolled.max().reset_index().astype(np.float16)
 lag_min = rolled.min().reset_index().astype(np.float16)
 lag_std = rolled.std().reset_index().astype(np.float16)
 for col in cols:
weather_df[f'{col}_mean_lag{window}'] = lag_mean[col]
weather_df[f'{col}_max_lag{window}'] = lag_max[col]
weather_df[f'{col}_min_lag{window}'] = lag_min[col]
weather_df[f'{col}_std_lag{window}'] = lag_std[col]<feature_engineering>
OP["ConfirmedCases"] = OP["ConfirmedCases"].astype(int)
 OP["Fatalities"] = OP["Fatalities"].astype(int)
<feature_engineering><EOS> 
OP.to_csv("submission.csv",index=False )