Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<define_variables>
pd.options.display.max_rows = 4000
warnings.filterwarnings('ignore')
 sns.set()
%matplotlib inline
pd.set_option('display.max_columns', 50)
 pd.set_option('display.max_rows', 150 )
if 0:
training_dataset = get_training_dataset(train_ds, simple_aug=True, advance_aug=False, cut_mix_aug=1)
 training_dataset = training_dataset.unbatch().batch(20)
 train_batch = iter(training_dataset )<define_variables>
def clean_WindDirection(txt):
if isinstance(txt, int):
return('NA')
 elif pd.isna(txt):
return('NA')
 else:
if pd.isna(txt):
return np.nan
txt = txt.lower()
txt = ''.join([c for c in txt if c not in punctuation])
 txt = txt.replace('from', '')
 txt = txt.replace(' ', '')
 txt = txt.replace('north', 'n')
 txt = txt.replace('south', 's')
 txt = txt.replace('west', 'w')
 txt = txt.replace('east', 'e')
 return txt
def transform_WindDirection(txt):
if pd.isna(txt):
return('-1')
 if txt=='n':
return 0
elif txt=='nne' or txt=='nen':
return 1/8
elif txt=='ne':
return 2/8
elif txt=='ene' or txt=='nee':
return 3/8
elif txt=='e':
return 4/8
elif txt=='ese' or txt=='see':
return 5/8
elif txt=='se':
return 6/8
elif txt=='ses' or txt=='sse':
return 7/8
elif txt=='s':
return 8/8
elif txt=='ssw' or txt=='sws':
return 9/8
elif txt=='sw':
return 10/8
elif txt=='sww' or txt=='wsw':
return 11/8
elif txt=='w':
return 12/8
elif txt=='wnw' or txt=='nww':
return 13/8
elif txt=='nw':
return 14/8
elif txt=='nwn' or txt=='nnw':
return 15/8
else: return -1
return np.nan
def clean_StadiumType(txt):
if pd.isna(txt):
return('NA')
 if isinstance(txt, int):
return('NA')
 else:
txt = txt.lower()
txt = ''.join([c for c in txt if c not in punctuation])
 txt = re.sub(' +', ' ', txt)
 txt = txt.strip()
txt = txt.replace('outside', 'outdoor')
 txt = txt.replace('outdor', 'outdoor')
 txt = txt.replace('outddors', 'outdoor')
 txt = txt.replace('outdoors', 'outdoor')
 txt = txt.replace('oudoor', 'outdoor')
 txt = txt.replace('indoors', 'indoor')
 txt = txt.replace('ourdoor', 'outdoor')
 txt = txt.replace('retractable', 'rtr.')
 return txt
def set_Turf(df):
Turf = {'Field Turf':'Artificial', 'A-Turf Titan':'Artificial', 'Grass':'Natural',
'UBU Sports Speed S5-M':'Artificial', 'Artificial':'Artificial',
'DD GrassMaster':'Artificial', 'Natural Grass':'Natural', 'UBU Speed Series-S5-M':'Artificial',
'FieldTurf':'Artificial', 'FieldTurf 360':'Artificial', 'Natural grass':'Natural', 'grass':'Natural',
'Natural':'Natural', 'Artifical':'Artificial', 'FieldTurf360':'Artificial', 'Naturall Grass':'Natural',
'Field turf':'Artificial', 'SISGrass':'Artificial', 'Twenty-Four/Seven Turf':'Artificial',
'natural grass':'Natural'}
df['Turf_type']=df['Turf'].map(Turf)
 return df
def GameWeather(df):
if isinstance(df['GameWeather'].iloc[0], np.integer):
df['GameWeather']='NA'
return(df)
 else:
