Kaggle competition: Lyft Motion Prediction for Autonomous Vehicles
<SOS> metric: PostProcessorKernel Kaggle data source: lyft-motion-prediction-autonomous-vehicles<feature_engineering>
pd.options.display.max_columns=305
all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )<data_type_conversions>
paths = [
".. /input/lyft-best-performing-public-kernels/lyft-ensembling-raster-sizes.csv",
".. /input/lyft-best-performing-public-kernels/lyft-prediction-with-multi-mode-confidence.csv",
]
weights = [0.4, 0.6]
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )<feature_engineering>
conf_cols = np.array(["conf_0", "conf_1", "conf_2"] )
all_data['_totalDistance'] = all_data['rideDistance'] + all_data["walkDistance"] + all_data["swimDistance"]
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
all_data['_killsOverDistance'] = all_data['kills'] / all_data['_totalDistance']
all_data['_killPlacePerc'] = all_data['killPlace'] / all_data['maxPlace']
fillInf(all_data, 0 )<drop_column>
xy_cols = [[],[],[]]
for i in range(50):
for j in range(3):
xy_cols[j].append(f"coord_x{j}{i}")
 xy_cols[j].append(f"coord_y{j}{i}")
 xy_cols[0][:10]
agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','matchDuration','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col )<groupby>
COLUMNS = ["timestamp", "track_id"] + list(conf_cols)+ xy_cols[0] + xy_cols[1] + xy_cols[2]
def grouping(df):
group = df.groupby(['matchId','groupId','matchType'])
 gCount = group.size().to_frame('players')
 gMean = group.mean()
gMax = group[agg_col].max().rename(columns=lambda s: '_max.' + s)
 gMin = group[agg_col].min().rename(columns=lambda s: '_min.' + s)
 return pd.concat([gCount, gMean, gMax, gMin], axis=1 ).reset_index()
all_data = grouping(all_data)
 all_data = reduce_mem_usage(all_data)
 print(all_data.shape )<categorify>
def sort_df(df, sort_timestamp_track_id=True):
conf_orders = np.argsort(-df[conf_cols].values,1)
 XY = np.stack([df[xy_cols[0]].values,df[xy_cols[1]].values, df[xy_cols[2]].values], axis=1)
 XY = XY[np.arange(len(XY)) [:, None], conf_orders]
df2 = pd.DataFrame(columns = COLUMNS)
 df2["timestamp"] = df["timestamp"].values
df2["track_id"] = df["track_id"].values
df2[xy_cols[0] + xy_cols[1] + xy_cols[2]] = XY.reshape(-1,300)
 df2[conf_cols] = df[conf_cols].values[np.arange(len(df)) [:, None], conf_orders]
if sort_timestamp_track_id:
df2.sort_values(["timestamp", "track_id"], inplace=True)
 df2.reset_index(inplace=True, drop=True)
 return df2
group = all_data[np.r_[cols,['groupId']]].groupby(['matchId','groupId'])
 gcol = ['kills','teamKills','revives','damageDealt','walkDistance']
gSum = group[gcol].sum().rename(columns=lambda s: '_gsum.' + s)
 all_data = reduce_mem_usage(pd.merge(all_data, gSum.reset_index()))
for c in gcol:
all_data['_perc.gmean_gmax.' + c] = all_data[c] / all_data['_max.' + c]
del gSum
gc.collect()
print(all_data.shape )<groupby>
%%time
df = None
for path,w in zip(paths,weights):
print(w, path)
 temp = pd.read_csv(path)
 temp = sort_df(temp)
 temp[COLUMNS[5:]] *= w
if df is None:
df = temp
else:
df[COLUMNS[2:]] += temp[COLUMNS[2:]]
df[conf_cols] /= df[conf_cols].sum(1 ).values[:, None]
sample = pd.read_csv(".. /input/lyft-motion-prediction-autonomous-vehicles/multi_mode_sample_submission.csv")
 df = sample[["timestamp", "track_id"]].merge(df, on=["timestamp", "track_id"])
 sample.shape, df.shape
<groupby><EOS> 
df.to_csv("submission.csv", index=False, float_format='%.6f' )