Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<choose_model_class>
sns.set()
gene_dens = KDE(endog = mini_df.Yards, exog = mini_df.YardLine, dep_type='c', indep_type='c', bw='normal_reference' )<split>
%%time
X_train_df = pd.read_csv(PATH+"X_train.csv")
 X_test_df = pd.read_csv(PATH+"X_test.csv")
 Y_train_df = pd.read_csv(PATH+"y_train.csv")
 sub = pd.read_csv(PATH+"sample_submission.csv" )
iter_test = env.iter_test()<prepare_x_and_y>
def missing_data(data):
total = data.isnull().sum()
percent =(data.isnull().sum() /data.isnull().count() *100)
 tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 types = []
for col in data.columns:
dtype = str(data[col].dtype)
 types.append(dtype)
 tt['Types'] = types
return(np.transpose(tt))
for(test_df, sample_prediction_df)in iter_test:
mini_test = test_df[['PlayId','YardLine','FieldPosition','PlayDirection']].drop_duplicates('PlayId')
 Y = mini_test.YardLine.iloc[0]
FP = test_df.FieldPosition.iloc[0]
PD = test_df.PlayDirection.iloc[0]
train_mini_df = mini_df[(mini_df.FieldPosition == FP)&(mini_df.PlayDirection == PD)].reset_index()
dens = KDE(endog = train_mini_df.Yards, exog = train_mini_df.YardLine, dep_type='c', indep_type='c', bw=gene_dens.bw)
 pred_value = dens.cdf(endog, [Y]*199)
 pred_value[198] = 1
pred_value[pred_value>1]=1
pred_value[pred_value<0]=0
sample_prediction_df.iloc[0] = pred_value
if sample_prediction_df.iloc[0].isnull().any() ==1 :
pred_value = gene_dens.cdf(endog,[Y]*199)
 pred_value[198] = 1
pred_value[pred_value>1]=1
pred_value[pred_value<0]=0
sample_prediction_df.iloc[0] = pred_value
env.predict(sample_prediction_df)
<save_to_csv>
missing_data(X_train_df )
env.write_submission_file()<set_options>
missing_data(X_test_df )
sns.set_style('darkgrid')
 mpl.rcParams['figure.figsize'] = [15,10]
pd.set_option('mode.chained_assignment', None )<find_best_params>
missing_data(Y_train_df )
env = nflrush.make_env()<load_from_csv>
def _kurtosis(x):
return kurtosis(x)
 def CPT5(x):
den = len(x)*np.exp(np.std(x))
return sum(np.exp(x)) /den
def skewness(x):
return skew(x)
 def SSC(x):
x = np.array(x)
 x = np.append(x[-1], x)
 x = np.append(x,x[1])
 xn = x[1:len(x)-1]
xn_i2 = x[2:len(x)]
xn_i1 = x[0:len(x)-2]
ans = np.heaviside(( xn-xn_i1)*(xn-xn_i2),0)
 return sum(ans[1:])
 def wave_length(x):
x = np.array(x)
 x = np.append(x[-1], x)
 x = np.append(x,x[1])
 xn = x[1:len(x)-1]
xn_i2 = x[2:len(x)]
return sum(abs(xn_i2-xn))
def norm_entropy(x):
tresh = 3
return sum(np.power(abs(x),tresh))
def SRAV(x):
SRA = sum(np.sqrt(abs(x)))
 return np.power(SRA/len(x),2)
 def mean_abs(x):
return sum(abs(x)) /len(x)
 def zero_crossing(x):
x = np.array(x)
 x = np.append(x[-1], x)
 x = np.append(x,x[1])
 xn = x[1:len(x)-1]