df['GameWeather'] = df['GameWeather'].str.lower()
indoor = "indoor"
df['GameWeather'] = df['GameWeather'].apply(lambda x: indoor if not pd.isna(x)and indoor in x else x)
 df['GameWeather'] = df['GameWeather'].apply(lambda x: x.replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly')if not pd.isna(x)else x)
 df['GameWeather'] = df['GameWeather'].apply(lambda x: x.replace('clear and sunny', 'sunny and clear')if not pd.isna(x)else x)
 df['GameWeather'] = df['GameWeather'].apply(lambda x: x.replace('skies', '' ).replace("mostly", "" ).strip() if not pd.isna(x)else x)
 return df
if 0:
display_batch_of_images(next(train_batch))<define_variables>
def clean_TeamAbbreviations(train_df):
train_df.loc[train_df.VisitorTeamAbbr == "ARI", 'VisitorTeamAbbr'] = "ARZ"
train_df.loc[train_df.HomeTeamAbbr == "ARI", 'HomeTeamAbbr'] = "ARZ"
train_df.loc[train_df.VisitorTeamAbbr == "BAL", 'VisitorTeamAbbr'] = "BLT"
train_df.loc[train_df.HomeTeamAbbr == "BAL", 'HomeTeamAbbr'] = "BLT"
train_df.loc[train_df.VisitorTeamAbbr == "CLE", 'VisitorTeamAbbr'] = "CLV"
train_df.loc[train_df.HomeTeamAbbr == "CLE", 'HomeTeamAbbr'] = "CLV"
train_df.loc[train_df.VisitorTeamAbbr == "HOU", 'VisitorTeamAbbr'] = "HST"
train_df.loc[train_df.HomeTeamAbbr == "HOU", 'HomeTeamAbbr'] = "HST"
return train_df
if 0:
test_dataset = get_test_dataset(test_ds)
 test_dataset = test_dataset.unbatch().batch(20)
 test_batch = iter(test_dataset )<define_variables>
def tag_rusher(train_df):
train_df['IsBallCarrier'] = train_df.NflId == train_df.NflIdRusher
return train_df
if 0:
display_batch_of_images(next(test_batch))<define_variables>
def get_direction(train_df):
train_df['ToLeft'] = train_df.PlayDirection == "left"
return train_df
if 0:
images, labels = next(iter(training_dataset.take(1)))
 new_images, new_labels = batch_cutmix(images, labels, PROBABILITY=1.0, batch_size=20)
 display_batch_of_images(( new_images, new_labels))<define_variables>
def feet_inch_to_cm(height_fi):
foot = 30.48
inch = 2.54
return int(height_fi.split('-')[0])* foot + int(height_fi.split('-')[1])* inch
def imperial_to_metric(train_df):
train_df['PlayerHeight_cm']= train_df['PlayerHeight'].apply(feet_inch_to_cm)
 train_df['PlayerWeight_kg']= train_df['PlayerWeight']*0.453592
return train_df
if 0:
images, labels = next(iter(training_dataset.take(1)))
 new_images, new_labels = batch_mixup(images, labels, PROBABILITY=1.0, batch_size=20)
 display_batch_of_images(( new_images, new_labels))<choose_model_class>
def get_player_age(train_df):
train_df['Age']=2019 - pd.DatetimeIndex(train_df['PlayerBirthDate'] ).year
return train_df
LR_START = 0.00003
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.000001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 3
LR_EXP_DECAY =.8
@tf.function
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))<import_modules>
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
from tensorflow.keras.applications import DenseNet201<choose_model_class>
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
with strategy.scope() :
pretrained_model = DenseNet201(weights='imagenet', include_top=False ,input_shape=[*IMAGE_SIZE, 3])
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 class LRSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __call__(self, step):
return lrfn(epoch=step//STEPS_PER_EPOCH)
 optimizer = tf.keras.optimizers.Adam(learning_rate=LRSchedule())
 model.compile(
 optimizer = optimizer,
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 model.summary()
train_accuracy = tf.keras.metrics.CategoricalAccuracy()
valid_accuracy = tf.keras.metrics.CategoricalAccuracy()
train_loss = tf.keras.metrics.Sum()
valid_loss = tf.keras.metrics.Sum()
loss_fn = lambda a,b: tf.nn.compute_average_loss(tf.keras.losses.categorical_crossentropy(a,b), global_batch_size=BATCH_SIZE )<train_model>
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
if 0:
history = model.fit(
 get_training_dataset(train_ds, simple_aug=True, advance_aug=False, cut_mix_aug=3),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=get_validation_dataset(valid_ds)if not SKIP_VALIDATION else None,
verbose=2
)<normalization>
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
if 1:
if tpu:
STEPS_PER_TPU_CALL = 99
VALIDATION_STEPS_PER_TPU_CALL = 29
@tf.function
def train_step(data_iter):
def train_step_fn(images, labels):
with tf.GradientTape() as tape:
probabilities = model(images, training=True)
 loss = loss_fn(labels, probabilities)
 grads = tape.gradient(loss, model.trainable_variables)
 optimizer.apply_gradients(zip(grads, model.trainable_variables))
train_accuracy.update_state(labels, probabilities)
 train_loss.update_state(loss)
 for _ in tf.range(STEPS_PER_TPU_CALL):
strategy.experimental_run_v2(train_step_fn, next(data_iter))
@tf.function
def valid_step(data_iter):
def valid_step_fn(images, labels):
probabilities = model(images, training=False)
 loss = loss_fn(labels, probabilities)
 valid_accuracy.update_state(labels, probabilities)
 valid_loss.update_state(loss)
 for _ in tf.range(VALIDATION_STEPS_PER_TPU_CALL):
strategy.experimental_run_v2(valid_step_fn, next(data_iter)) 
<split>
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
if 1:
start_time = epoch_start_time = time.time()
if tpu:
train_dist_ds = strategy.experimental_distribute_dataset(get_training_dataset(train_ds, simple_aug=True, advance_aug=False, cut_mix_aug=3))
valid_dist_ds = strategy.experimental_distribute_dataset(get_validation_dataset(valid_ds, repeated=True)) if not SKIP_VALIDATION else None
print("Training steps per epoch:", STEPS_PER_EPOCH, "in increment of:", STEPS_PER_TPU_CALL)
 if not SKIP_VALIDATION:
print("Validation images:", NUM_VALIDATION_IMAGES,
"Batch size:", BATCH_SIZE,
"Validation steps:", NUM_VALIDATION_IMAGES//BATCH_SIZE, "in increments of", VALIDATION_STEPS_PER_TPU_CALL)
 print("Repeated validation images:", int_div_round_up(NUM_VALIDATION_IMAGES, BATCH_SIZE*VALIDATION_STEPS_PER_TPU_CALL)*VALIDATION_STEPS_PER_TPU_CALL*BATCH_SIZE-NUM_VALIDATION_IMAGES)
 History = namedtuple('History', 'history')
 history = History(history={'loss': [], 'val_loss': [], 'categorical_accuracy': [], 'val_categorical_accuracy': []})if not SKIP_VALIDATION else History(history={'loss': [], 'val_loss': [], 'categorical_accuracy': []})
 epoch = 0
train_data_iter = iter(train_dist_ds)
 valid_data_iter = iter(valid_dist_ds)if not SKIP_VALIDATION else None
step = 0
epoch_steps = 0
while True:
train_step(train_data_iter)
 epoch_steps += STEPS_PER_TPU_CALL
step += STEPS_PER_TPU_CALL
print('=', end='', flush=True)
 if(step//STEPS_PER_EPOCH)> epoch:
print('|', end='', flush=True)
 if not SKIP_VALIDATION:
valid_epoch_steps = 0
valid_step(valid_data_iter)
 valid_epoch_steps += VALIDATION_STEPS_PER_TPU_CALL
print('=', end='', flush=True)
 history.history['val_categorical_accuracy'].append(valid_accuracy.result().numpy())
 history.history['val_loss'].append(valid_loss.result().numpy() / VALIDATION_STEPS)
 history.history['categorical_accuracy'].append(train_accuracy.result().numpy())
 history.history['loss'].append(train_loss.result().numpy() / STEPS_PER_EPOCH)
 epoch_time = time.time() - epoch_start_time
print('
EPOCH {:d}/{:d}'.format(epoch+1, EPOCHS))
if not SKIP_VALIDATION:
print('time: {:0.1f}s'.format(epoch_time),
'loss: {:0.4f}'.format(history.history['loss'][-1]),
'accuracy: {:0.4f}'.format(history.history['categorical_accuracy'][-1]),
'val_loss: {:0.4f}'.format(history.history['val_loss'][-1]),
'val_acc: {:0.4f}'.format(history.history['val_categorical_accuracy'][-1]),
'lr: {:0.4g}'.format(lrfn(epoch)) , flush=True)
 else:
print('time: {:0.1f}s'.format(epoch_time),
'loss: {:0.4f}'.format(history.history['loss'][-1]),
'accuracy: {:0.4f}'.format(history.history['categorical_accuracy'][-1]),
'lr: {:0.4g}'.format(lrfn(epoch)) , flush=True)
 epoch =(step+1)// STEPS_PER_EPOCH
epoch_start_time = time.time()
train_accuracy.reset_states()
if not SKIP_VALIDATION:
valid_accuracy.reset_states()
valid_loss.reset_states()
train_loss.reset_states()
if epoch >= EPOCHS:
break
else:
EPOCHS = 15
history = model.fit(
 get_training_dataset(train_ds),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=get_validation_dataset(valid_ds)if not SKIP_VALIDATION else None 
)
 simple_ctl_training_time = time.time() - start_time
print("OPTIMIZED CTL TRAINING TIME: {:0.1f}s".format(simple_ctl_training_time))<load_pretrained>
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
model.save_weights('densenet-flower-tpu.hdf5' )<predict_on_test>
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 )
if not SKIP_VALIDATION:
cmdataset = get_validation_dataset(load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True), ordered=True, do_onehot=False)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
cm_probabilities = model.predict(images_ds)
 cm_predictions = np.argmax(cm_probabilities, axis=-1)
 print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions )<compute_test_metric>
h2o.init(min_mem_size='16G' )
if not SKIP_VALIDATION:
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 cmat =(cmat.T / cmat.sum(axis=1)).T
display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<save_to_csv>
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
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')
 !head submission.csv<predict_on_test>
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}
if 1 and not SKIP_VALIDATION:
dataset = get_validation_dataset(valid_ds, do_onehot=False)
 dataset = dataset.unbatch().batch(20)
 batch = iter(dataset)
 images, labels = next(batch)
 probabilities = model.predict(images)
 predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )<install_modules>
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'
]
!pip install tensorflow-addons==0.9.1
!pip install -q efficientnet --upgrade<set_options>
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 )
gc.enable()
%matplotlib inline
KFold,
StratifiedKFold)
<import_modules>
training_columns = training_columns + list(one_hot.columns )
print(f"TensorFlow version: {tf.__version__}")
 print(f"Keras version: {tf.keras.__version__}" )<train_model>
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 )
print(f"TensorFlow is executing eagerly: {tf.executing_eagerly() }")
 print("No of physical devices available: {}".format(len(tf.config.experimental.list_physical_devices())))
 if not len(tf.config.experimental.list_physical_devices())== 0:
