Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<import_modules>
warnings.filterwarnings('ignore')
 %matplotlib inline
py.init_notebook_mode(connected=True)
from pandas_profiling import ProfileReport<load_from_csv>
debug = True
if debug == True:
df_train = pd.read_csv('.. /input/train_V2.csv', nrows=1000)
 df_test = pd.read_csv('.. /input/test_V2.csv', nrows=1000)
 else:
df_train = pd.read_csv('.. /input/train_V2.csv')
 df_test = pd.read_csv('.. /input/test_V2.csv' )
xtrain = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/train.csv')
 xtest = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/test.csv')
 xsubmission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/submission.csv')
 xtrain.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtest.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtrain.rename(columns={'Province_State':'State'}, inplace=True)
 xtest.rename(columns={'Province_State':'State'}, inplace=True)
 xtrain.State=xtrain.State.fillna('NA')
 xtest.State=xtest.State.fillna('NA')
 xtrain['Date'] = pd.to_datetime(xtrain['Date'], infer_datetime_format=True)
 xtest['Date'] = pd.to_datetime(xtest['Date'], infer_datetime_format=True)
 for j in range(14):
train_lag=xtrain.groupby(['Country','State'] ).shift(periods=j+1)
 xtrain['lag_'+str(j+1)+'_ConfirmedCases']=train_lag['ConfirmedCases'].fillna(0)
 xtrain['lag_'+str(j+1)+'_Fatalities']=train_lag['Fatalities'].fillna(0)
<prepare_x_and_y>
df_train[df_train['groupId']=='4d4b580de459be']
xtrain.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtest.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtrain.rename(columns={'Province_State':'State'}, inplace=True)
 xtest.rename(columns={'Province_State':'State'}, inplace=True)
 xtrain['Date'] = pd.to_datetime(xtrain['Date'], infer_datetime_format=True)
 xtest['Date'] = pd.to_datetime(xtest['Date'], infer_datetime_format=True)
 xtrain.info()
xtest.info()
for_y=xtest.merge(xtrain,on=['State','Country','Date'],how='inner')
 y1_xTrain = xtrain.ConfirmedCases
y1_xTrain.head()
y1_xTest = for_y.ConfirmedCases
y1_xTest.head()
y2_xTrain = xtrain.Fatalities
y2_xTrain.head()
y2_xTest = for_y.Fatalities
y2_xTest.head()
EMPTY_VAL = "NA"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state 
<data_type_conversions>
len(df_train[df_train['matchId']=='a10357fd1a4a91'] )
X_xTrain = xtrain.copy()
X_xTrain['State'].fillna(EMPTY_VAL, inplace=True)
 X_xTrain['State'] = X_xTrain.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_xTrain.loc[:, 'Date_ACT'] = X_xTrain.Date
X_xTrain.loc[:, 'Date'] = X_xTrain.Date.dt.strftime("%m%d")
 X_xTrain["Date"] = X_xTrain["Date"].astype(int)
 print(X_xTrain.head())
 X_xTest = for_y.copy()
X_xTest['State'].fillna(EMPTY_VAL, inplace=True)
 X_xTest['State'] = X_xTest.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_xTest.loc[:, 'Date'] = X_xTest.Date.dt.strftime("%m%d")
 X_xTest["Date"] = X_xTest["Date"].astype(int)
 print(X_xTest.head() )<categorify>
df_train = df_train[df_train['Id']!='f70c74418bb064']
lec = preprocessing.LabelEncoder()
les = preprocessing.LabelEncoder()
X_xTrain.Country = lec.fit_transform(X_xTrain.Country)
 X_xTrain['State'] = les.fit_transform(X_xTrain['State'])
 print(X_xTrain.head())
 X_xTest.Country = lec.transform(X_xTest.Country)
 X_xTest['State'] = les.transform(X_xTest['State'])
 print(X_xTest.head())
 xtrain.loc[xtrain.Country == 'Afghanistan', :]
print(xtest.tail() )<categorify>
headshot = df_train[['kills','winPlacePerc','headshotKills']]
headshot['headshotrate'] = headshot['kills'] / headshot['headshotKills']
filterwarnings('ignore')
 le = preprocessing.LabelEncoder()