xn_i2 = x[2:len(x)]
return sum(np.heaviside(-xn*xn_i2,0))
train_raw = pd.read_csv('.. /input/nfl-big-data-bowl-2020/train.csv', dtype={'WindSpeed': 'object'} )<define_variables>
def feature_extraction(raw_frame):
frame = pd.DataFrame()
raw_frame['angular_velocity'] = raw_frame['angular_velocity_X'] + raw_frame['angular_velocity_Y'] + raw_frame['angular_velocity_Z']
raw_frame['linear_acceleration'] = raw_frame['linear_acceleration_X'] + raw_frame['linear_acceleration_Y'] + raw_frame['linear_acceleration_Z']
raw_frame['velocity_to_acceleration'] = raw_frame['angular_velocity'] / raw_frame['linear_acceleration']
for col in raw_frame.columns[3:]:
frame[col + '_mean'] = raw_frame.groupby(['series_id'])[col].mean()
frame[col + '_CPT5'] = raw_frame.groupby(['series_id'])[col].apply(CPT5)
 frame[col + '_SSC'] = raw_frame.groupby(['series_id'])[col].apply(SSC)
 frame[col + '_skewness'] = raw_frame.groupby(['series_id'])[col].apply(skewness)
 frame[col + '_wave_lenght'] = raw_frame.groupby(['series_id'])[col].apply(wave_length)
 frame[col + '_norm_entropy'] = raw_frame.groupby(['series_id'])[col].apply(norm_entropy)
 frame[col + '_SRAV'] = raw_frame.groupby(['series_id'])[col].apply(SRAV)
 frame[col + '_kurtosis'] = raw_frame.groupby(['series_id'])[col].apply(_kurtosis)
 frame[col + '_mean_abs'] = raw_frame.groupby(['series_id'])[col].apply(mean_abs)
 frame[col + '_zero_crossing'] = raw_frame.groupby(['series_id'])[col].apply(zero_crossing)
 return frame
piv_cols=['PlayerHeight','PlayerWeight','PlayerAge','X','Y',
'Orientation_x','Orientation_y','Dir_x','Dir_y','S','A','Dis',
'is_off','is_rusher','is_rb','is_l','is_sk']<define_variables>
train_df = feature_extraction(X_train_df)
 test_df = feature_extraction(X_test_df)
cat_cols=['Week','Quarter','Down','OffenseFormation', 'DefendersInTheBox',
'outside','rain','snow','Turf','is_home'] 
<define_variables>
train_df.fillna(0, inplace = True)
 train_df.replace(-np.inf, 0, inplace = True)
 train_df.replace(np.inf, 0, inplace = True)
 test_df.fillna(0, inplace = True)
 test_df.replace(-np.inf, 0, inplace = True)
 test_df.replace(np.inf, 0, inplace = True )
cont_cols=['till_reg','YardsFromOwnGoal','PossScore','DefScore',
'Distance','handoff_delay']<normalization>
scaler = StandardScaler()
X_train_scaled = pd.DataFrame(scaler.fit_transform(train_df))
X_test_scaled = pd.DataFrame(scaler.transform(test_df))
tall_pipe=make_column_transformer(
(make_pipeline(SimpleImputer(strategy='mean'), StandardScaler()),['PlayerHeight','PlayerWeight','PlayerAge','X','Y',
'Orientation_x','Orientation_y','Dir_x','Dir_y','S','A','Dis']), 
('passthrough',['is_off','is_rusher','is_rb','is_l','is_sk'])
)
 cont_pipe=make_column_transformer(
(make_pipeline(SimpleImputer(strategy='median'),StandardScaler()),cont_cols)
)
 cat_pipe=make_column_transformer(
(make_pipeline(SimpleImputer(strategy='most_frequent'),OrdinalEncoder()),cat_cols)
)
<feature_engineering>
le = LabelEncoder()
target = le.fit_transform(Y_train_df['surface'] )
def feature_magic(train_in,tall_piper=tall_pipe,cat_piper=cat_pipe,cont_piper=cont_pipe,piv_cols=piv_cols,cat_cols=cat_cols,lefty=False,training=False):
train=train_in.copy()
if lefty:
train_left=train.copy()
train_left['PlayId']=-train_left['PlayId']
train=pd.concat([train,train_left])
 train=train.reset_index(drop=True)
 train.loc[train.VisitorTeamAbbr == "ARI",'VisitorTeamAbbr'] = "ARZ"