print("GPU is available." if tf.test.is_gpu_available() else "TPU is available." )<set_options>
print(f"Initializing radom seeds.. ")
 tf.random.set_seed(2020)
 print(f"Enabling TensorFlow Device Debugger.. ")
 tf.config.set_soft_device_placement(False)
 tf.debugging.set_log_device_placement(False)
 %reload_ext tensorboard
print("Done." )<train_model>
if 'XRT_TPU_CONFIG' not in os.environ and "TPU_NAME" not in os.environ:
print('ERROR: Not connected to a TPU runtime; please see the first cell in this notebook for instructions!')
 else:
tpu_address = os.environ['TPU_NAME']
print('TPU address is: ', tpu_address)
 print('TPU Configuration: ',os.environ['XRT_TPU_CONFIG'])
 with tf.compat.v1.Session(tpu_address)as session:
devices = session.list_devices()
print('TPU devices:')
 pprint.pprint(devices )<set_options>
final_model = RandomForestRegressor(bootstrap=False, max_features=0.3, min_samples_leaf=10,
min_samples_split=20, n_estimators=200, n_jobs=-1, random_state=42)
 final_model.fit(X, y )
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.cluster_spec().as_dict() ['worker'])
 except ValueError:
tpu = None
gpus = tf.config.experimental.list_logical_devices("GPU")
 if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 elif len(gpus)> 1:
