Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<feature_engineering>
%matplotlib inline
warnings.simplefilter('ignore')
def get_player_age(train_df):
train_df['Age']=2019 - pd.DatetimeIndex(train_df['PlayerBirthDate'] ).year
return train_df<categorify>
train = pd.read_csv(".. /input/X_train.csv")
 test = pd.read_csv(".. /input/X_test.csv")
 label = pd.read_csv(".. /input/y_train.csv")
 sub = pd.read_csv(".. /input/sample_submission.csv" )
def possession_team(possessionteam, hometeam, visitorteam):
if possessionteam == hometeam:
return 'home'
else:
return 'visitor'
def leading_team_home_visitor(homescore, visitorscore):
if homescore == visitorscore:
return 'draw'
else:
if homescore > visitorscore:
return 'home'
else:
return 'visitor'
def leading_team_def_off(team, posessionteam, hometeam, visitorteam, homescore, visitorscore):
if homescore == visitorscore:
return 'draw'
else:
if posessionteam == hometeam:
if homescore > visitorscore:
return 'offense'
else:
return 'defense'
else:
if visitorscore > homescore:
return 'offense'
else:
return 'defense'
def get_leading_team_features(train_df):
train_df['possession_team_home_visitor'] = train_df.apply(lambda row: possession_team(row.PossessionTeam,row.HomeTeamAbbr,row.VisitorTeamAbbr), axis = 1)
 train_df['leading_by'] = abs(train_df['HomeScoreBeforePlay'] - train_df['VisitorScoreBeforePlay'])
 train_df['leading_team_defense_offense'] = train_df.apply(lambda row: leading_team_def_off(row.Team,
row.PossessionTeam,
row.HomeTeamAbbr,
row.VisitorTeamAbbr,
row.HomeScoreBeforePlay,
row.VisitorScoreBeforePlay), axis = 1)
 train_df['leading_team_home_visitor'] = train_df.apply(lambda row: leading_team_home_visitor(row.HomeScoreBeforePlay,row.VisitorScoreBeforePlay), axis = 1)
 train_df['Offense_score']=np.where(train_df['leading_team_defense_offense']=='offense', train_df['leading_by'], -train_df['leading_by'])
 train_df['Deffense_score']=np.where(train_df['leading_team_defense_offense']=='defense', train_df['leading_by'], -train_df['leading_by'])
 train_df['Offense_Team'] = train_df['PossessionTeam']
train_df['Defense_Team'] = train_df.apply(lambda row: row['HomeTeamAbbr'] if row['PossessionTeam'] != row['HomeTeamAbbr'] else row['VisitorTeamAbbr'],axis=1)
 return train_df<feature_engineering>
train = reduce_mem_usage(train)
 test = reduce_mem_usage(test )
def get_adjusted_yardline(train_df):
train_df['Home_team_play']=np.where(train_df['PossessionTeam']==train_df['HomeTeamAbbr'], 1, 0)
 train_df['Team_side']=np.where(train_df['PossessionTeam']==train_df['FieldPosition'], 1, 0)
 train_df['YardLine_adj']=np.where(train_df['Team_side']==1,train_df['YardLine'],100-train_df['YardLine'])
 return train_df<data_type_conversions>
def create_valid_set(label):
ldict = {
'concrete': 0.16,
'soft_pvc': 0.18,
'wood': 0.06,
'tiled': 0.03,
'fine_concrete': 0.10,
'hard_tiles_large_space': 0.12,
'soft_tiles': 0.23,
'carpet': 0.05,
'hard_tiles': 0.07,
}
score = 0
print("Required count of target classes for the Valid Set :: ")
 for key, value in ldict.items() :