countries = X_xTrain.Country.unique() 
<init_hyperparams>
del headshot
<define_variables>
df_train['headshotrate'] = df_train['kills']/df_train['headshotKills']
df_test['headshotrate'] = df_test['kills']/df_test['headshotKills']
xout = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 k_log_col=['lag_'+str(j+1)+'_ConfirmedCases' for j in range(14)]+['lag_'+str(j+1)+'_Fatalities' for j in range(14)]
params = {'min_child_weight':[4,5], 'gamma':[i/10.0 for i in range(3,6)], 'subsample':[i/10.0 for i in range(6,8)],
'colsample_bytree':[i/10.0 for i in range(6,8)], 'max_depth': [3,4,5],'n_estimators':[500,1000],'learning_rate': [.03, 0.05,.07],
'objective':['reg:squaredlogerror']}
params = {'min_child_weight':[4,5], 'max_depth': [6],'n_estimators':[1000]}
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)))
 X_xTrain_CS = X_xTrain.loc[:, ['State', 'Country', 'Date','Date_ACT', 'ConfirmedCases', 'Fatalities']+k_log_col]
y1_xTrain_CS = X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['ConfirmedCases']-X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['lag_1_ConfirmedCases']
y2_xTrain_CS = X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['Fatalities']-X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['lag_1_Fatalities']
X_xTrain_CS1=X_xTrain_CS[(X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'])>0].loc[:, ['State', 'Country']+k_log_col].reset_index(drop=True)
 oh1=preprocessing.OneHotEncoder(sparse=False ).fit(X_xTrain_CS.Country.values.reshape(X_xTrain_CS.Country.shape[0],1))
oh2=preprocessing.OneHotEncoder(sparse=False ).fit(X_xTrain_CS.State.values.reshape(X_xTrain_CS.Country.shape[0],1))
all_val=oh1.transform(X_xTrain_CS1.Country.values.reshape(X_xTrain_CS1.Country.shape[0],1))
col=['cnty_'+str(k)for k in range(all_val.shape[1])]
X_xTrain_CS1=pd.concat([X_xTrain_CS1,pd.DataFrame(all_val,columns=col)],axis=1)
 all_val_State=oh2.transform(X_xTrain_CS1.State.values.reshape(X_xTrain_CS1.State.shape[0],1))
col_State=['state_'+str(k)for k in range(all_val_State.shape[1])]
X_xTrain_CS1=pd.concat([X_xTrain_CS1,pd.DataFrame(all_val_State,columns=col_State)],axis=1)
 X_xTest_CS = X_xTest.loc[:, ['State', 'Country', 'Date', 'ForecastId']+k_log_col]
y1_xTest_CS = for_y.loc[:, 'ConfirmedCases']-for_y.loc[:, 'lag_1_ConfirmedCases']
y2_xTest_CS = for_y.loc[:, 'Fatalities']-for_y.loc[:, 'lag_1_Fatalities']
X_xTest_CS_Id = X_xTest_CS.loc[:, 'ForecastId']
X_xTest_CS1 = X_xTest_CS.loc[:, ['State', 'Country']+k_log_col]
all_val=oh1.transform(X_xTest_CS1.Country.values.reshape(X_xTest_CS1.Country.shape[0],1))
col=['cnty_'+str(k)for k in range(all_val.shape[1])]
X_xTest_CS1=pd.concat([X_xTest_CS1,pd.DataFrame(all_val,columns=col)],axis=1)
 all_val_State=oh2.transform(X_xTest_CS1.State.values.reshape(X_xTest_CS1.State.shape[0],1))
col_State=['state_'+str(k)for k in range(all_val_State.shape[1])]
X_xTest_CS1=pd.concat([X_xTest_CS1,pd.DataFrame(all_val_State,columns=col_State)],axis=1)
<train_on_grid>
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()
xgb1 = XGBRegressor(nthread=-1,n_jobs=-1)
 grid = GridSearchCV(xgb1, params)
 grid.fit(X_xTrain_CS1, y1_xTrain_CS)
<find_best_params>
del healthitems
print(r2_score(y1_xTest_CS, np.where(grid.best_estimator_.predict(X_xTest_CS1)<0,0,grid.best_estimator_.predict(X_xTest_CS1))))
print(RMSLE(np.where(y1_xTest_CS<=0,0.001,y1_xTest_CS), np.where(grid.best_estimator_.predict(X_xTest_CS1)<=0,0.001,grid.best_estimator_.predict(X_xTest_CS1))))
xgb2 = XGBRegressor(nthread=-1,n_jobs=-1)
 grid1 = GridSearchCV(xgb2, params)
 grid1.fit(X_xTrain_CS1, y2_xTrain_CS)
 print(r2_score(y2_xTest_CS, np.where(grid1.best_estimator_.predict(X_xTest_CS1)<0,0,grid1.best_estimator_.predict(X_xTest_CS1))))
print(RMSLE(np.where(y2_xTest_CS<=0,0.001,y2_xTest_CS), np.where(grid1.best_estimator_.predict(X_xTest_CS1)<=0,0.001,grid1.best_estimator_.predict(X_xTest_CS1))))
print(r2_score(y2_xTest_CS, grid1.best_estimator_.predict(X_xTest_CS1)))
 print(RMSLE(y2_xTest_CS, grid1.best_estimator_.predict(X_xTest_CS1)))
 y2_xpred = grid1.best_estimator_.predict(X_xTest_CS1)
<categorify>
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()
X_xTrain_CS1_bk=X_xTrain_CS1.copy()
X_xTrain_CS12=X_xTrain_CS.copy()
all_val=oh1.transform(X_xTrain_CS12.Country.values.reshape(X_xTrain_CS12.Country.shape[0],1))
col=['cnty_'+str(k)for k in range(all_val.shape[1])]
X_xTrain_CS12=pd.concat([X_xTrain_CS12,pd.DataFrame(all_val,columns=col)],axis=1)
 all_val_State=oh2.transform(X_xTrain_CS12.State.values.reshape(X_xTrain_CS12.State.shape[0],1))
col_State=['state_'+str(k)for k in range(all_val_State.shape[1])]
X_xTrain_CS12=pd.concat([X_xTrain_CS12,pd.DataFrame(all_val_State,columns=col_State)],axis=1)
 X_xTrain_CS12=X_xTrain_CS12[X_xTrain_CS1.columns].copy()
X_xTrain_CS12['Date_ACT']=X_xTrain_CS['Date_ACT']
X_xTrain_CS12['Fatalities']=X_xTrain_CS['Fatalities']
X_xTrain_CS12['ConfirmedCases']=X_xTrain_CS['ConfirmedCases']<define_variables>
del df_train,df_test;
gc.collect()
for k in range(60):
X_xTrain_CS12
prev=X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ]
lt=[ 'lag_1_ConfirmedCases','lag_2_ConfirmedCases', 'lag_3_ConfirmedCases',
'lag_4_ConfirmedCases', 'lag_5_ConfirmedCases', 'lag_6_ConfirmedCases',
'lag_7_ConfirmedCases', 'lag_8_ConfirmedCases', 'lag_9_ConfirmedCases',
'lag_10_ConfirmedCases', 'lag_11_ConfirmedCases',
'lag_12_ConfirmedCases', 'lag_13_ConfirmedCases',
'lag_14_ConfirmedCases', 'lag_1_Fatalities','lag_2_Fatalities',
'lag_3_Fatalities', 'lag_4_Fatalities', 'lag_5_Fatalities',
'lag_6_Fatalities', 'lag_7_Fatalities', 'lag_8_Fatalities',
'lag_9_Fatalities', 'lag_10_Fatalities', 'lag_11_Fatalities',
'lag_12_Fatalities', 'lag_13_Fatalities', 'lag_14_Fatalities']
lt1=['ConfirmedCases','lag_1_ConfirmedCases', 'lag_2_ConfirmedCases', 'lag_3_ConfirmedCases',
'lag_4_ConfirmedCases', 'lag_5_ConfirmedCases', 'lag_6_ConfirmedCases',
'lag_7_ConfirmedCases', 'lag_8_ConfirmedCases', 'lag_9_ConfirmedCases',
'lag_10_ConfirmedCases', 'lag_11_ConfirmedCases',
'lag_12_ConfirmedCases', 'lag_13_ConfirmedCases',
'Fatalities', 'lag_1_Fatalities', 'lag_2_Fatalities',
'lag_3_Fatalities', 'lag_4_Fatalities', 'lag_5_Fatalities',
'lag_6_Fatalities', 'lag_7_Fatalities', 'lag_8_Fatalities',
'lag_9_Fatalities', 'lag_10_Fatalities', 'lag_11_Fatalities',
'lag_12_Fatalities', 'lag_13_Fatalities']
prev[lt]=X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ][lt1]
cc=grid.best_estimator_.predict(X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ][ ['State', 'Country']+k_log_col+col+col_State])
 prev['ConfirmedCases']=prev['lag_1_ConfirmedCases']+np.where(cc<=0,0.001,cc)
 Fl=grid1.best_estimator_.predict(X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ][ ['State', 'Country']+k_log_col+col+col_State])
 prev['Fatalities']=prev['lag_1_Fatalities']+np.where(Fl<=0,0.001,Fl)
 prev['Date_ACT']=X_xTrain_CS12.Date_ACT.max() + datetime.timedelta(days=1)
 prev.index=prev.index+1