strategy = tf.distribute.MirroredStrategy(gpus)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<set_options>
def make_my_predictions(test_df_all, sample_prediction_df, models, player_and_team_stats, personnel_columns_to_add, final_model, training_columns, categorical_columns, one_hot_columns):
test_df_all = clean_TeamAbbreviations(test_df_all)
 test_df_all = imperial_to_metric(test_df_all)
 test_df_all = tag_rusher(test_df_all)
 test_df_all = get_player_age(test_df_all)
 test_df_all = get_adjusted_yardline(test_df_all)
 test_df_all=norm_x(test_df_all)
 test_df_all['dis_from_center']=abs(train_df_all['X_adj'])
 test_df_all=Direction_orientation_adj(test_df_all)
 test_df_all=X_Y_Velocity(test_df_all)
 test_df_all = qb_and_rusher_to_closest_defender(test_df_all)
 test_df=test_df_all[test_df_all['NflId']==test_df_all['NflIdRusher']]
test_df = test_df.fillna(0)
 test_df = get_direction(test_df)
 test_df = get_leading_team_features(test_df)
 test_df = get_GameClock_features(test_df)
 test_df = get_split_personnel_types(personnel_columns_to_add, test_df)
 for key,value in player_and_team_stats.items() :
test_df = test_df.merge(value, on=key, how='left' ).fillna(0)
 if key == 'DisplayName' and test_df['DisplayName_rusher_performance_bin'].iloc[0] == 0:
