Kaggle competition: COVID19 Global Forecasting (Week 4)
train_data = lgb.Dataset(data=X_train, label=Y_train)
 valid_data = lgb.Dataset(data=X_valid, label=Y_valid)
 params = {
'num_leaves': 144,
"metric" : "mae",
'learning_rate': 0.1,
'n_estimators': 800,
'max_depth':13,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9
}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000 )<drop_column>
train_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission_csv = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )
X_test = test.drop(["Id", "groupId", "matchId"], axis = 1 )<save_to_csv>
convert_dict = {'Province_State': str,'Country_Region':str,'ConfirmedCases':int,'Fatalities':int}
convert_dict_test = {'Province_State': str,'Country_Region':str}
train_data = train_data.astype(convert_dict)
 test_data = test_data.astype(convert_dict_test )
Y_test = lgb_model.predict(X_test)
 submission = pd.DataFrame({
"Id": test['Id'],
"winPlacePerc": Y_test
})
 submission.to_csv('sample_submission_20181124_v1.csv', index=False )<set_options>
train_data['Date'] = pd.to_datetime(train_data['Date'], infer_datetime_format=True)
 test_data['Date'] = pd.to_datetime(test_data['Date'], infer_datetime_format=True )
sns.set_style('darkgrid')
 sns.set_palette('bone')
 pd.options.display.float_format = '{:,.3f}'.format
print(os.listdir(".. /input"))<concatenate>
train_data.loc[:, 'Date'] = train_data.Date.dt.strftime('%m%d')
 train_data.loc[:, 'Date'] = train_data['Date'].astype(int)
 test_data.loc[:, 'Date'] = test_data.Date.dt.strftime('%m%d')
 test_data.loc[:, 'Date'] = test_data['Date'].astype(int )
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))<load_from_csv>
train_data['Country_Region'] = np.where(train_data['Province_State'] == 'nan',train_data['Country_Region'],train_data['Province_State']+' '+train_data['Country_Region'])
 test_data['Country_Region'] = np.where(test_data['Province_State'] == 'nan',test_data['Country_Region'],test_data['Province_State']+' '+test_data['Country_Region'])
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )<count_missing_values>
train_data = train_data.drop(columns=['Province_State'])
 test_data = test_data.drop(columns=['Province_State'] )
null_cnt = train.isnull().sum().sort_values()
print('null count:', null_cnt[null_cnt > 0])
 train.dropna(inplace=True )<count_unique_values>
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index )
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )<count_values>
from sklearn.preprocessing import LabelEncoder
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )<drop_column>
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train_data['Country_Region'] = label_encoder2.fit_transform(train_data['Country_Region'])
 test_data['Country_Region'] = label_encoder2.transform(test_data['Country_Region'])
print(group['players in group'].nlargest(5))
del match,group<count_unique_values>
Test_id = test_data.ForecastId
( train.groupby('matchId')['matchDuration'].nunique() > 1 ).any()<count_unique_values>
train_data.drop(['Id'], axis=1, inplace=True)
 test_data.drop('ForecastId', axis=1, inplace=True )
<count_unique_values>
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column>0] )
print('match count:', train['matchId'].nunique())
 maxPlacePerc = train.groupby('matchId')['winPlacePerc'].max()
print('match [not contains 1st place]:', len(maxPlacePerc[maxPlacePerc != 1]))
del maxPlacePerc
sub = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(sub.groupby('matchId')))
 print(' - unique winPlacePerc:', sub['winPlacePerc'].unique() )<concatenate>
from xgboost import XGBRegressor
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )<set_options>
X_train = train_data[['Country_Region','Date']]
y_train = train_data[['ConfirmedCases', 'Fatalities']]
grpSolo.nlargest(5, 'kills' )<count_unique_values>
x_train = X_train.iloc[:,:].values
x_test = test_data.iloc[:,:].values
grpTeam.nlargest(5, 'kills' )<drop_column>
del grpSolo, grpTeam<filter>
error_list = []
def return_error(estimator, x_train,x_test,y_train):
model = MultiOutputRegressor(XGBRegressor(n_estimators=estimator, random_state=42, max_depth=40))
model.fit(x_train, y_train)
 predict = MultiOutputRegressor(model.predict(x_test))
return predict
def printMatchStats(matchIds):
for mid in matchIds:
subMatch = train[train['matchId'] == mid]
print('matchType:', subMatch['matchType'].values[0])
 grp1st = subMatch[subMatch['winPlacePerc'] == 1]
grpOther = subMatch[subMatch['winPlacePerc'] != 1]
print('players'.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(len(subMatch), len(grp1st), len(grpOther)))
 for c in ['kills','teamKills','roadKills','DBNOs','revives','assists']:
print(c.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(subMatch[c].sum() , grp1st[c].sum() , grpOther[c].sum()))
print('-' * 30 )<count_unique_values>
predict = return_error(2000,x_train,x_test,y_train )
<groupby><EOS> 
df_sub = pd.DataFrame()
df_sub['ForecastId'] = Test_id
df_sub['ConfirmedCases'] = np.round(predict.estimator[:,0],0)
 df_sub['Fatalities'] = np.round(predict.estimator[:,1],0)
 df_sub.to_csv('submission.csv', index=False )