X_xTrain_CS12=X_xTrain_CS12.append(prev)
<create_dataframe>
%%time
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
x_train, y_train, train_columns, _ = feature_engineering(True,False)
 x_test, _, _ , test_idx = feature_engineering(False,True)
X_xTrain_CS2=X_xTrain_CS12.copy()<categorify>
%%time
x_train['headshotrate'] = x_train['kills']/x_train['headshotKills']
x_test['headshotrate'] = x_test['kills']/x_test['headshotKills']
x_train['killStreakrate'] = x_train['killStreaks']/x_train['kills']
x_test['killStreakrate'] = x_test['killStreaks']/x_test['kills']
x_train['healthitems'] = x_train['heals'] + x_train['boosts']
x_test['healthitems'] = x_test['heals'] + x_test['boosts']
del x_train['heals'];del x_test['heals']
train_columns.append('headshotrate')
 train_columns.append('killStreakrate')
 train_columns.append('healthitems')
 train_columns.remove('heals' )
X_xTrain_CS2['Country_1']=lec.inverse_transform(X_xTrain_CS2['Country'])
 X_xTrain_CS2['State_1']=les.inverse_transform(X_xTrain_CS2['State'] )<load_from_csv>
%%time
x_train = reduce_mem_usage(x_train)
 x_test = reduce_mem_usage(x_test )