test_df['DisplayName_rusher_performance_bin'] = 'few_plays'
test_df=set_Turf(test_df)
 test_df['StadiumType'] = test_df['StadiumType'].apply(clean_StadiumType)
 test_df['WindDirection']=test_df['WindDirection'].apply(clean_WindDirection)
 test_df['WindDirection']=test_df['WindDirection'].apply(transform_WindDirection)
 test_df=GameWeather(test_df)
 for model_type,model in models.items() :
test_df_curr = h2o.H2OFrame(test_df)
 pred=model.predict(test_df_curr)
 test_df = test_df.join(pred.as_data_frame())
 test_df.rename(columns={'predict':'predict_' + model_type}, inplace=True)
 one_hot_test = pd.get_dummies(test_df[categorical_columns])
 one_hot_test_cols = [x for x in one_hot_test if x in one_hot_columns]
one_hot_test = one_hot_test[one_hot_test_cols]
test_df = test_df.join(one_hot_test)
 more_to_add = [x for x in one_hot_columns if x not in list(one_hot_test.columns)]
for column_to_add in more_to_add:
test_df[column_to_add] = 0
test_df=test_df[training_columns]
test_X = np.array(test_df)
 pred = final_model.predict(test_X)
 pred = np.array([np.concatenate(( np.zeros(85), pred[0], np.zeros(77)) , axis=0)])
 pred = np.clip(np.cumsum(pred, axis=1), 0, 1)
 pred = pd.DataFrame(data=[list(pred[0])], columns=sample_prediction_df.columns)
 return pred
print('Configuring XLA.. ')
 if os.getenv("XLA_FLAGS")is None:
print('XLA not found.')
 tf.config.optimizer.set_jit(False)
 else:
print("XLA_FLAGS='{}'".format(os.getenv("XLA_FLAGS")))
 tf.config.optimizer.set_jit(True )<set_options>
env = nflrush.make_env()
for(test_df, sample_prediction_df)in env.iter_test() :
predictions_df = make_my_predictions(test_df, sample_prediction_df, models, player_and_team_stats, personnel_columns_to_add, final_model, training_columns, categorical_columns, list(one_hot.columns))
env.predict(predictions_df)
 env.write_submission_file()
tf.config.optimizer.set_jit(True )<define_variables>
AUTO = tf.data.experimental.AUTOTUNE
IMAGE_SIZE = [512, 512]
EPOCHS = 25
FOLDS = 2
SEED = 2020
BATCH_SIZE = 12 * strategy.num_replicas_in_sync
AUG_BATCH = BATCH_SIZE
FIRST_FOLD_ONLY = False<define_variables>
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')+ tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec' )<define_variables>
CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle',
'snapdragon', "colt's foot", 'king protea', 'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower', 'giant white arum lily',
'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth', 'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose']<categorify>
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled = True, ordered = False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads = AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls = AUTO)
 return dataset<categorify>
<categorify><EOS>