train.loc[train.HomeTeamAbbr == "ARI",'HomeTeamAbbr'] = "ARZ"
train.loc[train.VisitorTeamAbbr == "BAL",'VisitorTeamAbbr']= "BLT"
train.loc[train.HomeTeamAbbr == "BAL",'HomeTeamAbbr'] = "BLT"
train.loc[train.VisitorTeamAbbr == "CLE",'VisitorTeamAbbr'] = "CLV"
train.loc[train.HomeTeamAbbr == "CLE",'HomeTeamAbbr'] = "CLV"
train.loc[train.VisitorTeamAbbr == "HOU",'VisitorTeamAbbr'] = "HST"
train.loc[train.HomeTeamAbbr == "HOU",'HomeTeamAbbr'] = "HST"
train['is_rusher']=train.NflId==train.NflIdRusher
train['is_home']=train.Team=='home'
train['is_off']=(train.is_home &(train['HomeTeamAbbr']==train['PossessionTeam'])) |(~train.is_home &(train['HomeTeamAbbr']!=train['PossessionTeam']))
train['YardsFromOwnGoal']=train['YardLine']
train.loc[train.FieldPosition!=train.PossessionTeam,'YardsFromOwnGoal']=50+50-train.loc[train.FieldPosition!=train.PossessionTeam,'YardsFromOwnGoal']
train['to_left']=train.PlayDirection=='left'
train.loc[train.to_left,'X']=120-train.loc[train.to_left,'X']
train['X']=train['X']-train['YardsFromOwnGoal']
train.loc[train.to_left,'Y']=(160/3)-train.loc[train.to_left,'Y']
train['Y']=train.Y-(160/3)/2
train.loc[train.PlayId<0,'Y']=-train.loc[train.PlayId<0,'Y']
train.loc[train.to_left,'Orientation']=train.loc[train.to_left,'Orientation']-180
train.loc[train.to_left,'Dir']=train.loc[train.to_left,'Dir']-180
train.loc[train['Season'] == 2017, 'Orientation'] = np.mod(90 + train.loc[train['Season'] == 2017, 'Orientation'], 360)
 deg_cols=['Orientation','Dir']
for deg_col in deg_cols:
train[deg_col+'_x']=np.sin(np.radians(train[deg_col]))
train[deg_col+'_y']=np.cos(np.radians(train[deg_col]))
train.loc[train.PlayId<0,deg_col+'_y']=-train.loc[train.PlayId<0,deg_col+'_y']
train['TimeSnap']=pd.to_datetime(train.TimeSnap)
 train['PlayerBirthDate']=pd.to_datetime(train['PlayerBirthDate'],utc=True)
 train['PlayerAge']=train['TimeSnap']-train['PlayerBirthDate']
train['PlayerAge']=train['PlayerAge'].apply(lambda x:x.total_seconds() /(365.25*24*3600))
train.PlayerHeight=train.PlayerHeight.apply(heightsplit)
 posmap={'CB':'DB','WR':'WR','G':'OL','T':'OL','DE':'DL','DT':'DL','OLB':'LB','TE':'TE','FS':'DB','C':'OL','RB':'RB','QB':'QB',
'SS':'DB','ILB':'LB','MLB':'LB','NT':'DL','LB':'LB','OT':'OL','FB':'FB','OG':'OL','DB':'DB','S':'DB','HB':'RB','SAF':'DB','DL':'DL'}
train.Position=train.Position.apply(lambda x: posmap[x])
 train['is_rb']=(train.Position=='RB')|(train.Position=='FB')
 train['is_l']=(train.Position=='OL')|(train.Position=='DL')|(train.Position=='TE')|(train.Position=='FB')
 train['is_sk']=(train.Position=='WR')|(train.Position=='DB')|(train.Position=='TE')
 train=train.sort_values(['PlayId','Y','X','is_off'])
 trains=train.copy()
train_run=train[train.is_rusher]
if training:
train[piv_cols]=tall_piper.fit_transform(train[piv_cols])
 else:
train[piv_cols]=tall_piper.transform(train[piv_cols])
 convdat=train.groupby('PlayId',as_index=False ).apply(lambda x:x[piv_cols].values)
 convdat=np.dstack(convdat,)
 convdat=np.swapaxes(convdat,0,2)
 convdat=convdat.reshape(convdat.shape[0],convdat.shape[1],convdat.shape[2],1)
 train_run.GameClock=train.GameClock.apply(timesplit)
 train_run['till_reg']=train_run.GameClock+(4-train_run.Quarter)*60*15