xtest = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/test.csv')
 xsubmission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/submission.csv')
 xtest.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtest.rename(columns={'Province_State':'State'}, inplace=True)
 EMPTY_VAL = "NA"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
xtest['Date'] = pd.to_datetime(xtest['Date'], infer_datetime_format=True)
 xtest['State'].fillna(EMPTY_VAL, inplace=True)
 xtest['State'] = xtest.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 xtest<merge>
warnings.filterwarnings("ignore")
xtest1=xtest.merge(X_xTrain_CS2,left_on=['State','Country','Date'],right_on=['State_1','Country_1','Date_ACT'],how='inner')
 xtest1<filter>
%%time
folds = KFold(n_splits=3,random_state=6)
 oof_preds = np.zeros(x_train.shape[0])
 sub_preds = np.zeros(x_test.shape[0])
 start = time.time()
valid_score = 0
feature_importance_df = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(x_train, y_train)) :
trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]
val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 params = {"objective" : "regression",
"metric" : "mae",
'n_estimators':1500,
'early_stopping_rounds':100,
"num_leaves" : 31,
"learning_rate" : 0.05,
"bagging_fraction" : 0.9,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.7
}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)
 oof_preds[oof_preds>1] = 1
oof_preds[oof_preds<0] = 0
sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration)
 sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train_columns
fold_importance_df["importance"] = lgb_model.feature_importance()
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 gc.collect()
end = time.time()
print("Take Time :",(end-start))
xtest1[(xtest1['Country_x']=='US')&(xtest1['State_x']=='New York')][['Date','ConfirmedCases','Fatalities']]<filter>
%%time
df_test = pd.read_csv('.. /input/' + 'test_V2.csv')
 pred = sub_preds
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
if(i + 1)% 100000 == 0:
print(i, flush=True, end=" " )
<save_to_csv><EOS> 
%%time
df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )