Kaggle competition: COVID19 Global Forecasting (Week 4)
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
features[col] = features[col].fillna(0)
 for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
features[col] = features[col].fillna('None')
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
features[col] = features[col].fillna('None' )<groupby>
df_train = pd.read_csv('.. /input/covid19-global-forecasting-week-4/train.csv')
 df_test = pd.read_csv('.. /input/covid19-global-forecasting-week-4/test.csv')
 df_train.head()
features['MSZoning'] = features.groupby('MSSubClass')['MSZoning'].transform(lambda x: x.fillna(x.mode() [0]))<define_variables>
def datesplit(df):
year = []
month = []
day = []
for item in df['Date']:
x = item.split("-")
 year.append(x[0])
 month.append(x[1])
 day.append(x[2])
 df['Year'] = year
df['Month'] = month
df['Day'] = day
datesplit(df_train)
 datesplit(df_test)
 df_train['ConfirmedCases'] = df_train['ConfirmedCases'].apply(int)
 df_train['Fatalities'] = df_train['Fatalities'].apply(int)
 df_train.head()
objects = []
for i in features.columns:
if features[i].dtype == object:
objects.append(i )<correct_missing_values>
df_train['Province_State'].fillna('',inplace=True)
 df_test['Province_State'].fillna('',inplace=True)
 lbe = LabelEncoder()
df_train['Country_Region'] = lbe.fit_transform(df_train['Country_Region'])
 df_test['Country_Region'] = lbe.transform(df_test['Country_Region'])
 df_train['Province_State'] = lbe.fit_transform(df_train['Province_State'])
 df_test['Province_State'] = lbe.transform(df_test['Province_State'])
features.update(features[objects].fillna('None'))<groupby>
X_train = df_train.drop(["Id", "ConfirmedCases", "Fatalities", "Date"], axis = 1)
 X_test = df_test.drop(["ForecastId","Date"], axis = 1)
 scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train.values)
 X_test = scaler.transform(X_test.values )
features['LotFrontage'] = features.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))<sort_values>
y1 = df_train['ConfirmedCases']
y2 = df_train['Fatalities']
skew_features = features[numerics2].apply(lambda x: skew(x)).sort_values(ascending=False )<filter>
y_train = y1
y_train_fat = y2
high_skew = skew_features[skew_features > 0.5]
skew_index = high_skew.index<concatenate>
xgb = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 xgb.fit(X_train,y_train )
for i in skew_index:
features[i] = boxcox1p(features[i], boxcox_normmax(features[i] + 1))<drop_column>
y_pred = xgb.predict(X_test)
 y_pred = np.around(y_pred,decimals = 0)
 y_pred
features = features.drop(['Utilities', 'Street', 'PoolQC',], axis=1 )<feature_engineering>
xgb1 = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 xgb1.fit(X_train,y_train_fat )
features['YrBltAndRemod']=features['YearBuilt']+features['YearRemodAdd']
features['TotalSF']=features['TotalBsmtSF'] + features['1stFlrSF'] + features['2ndFlrSF']<feature_engineering>
y_pred_fat = xgb1.predict(X_test)
 y_pred_fat = np.around(y_pred_fat,decimals = 0)
 y_pred_fat
features['Total_sqr_footage'] =(features['BsmtFinSF1'] + features['BsmtFinSF2'] +
features['1stFlrSF'] + features['2ndFlrSF'] )<feature_engineering>
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 soln = pd.DataFrame({'ForecastId': df_test.ForecastId, 'ConfirmedCases': y_pred, 'Fatalities': y_pred_fat})
 df_out = pd.concat([df_out, soln], axis=0)
 df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.ConfirmedCases = df_out.ConfirmedCases.astype('int')
 df_out.Fatalities = df_out.Fatalities.astype('int')
 df_out.to_csv('submission.csv', index=False )