Kaggle competition: COVID19 Global Forecasting (Week 4)
aggs = {
'grpSizeMult' : ['mean'],
'groupId_count' : ['mean'],
'matchId_count' : ['mean'],
'winPlacePerc' : ['mean'],
}
for c in feats:
if c not in aggs:
aggs[c] = ['mean', 'min', 'max', 'std']
new_cols = [k + '_' + agg for k in aggs.keys() for agg in aggs[k]]<groupby>
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.metrics import mean_squared_log_error
groups = concat.groupby('groupId' ).agg(aggs)
 groups.columns = new_cols<drop_column>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
del concat
gc.collect()<groupby>
train_df.isna().sum()
groups = reduce_mem_usage(groups )<choose_model_class>
test_df.isna().sum()
<define_search_model>
train_df['Province_State'].fillna('',inplace=True)
 test_df['Province_State'].fillna('',inplace=True)
 lb = LabelEncoder()
train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])
 test_df['Country_Region'] = lb.transform(test_df['Country_Region'])
 lb1 = LabelEncoder()
train_df['Province_State'] = lb.fit_transform(train_df['Province_State'])
 test_df['Province_State'] = lb.transform(test_df['Province_State'])
 def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date)
 year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']
del train_df['Year']
del test_df['Year']
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)
 train_df['Fatalities'] = train_df['Fatalities'].apply(int)
 cases = train_df.ConfirmedCases
fatalities = train_df.Fatalities
del train_df['ConfirmedCases']
del train_df['Fatalities']
scaler = MinMaxScaler()
X = scaler.fit_transform(train_df.values)
 x_test = scaler.transform(test_df.values)
LGBMRegressor()<define_search_model>
def kfold_lightgbm(df, num_folds, stratified = False, debug= False):
train_df = df[df['winPlacePerc_mean'].notnull() ]
test_df = df[df['winPlacePerc_mean'].isnull() ]
print("Starting LightGBM.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
del df
gc.collect()
if stratified:
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=1001)
 else:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=1001)
 oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
drop_features = ["winPlacePerc_mean", "Id", "groupId", "matchId"]
feats = [f for f in train_df.columns if f not in drop_features]
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df[feats], train_df['winPlacePerc_mean'])) :
train_x, train_y = train_df[feats].iloc[train_idx], train_df['winPlacePerc_mean'].iloc[train_idx]
valid_x, valid_y = train_df[feats].iloc[valid_idx], train_df['winPlacePerc_mean'].iloc[valid_idx]
reg = LGBMRegressor(num_leaves=100,
learning_rate=.03,
n_estimators=1000,
min_data_in_leaf=100, 
)
 reg.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
eval_metric= 'mae', verbose= 50, early_stopping_rounds= 200)
 oof_preds[valid_idx] = reg.predict(valid_x)
 sub_preds += reg.predict(test_df[feats])/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = reg.feature_importances_
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d MAE : %.6f' %(n_fold + 1, mean_absolute_error(valid_y, oof_preds[valid_idx])))
 del reg, train_x, train_y, valid_x, valid_y
gc.collect()
print('Full MAE score %.6f' % mean_absolute_error(train_df['winPlacePerc_mean'], oof_preds))
if not debug:
test_df['winPlacePerc'] = sub_preds
return feature_importance_df, test_df[['winPlacePerc']]<concatenate>
xg = XGBRegressor(n_estimators = 10*1000 , random_state = 0 , max_depth = 15)
 xg.fit(X,cases)
 cases_pred = xg.predict(x_test)
 cases_pred[cases_pred < 0] = 0
feat_importances, test_df = kfold_lightgbm(groups, num_folds=5, stratified=False, debug=False )<compute_test_metric>
display_importances(feat_importances )<sort_values>
X_cas = []
for i in range(len(X)) :
x = list(X[i])
 x.append(cases[i])
 X_cas.append(x)
 X_cas = np.array(X_cas)
feat_gb = feat_importances.groupby('feature' ).mean().sort_values(by="importance", ascending=False )<save_to_csv>
rf = XGBRegressor(n_estimators = 10000 , random_state = 0 , max_depth = 15)
 rf.fit(X,fatalities)
 fatalities_pred = rf.predict(x_test)
 fatalities_pred[fatalities_pred < 0] = 0
feat_gb.to_csv("feature_importance.csv" )<load_from_csv>
submission_df['ConfirmedCases'] = cases_pred
submission_df['Fatalities'] = fatalities_pred
test = pd.read_csv(".. /input/test.csv" )<merge>
submission_df.to_csv("submission.csv" , index = False )