train_run['outside']=(train_run.StadiumType.str.lower().str.contains(r'ou[a-z]+')|
train_run.StadiumType.str.lower().str.contains(r'open')|
train_run.StadiumType.str.lower().str.contains(r'heinz field')|
train_run.StadiumType.str.lower().str.contains(r'bowl')
)
 train_run['Turf']=~(train_run.Turf.str.lower() =='grass')|(train_run.Turf.str.lower().str.contains(r'natur'))
train_run['rain']=(train_run.GameWeather.str.lower().str.contains(r'rain')|
train_run.GameWeather.str.lower().str.contains(r'shower'))
train_run['snow']=(train_run.GameWeather.str.lower().str.contains(r'snow'))
train_run['PossScore']=np.select([train_run.is_home,~train_run.is_home],[train_run.HomeScoreBeforePlay,train_run.VisitorScoreBeforePlay])
 train_run['DefScore']=np.select([train_run.is_home,~train_run.is_home],[train_run.VisitorScoreBeforePlay,train_run.HomeScoreBeforePlay])
 train_run['PossTeam']=np.select([train_run.is_home,~train_run.is_home],[train_run.HomeTeamAbbr,train_run.VisitorTeamAbbr])
 train_run['DefTeam']=np.select([train_run.is_home,~train_run.is_home],[train_run.VisitorTeamAbbr,train_run.HomeTeamAbbr])
 td=pd.to_datetime(train_run.TimeHandoff)-pd.to_datetime(train_run.TimeSnap)
 train_run['handoff_delay']=td.dt.total_seconds()
if training:
yards=train_run.Yards
train_run=train_run.drop(columns='Yards')
 train_cont=cont_piper.fit_transform(train_run)
 train_cat=cat_piper.fit_transform(train_run)
 train_cat=train_cat.reshape(train_cat.shape+(1,))
train_run['Yards']=yards
else:
train_cont=cont_piper.transform(train_run)
 train_cat=cat_piper.transform(train_run)
 train_cat=train_cat.reshape(train_cat.shape+(1,))
train_cat=train_cat.swapaxes(0,1)
 train_cat=train_cat.tolist()
train_run.reset_index(drop=True,inplace=True)
 return(trains,train_run,convdat,train_cont,train_cat,tall_piper,cont_piper,cat_piper )<feature_engineering>
sub['surface'] = le.inverse_transform(predictions.argmax(axis=1))
sub.to_csv('submission_lgbm.csv', index=False )
trainn,train_runn,train_conv,train_cont,train_cat,tall_pipe,cont_pipe,cat_pipe=feature_magic(train_raw,lefty=True,training=True )<define_variables>
X_train, X_test, Y_train, Y_test = train_test_split(X_train_scaled, target, test_size=0.2, random_state=23, stratify=target )
test_mask=(( train_runn.Season==2018)&(( train_runn.Week>=13)))&(train_runn.PlayId>0)
 train_mask=~(( train_runn.Season==2018)&(( train_runn.Week>=13)) )<data_type_conversions>
optimal_k = 120
rf_acc = []
rf_clf = RandomForestClassifier(n_estimators=optimal_k, random_state=0)
 rf_clf.fit(X_train, Y_train)
 Y_pred = rf_clf.predict(X_test)
 acc = accuracy_score(Y_pred,Y_test)
 print('Acc_Test:')
 print(float("%0.3f" %(100*acc)) )
cv_cat=train_cat[train_mask]
cv_cat=cv_cat.swapaxes(0,1)
 cv_cat=cv_cat.tolist()
test_cat=train_cat[test_mask]
test_cat=test_cat.swapaxes(0,1)
 test_cat=test_cat.tolist()
train_catt=train_cat.swapaxes(0,1)
 train_catt=train_catt.tolist()<filter>
rf_clf.fit(X_train_scaled, target )
cv_cont=train_cont[train_mask]
test_cont=train_cont[test_mask]<feature_engineering>
Y_test_sub = rf_clf.predict(X_test_scaled)
 sub['surface'] = le.inverse_transform(Y_test_sub)
 sub.to_csv('submission_rf.csv', index=False )