score += value
print(key, int(value * 380))
ldict[key] = int(value * 380)
 print("
Total Weights of class :: ", score)
 ser = label.groupby(['surface'])['group_id'].value_counts()
ser = pd.DataFrame(ser)
 ser.columns = ['count']
cv_set = {
'concrete': [0],
'soft_pvc': [69],
'wood': [2],
'tiled': [28],
'fine_concrete': [36],
'hard_tiles_large_space': [16],
'soft_tiles': [4, 17],
'carpet': [52],
'hard_tiles': [27],
}
cv_size = 0
for key, value in cv_set.items() :
print(key)
 for i in value:
cv_size += label[label['group_id'] == i].shape[0]
print("
Got shape :: ", label[label['group_id'] == i].shape[0])
 print("Expected shape :: ", ldict[key])
 val_df = pd.DataFrame()
for key, value in cv_set.items() :
for i in value:
val_df = pd.concat([val_df, label[label['group_id'] == i]])
 print("Valid Set Size :: ", val_df.shape[0])
 hard_tiles_index = label[(label['surface'] == 'hard_tiles')&(label['group_id'] == 27)].index
trn_series_id_list = list(set(label.series_id.unique())- set(val_df.series_id.unique())) + hard_tiles_index.tolist()
print("Train Set Distribution")
 print(label['surface'].iloc[trn_series_id_list].value_counts())
 print("Valid Set Distribution")
 print(label['surface'].iloc[val_df.index].value_counts())
 trn_df = label.iloc[trn_series_id_list]
trn_df.set_index(['series_id'], inplace=True)
 val_df.set_index(['series_id'], inplace=True)
 return trn_df, val_df
def get_GameClock_features(train_df):
train_df['snap_to_handoff']=(pd.to_datetime(train_df['TimeHandoff'])-pd.to_datetime(train_df['TimeSnap'])).dt.total_seconds()
sec=pd.to_timedelta(train_df['GameClock'])
 sec=sec.dt.total_seconds() /60
train_df['GameClock_sec']=sec
train_df['clock_bin']=np.where(train_df['Quarter']==1,1,np.where(train_df['Quarter']==2,2,np.where(train_df['Quarter']==3,3,
np.where(train_df['Quarter']==4,np.where(train_df['GameClock_sec']<=564,4,5),np.where(train_df['GameClock_sec']<=333,6,7)))))
 return train_df<string_transform>
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z
def get_personnel_types(dataset):
setups = list(dataset['OffensePersonnel'].unique())
 setups = ','.join(setups)
 setups = re.sub(r'\d+', '', setups ).replace(' ','')
 mylist = list(set(setups.split(',')))
 offense_column_names = ['Offense_' + s for s in mylist]
setups = list(dataset['DefensePersonnel'].unique())
 setups = ','.join(setups)
 setups = re.sub(r'\d+', '', setups ).replace(' ','')
 mylist = list(set(setups.split(',')))
 defense_column_names = ['Defense_' + s for s in mylist]
return offense_column_names + defense_column_names
def extract_personnel_count(whole_personnel, specific_personnel):
specific_personnel = specific_personnel.split('_')[1]
if specific_personnel in whole_personnel:
whole_personnel = whole_personnel.split(',')
 types = [re.sub(r'\d+', '', x ).replace(' ','')for x in whole_personnel]
counts = [[int(s)for s in x.split() if s.isdigit() ][0] for x in whole_personnel]
dictionary = dict(zip(types, counts))
return dictionary[specific_personnel]
else:
return 0
def get_split_personnel_types(columns_to_add, train_df):
for col in columns_to_add:
if col.split('_')[0] == 'Defense':
train_df[col] = train_df['DefensePersonnel'].apply(lambda x: extract_personnel_count(x, col))
else:
train_df[col] = train_df['OffensePersonnel'].apply(lambda x: extract_personnel_count(x, col))
return train_df 
<groupby>
def FE(data):
df = pd.DataFrame()
data['norm_quat'] =(data['orientation_X']**2 + data['orientation_Y']**2 + data['orientation_Z']**2 + data['orientation_W']**2)
 data['mod_quat'] =(data['norm_quat'])**0.5
data['norm_X'] = data['orientation_X'] / data['mod_quat']
data['norm_Y'] = data['orientation_Y'] / data['mod_quat']
data['norm_Z'] = data['orientation_Z'] / data['mod_quat']
data['norm_W'] = data['orientation_W'] / data['mod_quat']
data['total_angular_velocity'] =(data['angular_velocity_X'] ** 2 + data['angular_velocity_Y'] ** 2 +
data['angular_velocity_Z'] ** 2)** 0.5
data['total_linear_acceleration'] =(data['linear_acceleration_X'] ** 2 + data['linear_acceleration_Y'] ** 2 +
data['linear_acceleration_Z'] ** 2)** 0.5
data['total_orientation'] =(data['orientation_X'] ** 2 + data['orientation_Y'] ** 2 +
data['orientation_Z'] ** 2)** 0.5
data['acc_vs_vel'] = data['total_linear_acceleration'] / data['total_angular_velocity']
x, y, z, w = data['orientation_X'].tolist() , data['orientation_Y'].tolist() , data['orientation_Z'].tolist() , data['orientation_W'].tolist()
nx, ny, nz = [], [], []
for i in range(len(x)) :
xx, yy, zz = quaternion_to_euler(x[i], y[i], z[i], w[i])
 nx.append(xx)
 ny.append(yy)
 nz.append(zz)
 data['euler_x'] = nx
data['euler_y'] = ny
data['euler_z'] = nz
data['total_angle'] =(data['euler_x'] ** 2 + data['euler_y'] ** 2 + data['euler_z'] ** 2)** 0.5
data['angle_vs_acc'] = data['total_angle'] / data['total_linear_acceleration']
data['angle_vs_vel'] = data['total_angle'] / data['total_angular_velocity']
def f1(x):
return np.mean(np.diff(np.abs(np.diff(x))))
def f2(x):
return np.mean(np.abs(np.diff(x)))
 for col in data.columns:
if col in ['row_id', 'series_id', 'measurement_number']:
continue
df[col + '_mean'] = data.groupby(['series_id'])[col].mean()
df[col + '_max'] = data.groupby(['series_id'])[col].max()
df[col + '_min'] = data.groupby(['series_id'])[col].min()
df[col + '_std'] = data.groupby(['series_id'])[col].std()
df[col + '_maxtoMin'] = df[col + '_max'] / df[col + '_min']
df[col + '_abs_min'] = data.groupby(['series_id'])[col].apply(lambda x: np.min(np.abs(x)))
 df[col + '_abs_max'] = data.groupby(['series_id'])[col].apply(lambda x: np.max(np.abs(x)))
 df[col + '_abs_avg'] =(df[col + '_abs_min'] + df[col + '_abs_max'])/2
df[col + '_mean_abs_change'] = data.groupby('series_id')[col].apply(f2)
 df[col + '_mean_change_of_abs_change'] = data.groupby('series_id')[col].apply(f1)
 return df
def add_bins_and_generate_stats(train_df):
bins=[-99,-0.01,0.01,1.72,4.21,6.7,99]
train_df['Yards_Bin'] = pd.cut(train_df['Yards'], bins)
 stats = {}
for my_type in ['DisplayName', 'Offense_Team', 'Defense_Team']:
per_my_type = train_df[[my_type,'Yards','Yards_Bin']].groupby([my_type,'Yards_Bin'] ).count().fillna(0 ).reset_index()
per_my_type = per_my_type.merge(per_my_type.groupby(my_type)['Yards'].sum().reset_index() , on=my_type, how='left')
 per_my_type['Yards_Bin_Rate'] = per_my_type['Yards_x'] / per_my_type['Yards_y']
per_my_type.rename(columns={'Yards_x':'Bin_Count', 'Yards_y': 'Total_Plays'}, inplace=True)
 per_my_type = pd.pivot_table(per_my_type, values=['Bin_Count', 'Yards_Bin_Rate'], index=[my_type], columns=['Yards_Bin'])
 per_my_type.columns = per_my_type.columns.map('{0[0]}_{0[1]}'.format)
 per_my_type = per_my_type.reset_index()
avg_yards = train_df.groupby(my_type)['Yards'].mean().reset_index()
std_yards = train_df.groupby(my_type)['Yards'].std().reset_index().fillna(0)
 counts = train_df.groupby(my_type)['Yards'].count().reset_index()
per_type = avg_yards.merge(counts, on=my_type, how='inner' ).merge(std_yards, on=my_type, how='inner')
 per_type.rename(columns={'Yards_x':'Avg_Yards', 'Yards_y':'Count', 'Yards':'Std_Yards'}, inplace= True)
 per_my_type= per_my_type.merge(per_type, on=my_type, how='left')
 if my_type == 'DisplayName':
per_player_enough_plays = per_type[per_type['Count'] >= 10]
per_player_not_enough_plays = per_type[per_type['Count'] < 10]
per_player_not_enough_plays['rusher_performance_bin'] = 'few_plays'
per_player_enough_plays['rusher_performance_bin'] = pd.qcut(per_player_enough_plays['Avg_Yards'], q=[0, 0.2,0.5,0.9,1], labels= ['bottom_20','20-50','50-90','top_10'])
 per_player = pd.concat([per_player_enough_plays, per_player_not_enough_plays])
 per_my_type= per_my_type.merge(per_player[[my_type,'rusher_performance_bin']], on=my_type, how='left')
 per_my_type.columns = [per_my_type.columns[0]] + [my_type + '_' + str(col)for col in per_my_type.columns[1:]]
stats[my_type] = per_my_type
train_df = train_df.merge(per_my_type, on=my_type, how='left')
 return stats, train_df<data_type_conversions>
%%time
trn_df, val_df = create_valid_set(label)
 train = FE(train)
 test = FE(test )
def convert_to_cat(df):
for col in ['DefensePersonnel','Home_team_play','Team_side','Down','Defense_Team','OffenseFormation','OffensePersonnel',
'leading_team_defense_offense','PlayDirection','clock_bin','Defense_DB','Defense_LB','Defense_DL','Offense_WR']:
df[col] = df[col].astype('category')
 return df<compute_test_metric>
le = LabelEncoder()
label['surface'] = le.fit_transform(label['surface'] )
def euclidean_distance(x1,x2,y1,y2):
return np.sqrt(( x1-x2)**2 +(y1-y2)**2)
 def norm_x(df):
df['Player_o_d']=np.where(( df["Home_team_play"] ==1)&(df["Team"] == 'home')|(df["Home_team_play"] ==0)&(df["Team"] == 'away'),'Offense_Player','Defense_Player')
 df['YardLine_Direction']=np.where(df["PlayDirection"] =='right',df['YardLine_adj'],100-df['YardLine_adj'])
 df['X_adj']=np.where(df["Player_o_d"] =='Offense_Player',abs(df['YardLine_Direction']+10-df['X']),-abs(df['YardLine_Direction']+10-df['X']))
return(df)
 def qb_and_rusher_to_closest_defender(df):
play_ids = list(df.PlayId.unique())
 rows = []
for play_id in play_ids:
play = df[df['PlayId'] == play_id]
rusher = play[play['IsBallCarrier']==True][['X', 'Y']]
QB_offense=play[play['Position']=='QB'][['X','Y']]
defenders = play[play['Player_o_d']=='Defense_Player'][['X', 'Y']]
defenders['rusher_X'] = rusher['X'].iloc[0]
defenders['rusher_Y'] = rusher['Y'].iloc[0]
try:
defenders['qb_X'] = QB_offense['X'].iloc[0]
defenders['qb_Y'] = QB_offense['Y'].iloc[0]
except:
defenders['qb_X'] = 50
defenders['qb_Y'] = 50
defenders['dist_rusher_to_defender'] = defenders.apply(lambda row: euclidean_distance(row.X,row.rusher_X,row.Y,row.rusher_Y), axis = 1)
 defenders['dist_qb_to_defender'] = defenders.apply(lambda row: euclidean_distance(row.X,row.qb_X,row.Y,row.qb_Y), axis = 1)
 rows.append([play_id, defenders['dist_rusher_to_defender'].min() , defenders['dist_qb_to_defender'].min() , euclidean_distance(rusher['X'].iloc[0], defenders['qb_X'].iloc[0], rusher['Y'].iloc[0], defenders['qb_Y'].iloc[0])])
 rows = pd.DataFrame(rows, columns=['PlayId', 'min_dist_rusher_to_defender', 'min_dist_qb_to_defender', 'dist_rusher_QB'])
 df = df.merge(rows, on='PlayId', how='left')
 return df
def Direction_orientation_adj(df):
df['dir_adj']=np.where(df["Player_o_d"] =='Offense_Player',np.where(df["PlayDirection"] =='right',df["Dir"],360-df["Dir"]),np.where(df["PlayDirection"] =='right',360-df["Dir"],df["Dir"]))
df['Orientation_adj']=np.where(df["Player_o_d"] =='Offense_Player',np.where(df["PlayDirection"] =='right',df["Orientation"],360-df["Orientation"]),np.where(df["PlayDirection"] =='right',360-df["Orientation"],df["Orientation"]))
df['dir_to_Orientation']=180 - abs(abs(df['dir_adj'] - df['Orientation_adj'])- 180)
 return(df)
 def X_Y_Velocity(df):
df['v_x']=np.sin(df['dir_adj'] * np.pi/180)*df['S']
df['v_y']=np.cos(df['dir_adj'] * np.pi/180)*df['S']
df['arc_tan']=np.arctan(( df['v_y']/df['v_x'])) *180/np.pi
df['arc_tan_cat']=pd.cut(df['arc_tan'],bins=[-90, -45, 0, 45, 90],labels=False)
 df['t1_distance']=df['Dis']+df['S']*1+0.5*df['A']*1
df['t2_distance']=df['Dis']+df['S']*2+0.5*df['A']*4
df['t3_distance']=df['Dis']+df['S']*3+0.5*df['A']*9
df['v_x_a1']=np.sin(df['dir_adj'] * np.pi/180)*(df['S']+df['A']*1)
 df['v_y_a1']=np.cos(df['dir_adj'] * np.pi/180)*(df['S']+df['A']*1)
 df['v_x_a2']=np.sin(df['dir_adj'] * np.pi/180)*(df['S']+df['A']*2)
 df['v_y_a2']=np.cos(df['dir_adj'] * np.pi/180)*(df['S']+df['A']*2)
 df['v_x_a3']=np.sin(df['dir_adj'] * np.pi/180)*(df['S']+df['A']*3)
 df['v_y_a3']=np.cos(df['dir_adj'] * np.pi/180)*(df['S']+df['A']*3)
 df['a_x']=np.sin(df['dir_adj'] * np.pi/180)*df['A']
df['a_y']=np.cos(df['dir_adj'] * np.pi/180)*df['A']
return(df )<set_options>
train.fillna(0,inplace=True)
 train.replace(-np.inf,0,inplace=True)
 train.replace(np.inf,0,inplace=True)
 test.fillna(0,inplace=True)
 test.replace(-np.inf,0,inplace=True)
 test.replace(np.inf,0,inplace=True )
h2o.init(min_mem_size='16G' )<train_model>
x_train = train.iloc[trn_df.index]
y_train = label['surface'].iloc[trn_df.index]
x_val = train.iloc[val_df.index]
y_val = label['surface'].iloc[val_df.index]
print(x_train.shape, y_train.shape, x_val.shape, y_val.shape )
def build_and_return_model(train_df, training_columns, name, ntrees, max_depth, min_rows):
train = h2o.H2OFrame(train_df)
 model = H2ORandomForestEstimator(ntrees=ntrees, max_depth=max_depth, min_rows=min_rows, keep_cross_validation_predictions=True, nfolds=10, seed=1)
 model.train(x=training_columns, y='Yards', training_frame=train)
 cv_predictions = model.cross_validation_holdout_predictions()
train_df = train_df.join(cv_predictions.as_data_frame())
 train_df.rename(columns={'predict':('predict_' + name)}, inplace=True)
 return model, train_df<load_from_csv>
def lb_dist(model):
model.fit(x_train, y_train)
 print("Train Acc :: ", accuracy_score(y_train, model.predict(x_train)))
 print("Valid Acc :: ", accuracy_score(y_val, model.predict(x_val)))
 print("CV Accuracy :: ", cross_val_score(rand, train, label['surface'], cv=5 ).mean())
 return model
train_df_all = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', low_memory=False)
 train_df_all = clean_TeamAbbreviations(train_df_all)
 train_df_all = imperial_to_metric(train_df_all)
 train_df_all = tag_rusher(train_df_all)
 train_df_all = get_player_age(train_df_all)
 train_df_all = get_adjusted_yardline(train_df_all)
 train_df_all=norm_x(train_df_all)
 train_df_all['dis_from_center']= abs(train_df_all['X_adj'])
 train_df_all=Direction_orientation_adj(train_df_all)
 train_df_all=X_Y_Velocity(train_df_all)
 train_df_all = qb_and_rusher_to_closest_defender(train_df_all)
 train_df=train_df_all[train_df_all['NflId']==train_df_all['NflIdRusher']]
train_df.FieldPosition = train_df.FieldPosition.fillna('NA')
 train_df = get_direction(train_df)
 train_df = get_leading_team_features(train_df)
 train_df = get_GameClock_features(train_df)
 train_df['StadiumType'] = train_df['StadiumType'].apply(clean_StadiumType)
 train_df['WindDirection']=train_df['WindDirection'].apply(clean_WindDirection)
 train_df['WindDirection']=train_df['WindDirection'].apply(transform_WindDirection)
 train_df=set_Turf(train_df)
 train_df=GameWeather(train_df)
 personnel_columns_to_add = get_personnel_types(train_df)
 train_df = get_split_personnel_types(personnel_columns_to_add, train_df)
 player_and_team_stats, train_df = add_bins_and_generate_stats(train_df)
 models_columns=get_models_columns()
model_player,train_df = build_and_return_model(train_df, models_columns['player'], 'player', 500, 5, 27)
 model_offense,train_df = build_and_return_model(train_df, models_columns['offense'], 'offense', 500, 5, 15)
 model_defense,train_df = build_and_return_model(train_df, models_columns['defense'], 'defense', 500, 6, 25)
 models = {'player': model_player, 'offense': model_offense,'defense': model_defense}<define_variables>
rand = RandomForestClassifier(n_estimators=500, random_state=13)
 rand = lb_dist(rand )
training_columns =[
'Down',
'Age',
'Offense_DL',
'Offense_QB',
'Offense_RB',
'Offense_LB',
'Offense_DB',
'Offense_TE',
'Defense_DL',
'Defense_OL',
'min_dist_rusher_to_defender',
'min_dist_qb_to_defender',
'dist_rusher_QB',
'A',
'predict_defense',
'predict_offense',
'predict_player',
'YardLine_adj',
'S',
'DefendersInTheBox',
'Dis',
'Distance',
'X_adj',
'DisplayName_Count',
'Defense_DB',
'DisplayName_Bin_Count_(1.72, 4.21]',
'DisplayName_Bin_Count_(6.7, 99.0]',
'DisplayName_Yards_Bin_Rate_(-99.0, -0.01]',
'DisplayName_Bin_Count_(0.01, 1.72]',
'VisitorScoreBeforePlay',
'DisplayName_Bin_Count_(4.21, 6.7]',
'clock_bin',
'Defense_Team_Yards_Bin_Rate_(6.7, 99.0]',
'Offense_WR',
'Team_side',
'Offense_score',
'Deffense_score',
'Defense_LB',
'DisplayName_Yards_Bin_Rate_(0.01, 1.72]',
'Defense_Team_Yards_Bin_Rate_(-0.01, 0.01]',
'Defense_Team_Bin_Count_(1.72, 4.21]',
'Defense_Team_Avg_Yards',
'Offense_OL',
'DisplayName_Yards_Bin_Rate_(-0.01, 0.01]',
'Defense_Team_Yards_Bin_Rate_(-99.0, -0.01]',
'Defense_Team_Bin_Count_(-99.0, -0.01]',
'dis_from_center',
'dir_adj',
'Orientation_adj',
'dir_to_Orientation',
'v_x',
'v_y',
'arc_tan',
'arc_tan_cat',
't1_distance',
't2_distance',
't3_distance',
'v_x_a1',
'v_y_a1',
'v_x_a2',
'v_y_a2',
'v_x_a3',
'v_y_a3'
] 
<categorify>
print("Accuracy Score :: ", accuracy_score(label['surface'], rand.predict(train)))
 plot_confusion_matrix(label['surface'], rand.predict(train), classes=le.classes_ )
categorical_columns = ['DisplayName_rusher_performance_bin', 'Position', 'Stadium','GameWeather','OffensePersonnel','Turf_type','OffenseFormation']
one_hot = pd.get_dummies(train_df[categorical_columns])
 train_df = train_df.join(one_hot )<define_variables>
folds = StratifiedKFold(n_splits=10, shuffle=True, random_state=20)
 predicted = np.zeros(( test.shape[0],9))
measured= np.zeros(( train.shape[0]))
score = 0
training_columns = training_columns + list(one_hot.columns )<prepare_x_and_y>
for times,(trn_idx, val_idx)in enumerate(folds.split(train.values, label['surface'].values)) :
model = RandomForestClassifier(n_estimators=500, random_state=13)
 model.fit(train.iloc[trn_idx], label['surface'][trn_idx])
 measured[val_idx] = model.predict(train.iloc[val_idx])
 predicted += model.predict_proba(test)/folds.n_splits
score += model.score(train.iloc[val_idx], label['surface'][val_idx])
 print("Fold: {} score: {}".format(times, model.score(train.iloc[val_idx], label['surface'][val_idx])))
 gc.collect()
train_df_temp=train_df[training_columns].fillna(0)
 X = train_df_temp
yards = train_df.Yards
y = np.zeros(( yards.shape[0], 37))
for idx, target in enumerate(list(yards)) :
if target > 22:
y[idx][36] = 1
elif target < -14:
y[idx][0] = 1
else:
y[idx][14 + target] = 1
X = np.array(X )<prepare_x_and_y>
sub['surface'] = le.inverse_transform(predicted.argmax(axis=1))
sub.to_csv('rand_sub_10.csv', index=False)
 sub.head()
<compute_test_metric><EOS> 
sub.surface.value_counts()