Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<predict_on_test>
pd.set_option('display.max_columns', 500)
 warnings.filterwarnings("ignore")
 folder = ".. /input/"
print(os.listdir(folder))
NORMFACTOR = 1000
TRAIN_SIZE = 0.9
RANDOM_STATE = 212
EARLY_STOP_ROUNDS = 10
DEBUG = False
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<count_missing_values>
cID = ['Id', 'groupId', 'matchId']
cOrigGP= ['assists', 'boosts', 'damageDealt',
'DBNOs', 'headshotKills', 'heals',
'kills', 'killStreaks', 'longestKill', 'revives',
'rideDistance', 'roadKills', 'swimDistance', 'teamKills',
'vehicleDestroys', 'walkDistance', 'weaponsAcquired', 'killPlace']
cRKAll = ['matchDuration', 'killPoints', 'maxPlace', 'numGroups','rankPoints','winPoints']
cRKPop =['matchDuration', 'maxPlace', 'numGroups']
cMT = ['matchType']
cY = ['winPlacePerc']
cAgg = ['totalKills','netKills', 'totalDistance','teamwork','itemUse','netKillsPerDist','netItemsPerDist','headsPerKill','netWeaponsPerDist']
cGPAggMOnly = ['teamSize']
cGP = cOrigGP+cAgg
cGPAggM = cGP+cGPAggMOnly
cGPNZip = [(col, col+"N")for col in cGP]
cGPN = [col+"N" for col in cGP]
cGPT = [col+"T" for col in cGP]
cGPM = [col+"M" for col in cGPAggM]
cGPMInherit = [col+"M" for col in cGP]
cGPAll = cGPAggM+cGPT+cGPM+cGPN
def preprocess(train, cOrigFeat):
train = train.copy()
cOrig = train.columns.values
train['teamSizeFeat'] = train.groupby('groupId')['groupId'].transform('count')
 train['totalKills'] = train['DBNOs'] + train['kills'] + train['teamKills']
train['totalDistance'] = train['rideDistance'] + train['walkDistance'] + train['walkDistance']
train['netKills'] = train['DBNOs'] + train['kills'] - train['teamKills']
train['teamwork'] = train['assists'] - train['teamKills']
train['itemUse'] = train['boosts'] + train['heals']
train['netKillsPerDist'] = train['netKills']/(train['totalDistance']+1)
 train['netItemsPerDist'] = train['itemUse']/(train['totalDistance']+1)
 train['headsPerKill'] = train['headshotKills']/(train['totalKills']+1)
 train['netWeaponsPerDist'] = train['weaponsAcquired']/(train['totalDistance']+1)
 train['totalMatchPlayers'] = train.groupby('matchId')['Id'].transform('count')
 train['killsInvolved'] = train['DBNOs'] + train['kills'] + train['teamKills'] + train['assists']
train['pctMatchKills'] = train['totalKills']/train['totalMatchPlayers']
train['pctMatchKillInvolvement'] = train['killsInvolved']/train['totalMatchPlayers']
cEng = [col for col in train.columns.values if col not in cOrig]
cBase = list(cOrigFeat)+ list(cEng)
 train['teamSize'] = train['teamSizeFeat']
train['sampleWeight'] = 1/train['teamSize']
cNotFeatures = [col for col in train.columns.values if col not in cBase]
print('features done, scaling by match')
 train[cBase] = minMidMaxGroupScale(train.groupby('matchId'), 0.75, base=train, col=cBase)
 print('match aggregation done, aggregating teams')
 train = train.merge(train.groupby('groupId')[cBase].transform(np.min), how='left', suffixes=["","_min"], left_index=True, right_index=True)
 train = train.merge(train.groupby('groupId')[cBase].transform(np.max), how='left', suffixes=["","_max"], left_index=True, right_index=True)
 train = train.merge(train.groupby('groupId')[cBase].transform(np.mean), how='left', suffixes=["","_mean"], left_index=True, right_index=True)
 train[cBase] = train.groupby('groupId')[cBase].transform(np.sum)
 cDerived = [col for col in train.columns.values if col not in cBase+cNotFeatures]
cAll = cBase+cDerived
return(train, cAll)
 def minMidMaxGroupScale(g, midQuant, base, col=cGPAggM):
gMax = g[col].transform(np.max)
 print("max done, quantiles")
 g75 = g[col].transform(lambda x: np.quantile(x, midQuant))
print("quantiles done, mins")
 gMin = g[col].transform(np.min)
 print("mins done, time for big calc")
 outdf =(base[col] <= g75[col])*(base[col]-gMin[col])/(g75[col]-gMin[col] ).replace(to_replace=0,value=1)*midQuant+(base[col] > g75[col])*(( base[col]-g75[col])/(gMax[col]-g75[col] ).replace(to_replace=0,value=1)*(1-midQuant)+ midQuant)
 print("big calc done")
 return outdf
data.isna().sum() [lambda x: x>0]<define_variables>
def shape(df):
return '{:,} rows - {:,} columns'.format(df.shape[0], df.shape[1])
 def train_validation(df, train_size=TRAIN_SIZE):
