Kaggle competition: COVID19 Global Forecasting (Week 4)
features['Total_Bathrooms'] =(features['FullBath'] +(0.5 * features['HalfBath'])+
features['BsmtFullBath'] +(0.5 * features['BsmtHalfBath']))<feature_engineering>
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 sub = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )
features['Total_porch_sf'] =(features['OpenPorchSF'] + features['3SsnPorch'] +
features['EnclosedPorch'] + features['ScreenPorch'] +
features['WoodDeckSF'] )<feature_engineering>
train['Date'] = pd.to_datetime(train['Date'], infer_datetime_format=True)
 test['Date'] = pd.to_datetime(test['Date'], infer_datetime_format=True )
features['haspool'] = features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 features['has2ndfloor'] = features['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 features['hasgarage'] = features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 features['hasbsmt'] = features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 features['hasfireplace'] = features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )<categorify>
train.loc[:, 'Date'] = train.Date.dt.strftime('%y%m%d')
 train.loc[:, 'Date'] = train['Date'].astype(int)
 test.loc[:, 'Date'] = test.Date.dt.strftime('%y%m%d')
 test.loc[:, 'Date'] = test['Date'].astype(int )
print(features.shape)
 final_features = pd.get_dummies(features ).reset_index(drop=True)
 print(final_features.shape )<split>
train['Province_State'].fillna('nan', inplace=True)
 test['Province_State'].fillna('nan', inplace=True )
X = final_features.iloc[:len(y), :]
X_sub = final_features.iloc[len(X):, :]<drop_column>
from sklearn.preprocessing import LabelEncoder
outliers = [30, 88, 462, 631, 1322]
X = X.drop(X.index[outliers])
 y = y.drop(y.index[outliers] )<count_values>
s =(train.dtypes == 'object')
 object_cols = list(s[s].index )
overfit = []
for i in X.columns:
counts = X[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(X)* 100 > 99.94:
overfit.append(i )<concatenate>
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train['Province_State'] = label_encoder1.fit_transform(train['Province_State'])
 test['Province_State'] = label_encoder1.transform(test['Province_State'])
 train['Country_Region'] = label_encoder2.fit_transform(train['Country_Region'])
 test['Country_Region'] = label_encoder2.transform(test['Country_Region'])
overfit = list(overfit)
 overfit.append('MSZoning_C(all)' )<drop_column>
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.metrics import mean_squared_log_error
X = X.drop(overfit, axis=1 ).copy()
X_sub = X_sub.drop(overfit, axis=1 ).copy()<choose_model_class>
X_train = train[['Province_State','Country_Region','Date']]
X_train_with_confirmed = train[['Province_State','Country_Region','Date','ConfirmedCases']]
y_train_full = train[['ConfirmedCases', 'Fatalities']]
y_train_confirmed = train['ConfirmedCases']
y_train_fatal = train['Fatalities']
kfolds = KFold(n_splits=10, shuffle=True, random_state=42 )<compute_test_metric>
sub1 = pd.DataFrame()
sub1['ForecastID'] = test['ForecastId']
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<compute_test_metric>
del test['ForecastId']
def cv_rmse(model, X=X):
rmse = np.sqrt(-cross_val_score(model, X, y,
scoring="neg_mean_squared_error",
cv=kfolds))
return(rmse )<define_search_space>
alphas_alt = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas2 = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
e_alphas = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
e_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]<choose_model_class>
xgb = XGBRegressor(
 n_estimators = 500,
max_depth = 20,
learning_rate = 0.1,
)
ridge = make_pipeline(RobustScaler() ,
RidgeCV(alphas=alphas_alt, cv=kfolds))<compute_test_metric>
xgb.fit(X_train, y_train_confirmed )
score = cv_rmse(ridge)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<choose_model_class>
test_y_conf = xgb.predict(test)
 test_y_conf[test_y_conf < 0] = 0
lasso = make_pipeline(RobustScaler() ,
LassoCV(max_iter=1e7, alphas=alphas2,
random_state=42, cv=kfolds))<compute_test_metric>
test_y_conf = pd.DataFrame(test_y_conf )
score = cv_rmse(lasso)
 print("Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<choose_model_class>
test_y_conf.rename(columns={0: 'ConfirmedCases'}, inplace=True )
elasticnet = make_pipeline(RobustScaler() ,
ElasticNetCV(max_iter=1e7, alphas=e_alphas,
cv=kfolds, l1_ratio=e_l1ratio))<compute_test_metric>
sub1 = pd.concat([sub1, test_y_conf], axis=1 )
score = cv_rmse(elasticnet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<define_search_model>
test = pd.concat([test, test_y_conf], axis=1 )
svr = make_pipeline(RobustScaler() ,
SVR(C= 20, epsilon= 0.008, gamma=0.0003,))<compute_test_metric>
score = cv_rmse(svr)
 print("SVR score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<choose_model_class>
xgb1 = XGBRegressor(
 n_estimators = 500,
max_depth = 20,
learning_rate = 0.1,
)
gbr = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =42 )<compute_test_metric>
xgb1.fit(X_train_with_confirmed, y_train_fatal )
score = cv_rmse(gbr)
 print("GradientBoosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<choose_model_class>
test_y_fatal = xgb1.predict(test)
lightgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=5000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1,
)<compute_test_metric>
test_y_fatal = pd.DataFrame(test_y_fatal)
 test_y_fatal.rename(columns={0: 'Fatalities'}, inplace=True)
 test_y_fatal[test_y_fatal < 0] = 0
score = cv_rmse(lightgbm)
 print("Lightgbm score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<choose_model_class>
sub1 = pd.concat([sub1, test_y_fatal], axis=1 )
xgboost = XGBRegressor(learning_rate=0.01, n_estimators=3460,
max_depth=3, min_child_weight=0,
gamma=0, subsample=0.7,
colsample_bytree=0.7,
objective='reg:linear', nthread=-1,
scale_pos_weight=1, seed=27,
reg_alpha=0.00006 )<compute_test_metric>
sub1.to_csv("submission.csv" , index = False )
score = cv_rmse(xgboost)
 print("Xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )<choose_model_class>
stack_gen = StackingCVRegressor(regressors=(ridge, lasso, elasticnet,
gbr, xgboost, lightgbm),
meta_regressor=xgboost,
use_features_in_secondary=True )<train_model>