unique_games = df.matchId.unique()
train_index = round(int(unique_games.shape[0]*train_size))
np.random.shuffle(unique_games)
 train_id = unique_games[:train_index]
validation_id = unique_games[train_index:]
train = df[df.matchId.isin(train_id)]
validation = df[df.matchId.isin(validation_id)]
return train, validation
const_cols = ['MSSubClass','BldgType','HouseStyle','OverallQual','OverallCond','YearBuilt','YearRemodAdd','RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','MasVnrArea','ExterQual','ExterCond','Foundation','WoodDeckSF','PavedDrive']<data_type_conversions>
df = pd.read_csv(folder+'train_V2.csv' )
data['MSSubClass']=data.MSSubClass.replace('150','120' ).astype(object )<compute_test_metric>
time_0 = datetime.datetime.now()
df = reduce_mem_usage(df)
 df, cAll = preprocess(df, cOrigGP)
 print('Feature engineering done')
 df = reduce_mem_usage(df)
 time_1 = datetime.datetime.now()
print('Preprocessing took {} seconds'.format(( time_1 - time_0 ).seconds))
df.head()
adaptative_correlation(data.OverallQual,data.tmpPrice )<categorify>
train, validation = train_validation(df )
data['OverallQual'].replace([1, 2], 3, inplace=True)
 data['OverallQual'].replace([10], 9, inplace=True )<data_type_conversions>
train_weights =(1/train['teamSize'])
 validation_weights =(1/validation['teamSize'] )
data['OverallQual'] = data.OverallQual.astype('int' )<drop_column>
X_train = train[cAll]
X_test = validation[cAll]
y_train = train[cY]
y_test = validation[cY]
shape(X_train), shape(X_test )
data = data.drop(['RoofMatl'],axis=1 )<count_values>
time_0 = datetime.datetime.now()
lgbm = LGBMRegressor(objective='mae', n_estimators=250,
learning_rate=0.3, num_leaves=200,
n_jobs=-1, random_state=RANDOM_STATE, verbose=0)
 lgbm.fit(X_train, y_train, sample_weight=train_weights,
eval_set=[(X_test, y_test)], eval_sample_weight=[validation_weights],
eval_metric='mae', early_stopping_rounds=EARLY_STOP_ROUNDS,
verbose=0)
 time_1 = datetime.datetime.now()
print('Training took {} seconds.Best iteration is {}'.format(( time_1 - time_0 ).seconds, lgbm.best_iteration_))
data.OverallCond.value_counts()<categorify>
print('Mean Absolute Error is {:.5f}'.format(mean_absolute_error(y_test, lgbm.predict(X_test, num_iteration=lgbm.best_iteration_), sample_weight=validation_weights)))
 print('R2 score is {:.2%}'.format(r2_score(y_test, lgbm.predict(X_test, num_iteration=lgbm.best_iteration_), sample_weight=validation_weights)) )
mean_encoder(data,['MSSubClass','BldgType','HouseStyle','OverallCond','RoofStyle','Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond','Foundation','PavedDrive'])
<filter>
test = reduce_mem_usage(pd.read_csv(folder+'test_V2.csv'))
data[data.YearBuilt>data.YearRemodAdd]<feature_engineering>
time_0 = datetime.datetime.now()
test, cAll = preprocess(test, cOrigGP)
 time_1 = datetime.datetime.now()
print('Preprocessing took {} seconds'.format(( time_1 - time_0 ).seconds))
data.loc[data.YearBuilt>data.YearRemodAdd,'YearRemodAdd']=2002<feature_engineering>
y_sub_unfull = lgbm.predict(X_sub, num_iteration=lgbm.best_iteration_ )
data['YearBuilt'] = np.power(np.abs(85-(2010 - data.YearBuilt)) ,2 )<predict_on_test>
sub = test[['Id']]
sub['winPlacePerc'] = y_sub_unfull
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<define_variables>
sub.to_csv('sub_LGBM_unfull.csv',index=False )
basement_cols = ['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinSF1','BsmtFinType2','BsmtFinSF2','BsmtUnfSF','TotalBsmtSF','BsmtFullBath','BsmtHalfBath']<compute_test_metric>
X_train = df[cAll]
y_train = df[cY]
train_weights =(1/df['teamSize'] )
adaptative_correlation(data.BsmtFinSF2,data.tmpPrice )<drop_column>
time_0 = datetime.datetime.now()
lgbm = LGBMRegressor(objective='mae', n_estimators=250,
learning_rate=0.3, num_leaves=200,
n_jobs=-1, random_state=RANDOM_STATE, verbose=1)
 lgbm.fit(X_train, y_train, sample_weight=train_weights)
 time_1 = datetime.datetime.now()
print('Training took {} seconds.Best iteration is {}'.format(( time_1 - time_0 ).seconds, lgbm.best_iteration_))
data = data.drop(['BsmtFinSF2'],axis=1 )<categorify>
y_sub_full = lgbm.predict(X_sub, num_iteration=lgbm.best_iteration_)
 sub2 = test[['Id']]
sub2['winPlacePerc'] = y_sub_full
<compute_test_metric><EOS> 
sub2.to_csv('sub_LGBM_full.csv',index=False )