Kaggle competition: NFL Big Data Bowl
class data_generator:
def create_train(dataset_info, batch_size, shape, augument=True):
assert shape[2] == 3
while True:
dataset_info = shuffle(dataset_info)
 for start in range(0, len(dataset_info), batch_size):
end = min(start + batch_size, len(dataset_info))
batch_images = []
X_train_batch = dataset_info[start:end]
batch_labels = np.zeros(( len(X_train_batch), 28))
for i in range(len(X_train_batch)) :
image = data_generator.load_image(
 X_train_batch[i]['path'], shape)
 if augument:
if not np.all(X_train_batch[i]['labels']==(0,25)) :
image = data_generator.augment(image)
 batch_images.append(image/255.)
batch_labels[i][X_train_batch[i]['labels']] = 1
yield np.array(batch_images, np.float32), batch_labels
def load_image(path, shape):
image_red_ch = Image.open(path+'_red.png')
 image_yellow_ch = Image.open(path+'_yellow.png')
 image_green_ch = Image.open(path+'_green.png')
 image_blue_ch = Image.open(path+'_blue.png')
 image_red_yellow_ch = PIL.ImageChops.blend(image_red_ch,image_yellow_ch,0.5)
 image = np.stack((
np.array(image_green_ch),
np.array(image_red_yellow_ch),
np.array(image_blue_ch)) , -1)
 image = cv2.resize(image,(shape[0], shape[1]),interpolation = cv2.INTER_CUBIC)
 return image
def augment(image):
augment_img = iaa.Sequential([
iaa.OneOf([
iaa.Affine(rotate=0),
iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270),
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
])], random_order=True)
 image_aug = augment_img.augment_image(image)
 return image_aug<choose_model_class>
pd.set_option('display.max_columns', 250)
 pd.set_option('display.max_rows', 20 )
def create_model(input_shape, n_out):
input_tensor = Input(shape=input_shape)
 base_model = NASNetMobile(include_top=False,
weights='imagenet',
input_shape=input_shape)
 bn = BatchNormalization()(input_tensor)
 x = base_model(bn)
 x = GlobalAveragePooling2D()(x)
 x = Dense(1056, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 output = Dense(n_out, activation='sigmoid' )(x)
 model = Model(input_tensor, output)
 return model<choose_model_class>
train_df = pd.read_csv('.. /input/nfl-big-data-bowl-2020/train.csv', low_memory=False)
 print(train_df.shape)
epochs = 10; batch_size = 16
checkpoint = ModelCheckpoint('.. /working/InceptionV3.h5', monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3)
 early = EarlyStopping(monitor="val_loss",
mode="min",
patience=6)
 callbacks_list = [checkpoint]
indexes = np.arange(train_dataset_info.shape[0])
 np.random.shuffle(indexes)
 train_indexes, valid_indexes = train_test_split(indexes, test_size=0.19, random_state=8)
 train_generator = data_generator.create_train(
 train_dataset_info[train_indexes], batch_size,(SIZE,SIZE,3), augument=True)
 validation_generator = data_generator.create_train(
 train_dataset_info[valid_indexes], 32,(SIZE,SIZE,3), augument=False)
<choose_model_class>
train_df.Season.value_counts()
model = create_model(
 input_shape=(SIZE,SIZE,3),
n_out=28 )<train_model>
train_df.loc[train_df.HomeTeamAbbr.values == "ARI", 'HomeTeamAbbr'] = "ARZ"
train_df.loc[train_df.HomeTeamAbbr.values == "BAL", 'HomeTeamAbbr'] = "BLT"
train_df.loc[train_df.HomeTeamAbbr.values == "CLE", 'HomeTeamAbbr'] = "CLV"
train_df.loc[train_df.HomeTeamAbbr.values == "HOU", 'HomeTeamAbbr'] = "HST"
train_df.loc[train_df['Season'] == 2017, 'S'] =(train_df['S'][train_df['Season'] == 2017] - 2.4355)/1.2930 * 1.4551 + 2.7570
train_df.loc[train_df['Season'] == 2017, 'A'] =(train_df['A'][train_df['Season'] == 2017] - 1.5896)/0.8795 * 1.0603 + 1.7820
model.compile(loss='binary_crossentropy',
optimizer=Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0),
metrics=['accuracy'])
 model.fit_generator(
 train_generator,
steps_per_epoch=np.ceil(float(len(train_indexes)) / float(batch_size)) ,
validation_data=validation_generator,
validation_steps=np.ceil(float(len(valid_indexes)) / float(batch_size)) ,
epochs=5,
verbose=1,
callbacks=callbacks_list )<load_from_csv>
def strtoseconds(txt):
txt = txt.split(':')
 ans = int(txt[0])*60 + int(txt[1])+ int(txt[2])/60
return ans
def map_weather(txt):
ans = 1
if pd.isna(txt):
ans = 0
if 'partly' in txt:
ans *= 0.5
if 'climate controlled' in txt or 'indoor' in txt:
return ans*3
if 'sunny' in txt or 'sun' in txt:
return ans*2
if 'clear' in txt:
return ans
if 'cloudy' in txt:
return -ans
if 'rain' in txt or 'rainy' in txt or 'shower' in txt:
return -2*ans
if 'snow' in txt:
return -3*ans
return 0
def clean_wind_speed(windspeed):
ws = str(windspeed)
 if ws.isdigit() :
return int(ws)
 if '-' in ws:
return np.mean([int(i)for i in ws.split('-')])
 if ws.split(' ')[0].isdigit() :
return int(ws.split(' ')[0])
 if 'mph' in ws.lower() :
return int(ws.lower().split('mph')[0])
 else:
return 0
def clean_wind_direction(wind_direction):
wd = str(wind_direction ).upper()
if wd == 'N' or 'FROM S' in wd:
return 'north'
if wd == 'S' or 'FROM N' in wd:
return 'south'
if wd == 'W' or 'FROM E' in wd:
return 'west'
if wd == 'E' or 'FROM W' in wd:
return 'east'
if 'FROM SW' in wd or 'FROM SSW' in wd or 'FROM WSW' in wd:
return 'north east'
if 'FROM SE' in wd or 'FROM SSE' in wd or 'FROM ESE' in wd:
return 'north west'
if 'FROM NW' in wd or 'FROM NNW' in wd or 'FROM WNW' in wd:
return 'south east'
if 'FROM NE' in wd or 'FROM NNE' in wd or 'FROM ENE' in wd:
return 'south west'
if 'NW' in wd or 'NORTHWEST' in wd:
return 'north west'
if 'NE' in wd or 'NORTH EAST' in wd:
return 'north east'
if 'SW' in wd or 'SOUTHWEST' in wd:
return 'south west'
if 'SE' in wd or 'SOUTHEAST' in wd:
return 'south east'
return 'none'
def clean_turf(turf):
natural = ['Grass', 'Natural Grass', 'DD GrassMaster', 'SISGrass',
'Natural grass', 'Natural', 'Naturall Grass', 'natural grass', 'grass', 'natural']
artificial = ['Field Turf', 'Artificial', 'FieldTurf', 'UBU Speed Series-S5-M',
'A-Turf Titan', 'UBU Sports Speed S5-M', 'FieldTurf360', 'FieldTurf 360',
'Twenty-Four/Seven Turf', 'Artifical', 'Field turf', 'artificial']
if turf in natural:
return 0
elif turf in artificial:
return 1
else:
return 0.5
def euclid_distance(X, Y):
X = np.array(X)
 Y = np.array(Y)
 distance =(( X - Y)**2 ).sum()
return np.sqrt(distance)
 def get_height(txt):
feet = int(txt.split('-')[0])
 inch = int(txt.split('-')[1])
 return feet + np.round(inch/12, 2)
 def centeroidnp(arr):
length = arr.shape[0]
x = arr[:, 0]
y = arr[:, 1]
x_mean = x.mean()
y_mean = y.mean()
if not np.isfinite(x_mean):
x_mean = 0
if not np.isfinite(y_mean):
y_mean = 0
return x_mean, y_mean
def vertices_std(arr):
length = arr.shape[0]
x = arr[:, 0]
y = arr[:, 1]
x_std = x.std()
y_std = y.std()
if not np.isfinite(x_std):
x_std = 0
if not np.isfinite(y_std):
y_std = 0
return x_std, y_std
def cos_similarity(ax, ay, bx, by):
a = np.array([ax, ay] ).reshape(1, -1)
 b = np.array([bx, by] ).reshape(1, -1)
 return cosine_similarity(a, b)[0][0]
def area_triangle(a, b, c):
a = np.array(a)
 b = np.array(b)
 c = np.array(c)
 ab = b - a
ac = c - a
return 0.5*abs(ab[0]*ac[1] - ab[1]*ac[0])
 def standardize_vor(vertices):
v_std = []
for v in vertices:
if v[0] > 120 or v[0] < 0:
continue
if v[1] > 53.3 or v[1] < 0:
continue
v_std.append(list(v))
return np.array(v_std )
submit = pd.read_csv('.. /input/human-protein-atlas-image-classification/sample_submission.csv')
 predicted = []
draw_predict = []
model.load_weights('.. /working/InceptionV3.h5')
 for name in tqdm(submit['Id']):
path = os.path.join('.. /input/human-protein-atlas-image-classification/test/', name)
 image = data_generator.load_image(path,(SIZE,SIZE,3)) /255.
 score_predict = model.predict(image[np.newaxis])[0]
draw_predict.append(score_predict)
 label_predict = np.arange(28)[score_predict>=0.2]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label)
 submit['Predicted'] = predicted
np.save('draw_predict_InceptionV3.npy', score_predict)
 submit.to_csv('submit20.csv', index=False )<set_options>
def OffensePersonnelSplit(x):
dic = {'DB' : 0, 'DL' : 0, 'LB' : 0, 'OL' : 0, 'QB' : 0, 'RB' : 0, 'TE' : 0, 'WR' : 0, 'IRR': 0}
for xx in x.split(","):
xxs = xx.split(" ")
 if xxs[-1] not in dic.keys() :
dic['IRR'] = int(xxs[-2])
 else:
dic[xxs[-1]] = int(xxs[-2])
 return dic
def OffenseFormationMap(formation):
dic = {'SINGLEBACK': 0, 'SHOTGUN': 0, 'I_FORM': 0, 'PISTOL': 0, 'JUMBO': 0, 'WILDCAT': 0, 'EMPTY': 0}
if pd.isna(formation):
dic['EMPTY'] = 1
else:
fm = str(formation)
 if fm not in dic.keys() :
dic['EMPTY'] = 1
else:
dic[fm] = 1
return dic
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE<set_options>
def DefensePersonnelSplit(x):
dic = {'DB': 0, 'DL': 0, 'LB': 0, 'OL': 0, 'IRR': 0}
for xx in x.split(','):
xxs = xx.split(' ')
 if xxs[-1] not in dic.keys() :
dic['IRR'] = int(xxs[-2])
 else:
dic[xxs[-1]] = int(xxs[-2])
 return dic
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
def get_data(train, mode='train'):
data_df = pd.DataFrame()
df = pd.Series()
playid_list = train['PlayId'].unique()
for playid in tqdm(playid_list):
df_playid = train.query('PlayId == @playid')
 df_playid['ToLeft'] = df_playid.PlayDirection == 'left'
for c in df_playid.columns:
if df_playid[c].isnull().any() :
df_playid[c] = df_playid[c].fillna(df_playid[c].mean())

df['GameId'] = df_playid['GameId'].iloc[0]
df['PlayId'] = int(playid)
 play_direction = df_playid['PlayDirection'].iloc[0]
if play_direction == 'left':
df_playid['X'] = 120.0 - df_playid['X']
df_playid['Y'] = 53.3 - df_playid['Y']
df_playid['Dir_rad'] = np.mod(90 - df_playid.Dir, 360)* math.pi/180.0
if play_direction == 'left':
df_playid['Dir_rad'] = np.mod(np.pi + df_playid['Dir_rad'], 2*np.pi)
 df_playid['Orientation_rad'] = np.mod(df_playid.Orientation, 360)* math.pi/180.0
df_playid.loc[df_playid.Season >= 2018, 'Orientation_rad'] = np.mod(df_playid.loc[df_playid.Season >= 2018, 'Orientation']-90, 360)* math.pi/180.0
if play_direction == 'left':
df_playid['Orientation_rad'] = np.mod(np.pi + df_playid['Orientation_rad'], 2*np.pi)
 if df_playid['PossessionTeam'].iloc[0] == df_playid['FieldPosition'].iloc[0]:
df_playid['YardLine'] = df_playid['YardLine']
else :
df_playid['YardLine'] = 100 - df_playid['YardLine']

 df['GameWeather'] = map_weather(str(df_playid['GameWeather'].iloc[0] ).lower())
 df['Turf'] = clean_turf(df_playid['Turf'].iloc[0])
 if pd.isna(df_playid['Humidity'].iloc[0]):
df['Humidity'] = humidity_avg
else:
df['Humidity'] = df_playid['Humidity'].iloc[0]
if pd.isna(df_playid['Temperature'].iloc[0]):
df['Temperature'] = temp_avg
else:
df['Temperature'] = df_playid['Temperature'].iloc[0]
df['GameClock'] = strtoseconds(df_playid['GameClock'].iloc[0])
 if df_playid['Season'].iloc[0] < 2018:
df['Season'] = 0
else:
df['Season'] = 1
df['Quarter'] = df_playid['Quarter'].iloc[0]
df['Down'] = df_playid['Down'].iloc[0]

 rusher = df_playid['NflIdRusher'].iloc[0]
offense_team = df_playid[df_playid['NflId'] == rusher]['Team'].iloc[0]
df_offense = df_playid.query('Team == @offense_team')
 if offense_team == 'home':
df['ScoreDiff'] = df_playid['HomeScoreBeforePlay'].iloc[0] - df_playid['VisitorScoreBeforePlay'].iloc[0]
else:
df['ScoreDiff'] = df_playid['VisitorScoreBeforePlay'].iloc[0] - df_playid['HomeScoreBeforePlay'].iloc[0]
off_centroid =(df_offense['X'].mean() , df_offense['Y'].mean())
 off_Ori = df_offense['Orientation_rad'].mean()
off_Dir = df_offense['Dir_rad'].mean()
rusher_Ori = df_offense[df_offense['NflId'] == rusher]['Orientation_rad'].iloc[0]
rusher_Dir = df_offense[df_offense['NflId'] == rusher]['Dir_rad'].iloc[0]
rusher_X = df_playid[df_playid['NflId'] == rusher]['X'].iloc[0]
rusher_Y = df_playid[df_playid['NflId'] == rusher]['Y'].iloc[0]
rusher_S = df_offense[df_offense['NflId'] == rusher]['S'].iloc[0]
rusher_A = df_offense[df_offense['NflId'] == rusher]['A'].iloc[0]
rusher_Dis = df_playid[df_playid['NflId'] == rusher]['Dis'].iloc[0]
rusher_Age = df_playid['Season'].iloc[0] \
- int(df_playid[df_playid['NflId'] == rusher]['PlayerBirthDate'].iloc[0].split('/')[-1])
 df_offense['EuclidDistanceFromRusher'] = [euclid_distance([rusher_X, rusher_Y],
[df_offense['X'].iloc[i],
df_offense['Y'].iloc[i]])for i in range(len(df_offense)) ]
off_closest_EuclidDis_from_rusher = df_offense[df_offense['NflId'] != rusher]['EuclidDistanceFromRusher'].min()
closest_Off_from_rusher = df_offense[df_offense['EuclidDistanceFromRusher'] == off_closest_EuclidDis_from_rusher]['NflId'].iloc[0]
closest_Off_from_rusher_Dir = df_offense[df_offense['EuclidDistanceFromRusher'] == off_closest_EuclidDis_from_rusher]['Dir_rad'].iloc[0]
df['RusherClosestOffenseX'] = df_offense[df_offense['NflId'] == closest_Off_from_rusher]['X'].iloc[0]
df['RusherClosestOffenseY'] = df_offense[df_offense['NflId'] == closest_Off_from_rusher]['Y'].iloc[0]
df['RusherClosestOffenseS'] = df_offense[df_offense['NflId'] == closest_Off_from_rusher]['S'].iloc[0]
df['RusherClosestOffenseA'] = df_offense[df_offense['NflId'] == closest_Off_from_rusher]['A'].iloc[0]
df['RusherClosestOffenseDir'] = df_offense[df_offense['NflId'] == closest_Off_from_rusher]['Dir_rad'].iloc[0]
df['RusherClosestOffenseDis'] = df_offense[df_offense['NflId'] == closest_Off_from_rusher]['Dis'].iloc[0]
df['RusherClosestOffenseS_Horizontal'] = df['RusherClosestOffenseS'] * np.cos(closest_Off_from_rusher_Dir)
 df['RusherClosestOffenseS_Vertical'] = df['RusherClosestOffenseS'] * np.sin(closest_Off_from_rusher_Dir)
 df['RusherClosestOffenseA_Horizontal'] = df['RusherClosestOffenseA'] * np.cos(closest_Off_from_rusher_Dir)
 df['RusherClosestOffenseA_Vertical'] = df['RusherClosestOffenseA'] * np.sin(closest_Off_from_rusher_Dir)
 closest_top3_id_off = df_offense.sort_values(by = 'EuclidDistanceFromRusher')['NflId'][:3]
df['RusherSecondClosestOffenseX'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[1]]['X'].iloc[0]
df['RusherSecondClosestOffenseY'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[1]]['Y'].iloc[0]
df['RusherSecondClosestOffenseS'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[1]]['S'].iloc[0]
df['RusherSecondClosestOffenseA'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[1]]['A'].iloc[0]
df['RusherSecondClosestOffenseDir'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[1]]['Dir_rad'].iloc[0]
df['RusherSecondClosestOffenseDis'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[1]]['Dis'].iloc[0]
df['RusherThirdClosestOffenseX'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[2]]['X'].iloc[0]
df['RusherThirdClosestOffenseY'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[2]]['Y'].iloc[0]
df['RusherThirdClosestOffenseS'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[2]]['S'].iloc[0]
df['RusherThirdClosestOffenseA'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[2]]['A'].iloc[0]
df['RusherThirdClosestOffenseDir'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[2]]['Dir_rad'].iloc[0]
df['RusherThirdClosestOffenseDis'] = df_offense[df_offense['NflId'] == closest_top3_id_off.iloc[2]]['Dis'].iloc[0]
off_formation_dic = OffenseFormationMap(df_offense['OffenseFormation'].iloc[0])
 for fm in off_formation_dic.items() :
df['Formation_' + fm[0]] = fm[1]
df['AvgOffenseWeight'] = df_offense['PlayerWeight'].mean()
df['AvgOffenseHeight'] = df_offense['PlayerHeight'].apply(get_height ).mean()
df['AvgOffenseDis'] = df_offense['Dis'].mean()
df['StdOffenseDis'] = df_offense['Dis'].std()
df['MedOffenseDis'] = df_offense['Dis'].median()
df['AvgOffenseX'] = df_offense['X'].mean()
df['AvgOffenseY'] = df_offense['Y'].mean()
df['StdOffenseX'] = df_offense['X'].std()
df['StdOffenseY'] = df_offense['Y'].std()
df['AvgOffenseSpeed'] = df_offense['S'].mean()
df['AvgOffenseA'] = df_offense['A'].mean()
df['StdOffenseSpeed'] = df_offense['S'].std()
df['StdOffenseA'] = df_offense['A'].std()
df['MedOffenseSpeed'] = df_offense['S'].median()
df['MedOffenseA'] = df_offense['A'].median()
df['OffenseSVertical'] = df['AvgOffenseSpeed'] * np.sin(off_Dir)
 df['OffenseSHorizontal'] = df['AvgOffenseSpeed'] * np.cos(off_Dir)
 df['OffenseSVerticalMed'] = df['MedOffenseSpeed'] * np.sin(off_Dir)
 df['OffenseSHorizontalMed'] = df['MedOffenseSpeed'] * np.cos(off_Dir)
 df['OffenseAVertical'] = df['AvgOffenseA'] * np.sin(off_Dir)
 df['OffenseAHorizontal'] = df['AvgOffenseA'] * np.cos(off_Dir)
 df['OffenseAVerticalMed'] = df['MedOffenseA'] * np.sin(off_Dir)
 df['OffenseAHorizontalMed'] = df['MedOffenseA'] * np.cos(off_Dir)
 df['AvgOffenseDir'] = off_Dir
df['StdOffenseDir'] = df_offense['Dir_rad'].std()
df['OffenseSpreadX'] = df_offense['X'].max() - df_offense['X'].min()
df['OffenseSpreadY'] = df_offense['Y'].max() - df_offense['Y'].min()
df['OffDistanceFromYardline'] = abs(off_centroid[0] - df_playid['YardLine'].iloc[0])
 df['OffDistanceFromYardlineStd'] = abs(df_offense['X'] - df_playid['YardLine'].iloc[0] ).std()
df['OffDistanceFromYardlineMin'] = abs(df_offense['X'] - df_playid['YardLine'].iloc[0] ).min()
df['OffDistanceFromYardlineMed'] = abs(df_offense['X'] - df_playid['YardLine'].iloc[0] ).median()
offense_points = []
for idx, nflid in enumerate(df_offense['NflId']):
x = df_offense[df_offense['NflId'] == nflid]['X'].iloc[0]
y = df_offense[df_offense['NflId'] == nflid]['Y'].iloc[0]
offense_points.append([x, y])
 if nflid == rusher:
rusher_vor_region_idx = idx
offense_points = np.array(offense_points)
 offense_vor = Voronoi(offense_points)
 offense_vor_region = offense_vor.regions
rusher_vor_region = offense_vor_region[rusher_vor_region_idx]
rusher_vor_region = np.clip(np.array(rusher_vor_region), 0, None)
 rusher_vor_region_vertices = [offense_vor.vertices[v] for v in rusher_vor_region]
rusher_vor_region_vertices = standardize_vor(rusher_vor_region_vertices)
 if len(rusher_vor_region_vertices)< 1:
rusher_vor_closeX = rusher_X
rusher_vor_farX = rusher_X
rusher_vor_est_radius = 0
else:
rusher_vor_closeX = rusher_vor_region_vertices[np.argmin([abs(rusher_X - v[0])for v in rusher_vor_region_vertices])][0]
rusher_vor_farX = rusher_vor_region_vertices[np.argmax([abs(rusher_X - v[0])for v in rusher_vor_region_vertices])][0]
rusher_vor_est_radius = np.array([euclid_distance([rusher_X, rusher_Y], [v[0], v[1]])for v in rusher_vor_region_vertices] ).mean()
df['RusherVorRegionCloseX'] = rusher_vor_closeX
df['RusherVorRegionFarX'] = rusher_vor_farX
df['RusherVorCloseX_Yardline'] = rusher_vor_closeX - df_offense['YardLine'].iloc[0]
df['RusherVorFarX_Yardline'] = rusher_vor_farX - df_offense['YardLine'].iloc[0]
df['RusherVorArea'] = 2*np.pi*(rusher_vor_est_radius)**2
off_vor_v = standardize_vor(offense_vor.vertices)
 df['OffenseVorVerticesLen'] = len(off_vor_v)
 df['OffenseVorVerticesCentX'] = centeroidnp(off_vor_v)[0]
df['OffenseVorVerticesCentY'] = centeroidnp(off_vor_v)[1]
df['OffenseVorVerticesStdX'] = vertices_std(off_vor_v)[0]
df['OffenseVorVerticesStdY'] = vertices_std(off_vor_v)[1]
rusher_off_vor_distance = [euclid_distance([rusher_X, rusher_Y], [v[0], v[1]])for v in off_vor_v]
rusher_off_vor_distance.sort()
df['RusherVorVMinDistance'] = rusher_off_vor_distance[0]
df['RusherVorV2ndMinDistance'] = rusher_off_vor_distance[1]
df['IsRusherRB'] = int(df_offense[df_offense['NflId'] == rusher]['Position'] == 'RB')
 df['RusherPosFromYardLine'] = df_playid['YardLine'].iloc[0] - rusher_X
df['RusherDir'] = rusher_Dir
df['RusherOri'] = rusher_Ori
df['RusherOriDirDiff'] = abs(rusher_Ori - rusher_Dir)
 df['RusherSpeed'] = rusher_S
df['RusherAcceleration'] = rusher_A
df['RusherSVertical'] = df_offense[df_offense['NflId'] == rusher]['S'].iloc[0] * np.sin(rusher_Dir)
 df['RusherSHorizontal'] = df_offense[df_offense['NflId'] == rusher]['S'].iloc[0] * np.cos(rusher_Dir)
 df['RusherAVertical'] = df_offense[df_offense['NflId'] == rusher]['A'].iloc[0] * np.sin(rusher_Dir)
 df['RusherAHorizontal'] = df_offense[df_offense['NflId'] == rusher]['A'].iloc[0] * np.cos(rusher_Dir)
 df['RusherDis'] = rusher_Dis
df['RusherAge'] = rusher_Age
df['RusherWeight'] = df_offense[df_offense['NflId'] == rusher]['PlayerWeight'].iloc[0]
df['RusherHeight'] = get_height(df_offense[df_offense['NflId'] == rusher]['PlayerHeight'].iloc[0])
 df['RusherPower'] = 0.5 * df['RusherWeight'] *(rusher_S**2)
 df['RusherPowerHorizontal'] = 0.5 * df['RusherWeight'] *(rusher_S * np.cos(rusher_Dir)) **2
off_pos = OffensePersonnelSplit(df_playid['OffensePersonnel'].iloc[0])
 for pos in off_pos.items() :
df['Offense_' + pos[0]] = pos[1]
df_playid['TimeHandoff'] = pd.to_datetime(df_playid['TimeHandoff'])
 df_playid['TimeSnap'] = pd.to_datetime(df_playid['TimeSnap'])
 thts =(df_playid['TimeHandoff'].dt.second - df_playid['TimeSnap'].dt.second ).iloc[0]
if thts < 0:
thts = thts + 60
df['TimeTillHandoff'] = thts
df['Distance'] = df_playid['Distance'].iloc[0]
df['YardLine'] = df_playid['YardLine'].iloc[0]
month = int(df_playid['TimeSnap'].dt.month.iloc[0])
 month_dic = {9:0, 10:0, 11:0, 12:0, 'else':0}
if month in month_dic.keys() :
month_dic[month] = 1
else:
month_dic['else'] = 1
for mon in month_dic.keys() :
df['Month'+str(mon)] = month_dic[mon]

 df_defense = df_playid.query('Team != @offense_team')
 df_defense['DisXFromRusher'] = df_defense['X'] - rusher_X
df_defense['DisYFromRusher'] = df_defense['Y'] - rusher_Y
df_defense['DisFromRusher'] = np.sqrt(df_defense['DisXFromRusher']**2 + df_defense['DisYFromRusher']**2)
 def_centroid = [df_defense['X'].mean() , df_defense['Y'].mean() ]
def_Ori = df_defense['Orientation_rad'].mean()
def_Dir = df_defense['Dir_rad'].mean()
df['AvgDefenseWeight'] = df_defense['PlayerWeight'].mean()
df['AvgDefenseHeight'] = df_defense['PlayerHeight'].apply(get_height ).mean()
df['AvgDefenseX'] = df_defense['X'].mean()
df['AvgDefenseY'] = df_defense['Y'].mean()
df['StdDefenseX'] = df_defense['X'].std()
df['StdDefenseY'] = df_defense['Y'].std()
df['AvgDefenseSpeed'] = df_defense['S'].mean()
df['AvgDefenseA'] = df_defense['A'].mean()
df['StdDefenseSpeed'] = df_defense['S'].std()
df['StdDefenseA'] = df_defense['A'].std()
df['MedDefenseSpeed'] = df_defense['S'].median()
df['MedDefenseA'] = df_defense['A'].median()
df['DefenseSVertical'] = df['AvgDefenseSpeed'] * np.sin(def_Dir)
 df['DefenseSHorizontal'] = df['AvgDefenseSpeed'] * np.cos(def_Dir)
 df['DefenseSVertical'] = df['MedDefenseSpeed'] * np.sin(def_Dir)
 df['DefenseSHorizontal'] = df['MedDefenseSpeed'] * np.cos(def_Dir)
 df['DefenseAVertical'] = df['AvgDefenseA'] * np.sin(def_Dir)
 df['DefenseAHorizontal'] = df['AvgDefenseA'] * np.cos(def_Dir)
 df['DefenseAVertical'] = df['MedDefenseA'] * np.sin(def_Dir)
 df['DefenseAHorizontal'] = df['MedDefenseA'] * np.cos(def_Dir)
 df['AvgDefenseDis'] = df_defense['Dis'].mean()
df['StdDefenseDis'] = df_defense['Dis'].std()
df['MedDefenseDis'] = df_defense['Dis'].median()
df['AvgDefenseDir'] = def_Dir
df['StdDefenseDir'] = df_defense['Dir_rad'].std()
df['MedDefenseDir'] = df_defense['Dir_rad'].median()
df['AvgDisFromRusher'] = df_defense['DisFromRusher'].mean()
df['StdDisFromRusher'] = df_defense['DisFromRusher'].std()
df['MinDisFromRusher'] = df_defense['DisFromRusher'].min()
df['MaxDisFromRusher'] = df_defense['DisFromRusher'].max()
df['MedDisFromRusher'] = df_defense['DisFromRusher'].median()
df['DefDistanceFromYardline'] = abs(def_centroid[0] - df_playid['YardLine'].iloc[0])
 df['DefDistanceFromYardlineStd'] = abs(df_defense['X'] - df_playid['YardLine'].iloc[0] ).std()
df['DefDistanceFromYardlineMin'] = abs(df_defense['X'] - df_playid['YardLine'].iloc[0] ).min()
df['DefDistanceFromYardlineMed'] = abs(df_defense['X'] - df_playid['YardLine'].iloc[0] ).median()
def_pos = DefensePersonnelSplit(df_playid['DefensePersonnel'].iloc[0])
 for pos in def_pos.items() :
df['Def_' + pos[0]] = pos[1]
d_in_box = df_defense['DefendersInTheBox'].iloc[0]
if pd.isna(d_in_box):
df['DefendersInTheBox'] = 7
else:
df['DefendersInTheBox'] = d_in_box
df['DefenseSpreadX'] = df_defense['X'].max() - df_defense['X'].min()
df['DefenseSpreadY'] = df_defense['Y'].max() - df_defense['Y'].min()
defense_points = []
for x, y in zip(df_defense['X'], df_defense['Y']):
defense_points.append([x, y])
 defense_points = np.array(defense_points)
 defense_vor = Voronoi(defense_points)
 def_vor_v = standardize_vor(defense_vor.vertices)
 df['DefenseVorVerticesLen'] = len(def_vor_v)
 df['DefenseVorVerticesCentX'] = centeroidnp(def_vor_v)[0]
df['DefenseVorVerticesCentY'] = centeroidnp(def_vor_v)[1]
df['DefenseVorVerticesStdX'] = vertices_std(def_vor_v)[0]
df['DefenseVorVerticesStdY'] = vertices_std(def_vor_v)[1]

 if rusher_Dir > 0.5*math.pi:
num_off_on_rusher_dir = len(df_offense[df_offense['NflId'] != rusher][df_offense['Y'] < rusher_Y])
 num_def_on_rusher_dir = len(df_defense[df_defense['NflId'] != rusher][df_defense['Y'] < rusher_Y])
 else:
num_off_on_rusher_dir = len(df_offense[df_offense['NflId'] != rusher][df_offense['Y'] >= rusher_Y])
 num_def_on_rusher_dir = len(df_defense[df_defense['NflId'] != rusher][df_defense['Y'] >= rusher_Y])
 df['NumOffonRusherDir'] = num_off_on_rusher_dir
df['NumDefonRusherDir'] = num_def_on_rusher_dir
df['RusherVSDefCentroid'] = euclid_distance([rusher_X, rusher_Y], def_centroid)
 df['RusherVSOffVoronoiCentroid'] = euclid_distance([rusher_X, rusher_Y], [centeroidnp(off_vor_v)[0], centeroidnp(off_vor_v)[1]])
 rusher_vs_offvor = []
for i,v in enumerate(off_vor_v):
rusher_vs_offvor.append(euclid_distance([rusher_X, rusher_Y], off_vor_v[i]))
df['RusherVSOffVoronoiStd'] = np.array(rusher_vs_offvor ).std()
df['RusherVSOffVoronoiMed'] = np.median(np.array(rusher_vs_offvor))
df['RusherVSOffVoronoiMin'] = np.array(rusher_vs_offvor ).min()
df['RusherVSOffVoronoiMax'] = np.array(rusher_vs_offvor ).max()
df['RusherVSDefCentroid'] = euclid_distance([rusher_X, rusher_Y], def_centroid)
 df['RusherVSDefVoronoiCentroid'] = euclid_distance([rusher_X, rusher_Y], [centeroidnp(def_vor_v)[0], centeroidnp(def_vor_v)[1]])
 rusher_vs_defvor = []
for i,v in enumerate(def_vor_v):
rusher_vs_defvor.append(euclid_distance([rusher_X, rusher_Y], def_vor_v[i]))
df['RusherVSDefVoronoiStd'] = np.array(rusher_vs_defvor ).std()
df['RusherVSDefVoronoiMed'] = np.median(np.array(rusher_vs_defvor))
df['RusherVSDefVoronoiMin'] = np.array(rusher_vs_defvor ).min()
df['RusherVSDefVoronoiMax'] = np.array(rusher_vs_defvor ).max()
df_defense['EuclidDistanceFromRusher'] = [euclid_distance([rusher_X, rusher_Y],
[df_defense[df_defense['NflId'] != rusher]['X'].iloc[i],
df_defense[df_defense['NflId'] != rusher]['Y'].iloc[i]])for i in range(len(df_defense[df_defense['NflId'] != rusher])) ]
df_defense['DisYfromRusher'] = abs(df_defense[df_defense['NflId'] != rusher]['Y'] - rusher_Y)
 closest_EuclidDis_from_rusher = df_defense['EuclidDistanceFromRusher'].min()
closest_Def_from_rusher = df_defense[df_defense['EuclidDistanceFromRusher'] == closest_EuclidDis_from_rusher]['NflId'].iloc[0]
closest_Def_from_rusher_Dir = df_defense[df_defense['EuclidDistanceFromRusher'] == closest_EuclidDis_from_rusher]['Dir_rad'].iloc[0]
df['RusherClosestDefenseX'] = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['X'].iloc[0]
df['RusherClosestDefenseY'] = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['Y'].iloc[0]
df['RusherClosestDefenseS'] = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['S'].iloc[0]
df['RusherClosestDefenseA'] = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['A'].iloc[0]
df['RusherClosestDefenseDir'] = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['Dir_rad'].iloc[0]
df['RusherClosestDefenseDis'] = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['Dis'].iloc[0]
df['RusherClosestDefenseS_Horizontal'] = df['RusherClosestDefenseS'] * np.cos(closest_Def_from_rusher_Dir)
 df['RusherClosestDefenseS_Vertical'] = df['RusherClosestDefenseS'] * np.sin(closest_Def_from_rusher_Dir)
 df['RusherClosestDefenseA_Horizontal'] = df['RusherClosestDefenseA'] * np.cos(closest_Def_from_rusher_Dir)
 df['RusherClosestDefenseA_Vertical'] = df['RusherClosestDefenseA'] * np.sin(closest_Def_from_rusher_Dir)
 closest_top3_id = df_defense.sort_values(by = 'DisFromRusher')['NflId'][:3]
df['RusherSecondClosestDefenseX'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[1]]['X'].iloc[0]
df['RusherSecondClosestDefenseY'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[1]]['Y'].iloc[0]
df['RusherSecondClosestDefenseS'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[1]]['S'].iloc[0]
df['RusherSecondClosestDefenseA'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[1]]['A'].iloc[0]
df['RusherSecondClosestDefenseDir'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[1]]['Dir_rad'].iloc[0]
df['RusherSecondClosestDefenseDis'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[1]]['Dis'].iloc[0]
df['RusherThirdClosestDefenseX'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[2]]['X'].iloc[0]
df['RusherThirdClosestDefenseY'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[2]]['Y'].iloc[0]
df['RusherThirdClosestDefenseS'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[2]]['S'].iloc[0]
df['RusherThirdClosestDefenseA'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[2]]['A'].iloc[0]
df['RusherThirdClosestDefenseDir'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[2]]['Dir_rad'].iloc[0]
df['RusherThirdClosestDefenseDis'] = df_defense[df_defense['NflId'] == closest_top3_id.iloc[2]]['Dis'].iloc[0]
first_closestDef_coordinate = [df['RusherClosestDefenseX'], df['RusherClosestDefenseY']]
second_closestDef_coordinate = [df['RusherSecondClosestDefenseX'], df['RusherSecondClosestDefenseY']]
third_closestDef_coordinate = [df['RusherThirdClosestDefenseX'], df['RusherThirdClosestDefenseY']]
df['ClosestDefenceTriangle'] = area_triangle(first_closestDef_coordinate, second_closestDef_coordinate, third_closestDef_coordinate)
 closest_def_Dir = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['Dir_rad'].iloc[0]
closest_def_S = df_defense[df_defense['NflId'] == closest_Def_from_rusher]['S'].iloc[0]
df['RusherClosestDefenseDir'] = closest_def_Dir
df['Rusher-DefenderMinDistance'] = euclid_distance([rusher_X, rusher_Y],
[df['RusherClosestDefenseX'], df['RusherClosestDefenseY']])
 if closest_def_S < 0.01:
df['EstTimeTackled'] = 10
else:
df['EstTimeTackled'] = df['Rusher-DefenderMinDistance']/(closest_def_S)
 df_defense['SVertical'] = df_defense['S'] * np.sin(def_Dir)
 df_defense['SHorizontal'] = df_defense['S'] * np.cos(def_Dir)
 df['RusherDefender_CosSimilarity'] = cos_similarity(df['RusherSHorizontal'], df['RusherSVertical'],
df['RusherClosestDefenseS']*np.sin(closest_def_Dir), df['RusherClosestDefenseS']*np.cos(closest_def_Dir))

 if mode == 'train':
df['Yards'] = df_playid['Yards'].iloc[0]
data_df = data_df.append(df, ignore_index=True)
 if mode == 'train':
data_df = data_df.astype({'PlayId': int, 'GameId': int, 'Yards': int})
 else:
data_df = data_df.astype({'PlayId': int, 'GameId': int})
 return data_df
GCS_DS_PATH = KaggleDatasets().get_gcs_path('flower-classification' )<define_variables>
warnings.filterwarnings('ignore')
 X = get_data(train_df )
EXT_DS_PATH = KaggleDatasets().get_gcs_path('oxford-102-for-tpu-competition' )<define_variables>
for c in X.columns:
if(X[c].dtype != int):
X[c] = X[c].astype('float32')
 for c in X.columns:
if X[c].isnull().any() :
print(c)
 print(X[c].mean())
 X[c] = X[c].fillna(X[c].mean() )
USE_EXTERNAL = True
SKIP_VALIDATION = False<define_variables>
X.isnull().any()
if tpu:
sz = 512
else:
sz = 192
IMAGE_SIZE = [sz, sz]
EPOCHS = 20
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
AUG_BATCH = BATCH_SIZE
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')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
<define_variables>
yards_train = X['Yards']
X_gameid = pd.DataFrame(X['GameId'], columns=['GameId'])
 X_features = X.drop(['Yards', 'PlayId', 'GameId'], axis=1)
 print(X_features.shape)
 print(yards_train.shape)
 X_gameid.head()
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>
y_train = np.zeros(( yards_train.shape[0], 199))
for idx, target in enumerate(list(yards_train)) :
y_train[idx][99 + int(target)] = 1
y_train.shape
np.set_printoptions(threshold=15, linewidth=80)
 def batch_to_numpy_images_and_labels(data):
images, labels = data
numpy_images = images.numpy()
numpy_labels = labels.numpy()
if numpy_labels.dtype == object:
numpy_labels = [None for _ in enumerate(numpy_images)]
return numpy_images, numpy_labels
def title_from_label_and_target(label, correct_label):
if correct_label is None:
return CLASSES[label], True
correct =(label == correct_label)
 return "{} [{}{}{}]".format(CLASSES[label], 'OK' if correct else 'NO', u"\u2192" if not correct else '',
CLASSES[correct_label] if not correct else ''), correct
def display_one_flower(image, title, subplot, red=False, titlesize=16):
plt.subplot(*subplot)
 plt.axis('off')
 plt.imshow(image)
 if len(title)> 0:
plt.title(title, fontsize=int(titlesize)if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))
return(subplot[0], subplot[1], subplot[2]+1)
 def display_batch_of_images(databatch, predictions=None):

 images, labels = batch_to_numpy_images_and_labels(databatch)
 if labels is None:
labels = [None for _ in enumerate(images)]
rows = int(math.sqrt(len(images)))
 cols = len(images)//rows
FIGSIZE = 13.0
SPACING = 0.1
subplot=(rows,cols,1)
 if rows < cols:
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
else:
plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))
for i,(image, label)in enumerate(zip(images[:rows*cols], labels[:rows*cols])) :
if label is None:
title = ''
elif isinstance(label, int):
title = CLASSES[label]
else:
idx = np.argmax(label, axis=0)
 title = CLASSES[idx]
correct = True
if predictions is not None:
title, correct = title_from_label_and_target(predictions[i], label)
 dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3
subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)
 plt.tight_layout()
if label is None and predictions is None:
plt.subplots_adjust(wspace=0, hspace=0)
 else:
plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
 plt.show()
def display_confusion_matrix(cmat, score, precision, recall):
plt.figure(figsize=(15,15))
ax = plt.gca()
ax.matshow(cmat, cmap='Reds')
 ax.set_xticks(range(len(CLASSES)))
 ax.set_xticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_xticklabels() , rotation=45, ha="left", rotation_mode="anchor")
 ax.set_yticks(range(len(CLASSES)))
 ax.set_yticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_yticklabels() , rotation=45, ha="right", rotation_mode="anchor")
 titlestring = ""
if score is not None:
titlestring += 'f1 = {:.3f} '.format(score)
 if precision is not None:
titlestring += '
precision = {:.3f} '.format(precision)
 if recall is not None:
titlestring += '
recall = {:.3f} '.format(recall)
 if len(titlestring)> 0:
ax.text(101, 1, titlestring, fontdict={'fontsize': 18, 'horizontalalignment':'right', 'verticalalignment':'top', 'color':'
plt.show()
def display_training_curves(training, validation, title, subplot):
if subplot%10==1:
plt.subplots(figsize=(10,10), facecolor='
plt.tight_layout()
ax = plt.subplot(subplot)
 ax.set_facecolor('
ax.plot(training)
 ax.plot(validation)
 ax.set_title('model '+ title)
 ax.set_ylabel(title)
 ax.set_xlabel('epoch')
 ax.legend(['train', 'valid.'] )<categorify>
scaler = StandardScaler()
X_features_sc = scaler.fit_transform(X_features)
 X_features = pd.DataFrame(X_features_sc, index = X_features.index, columns = X_features.columns)
 X_features.head()
def get_batch_transformatioin_matrix(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):

 batch_size = tf.cast(tf.reduce_sum(tf.ones_like(rotation)) , tf.int64)
 rotation = tf.constant(math.pi)* rotation / 180.0
shear = tf.constant(math.pi)* shear / 180.0
one = tf.ones_like(rotation, dtype=tf.float32)
 zero = tf.zeros_like(rotation, dtype=tf.float32)
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 rotation_matrix_temp = tf.stack([c1, s1, zero, -s1, c1, zero, zero, zero, one], axis=0)
 rotation_matrix_temp = tf.transpose(rotation_matrix_temp)
 rotation_matrix = tf.reshape(rotation_matrix_temp, shape=(batch_size, 3, 3))
c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix_temp = tf.stack([one, s2, zero, zero, c2, zero, zero, zero, one], axis=0)
 shear_matrix_temp = tf.transpose(shear_matrix_temp)
 shear_matrix = tf.reshape(shear_matrix_temp, shape=(batch_size, 3, 3))
zoom_matrix_temp = tf.stack([one / height_zoom, zero, zero, zero, one / width_zoom, zero, zero, zero, one], axis=0)
 zoom_matrix_temp = tf.transpose(zoom_matrix_temp)
 zoom_matrix = tf.reshape(zoom_matrix_temp, shape=(batch_size, 3, 3))
shift_matrix_temp = tf.stack([one, zero, height_shift, zero, one, width_shift, zero, zero, one], axis=0)
 shift_matrix_temp = tf.transpose(shift_matrix_temp)
 shift_matrix = tf.reshape(shift_matrix_temp, shape=(batch_size, 3, 3))
return tf.linalg.matmul(tf.linalg.matmul(rotation_matrix, shear_matrix), tf.linalg.matmul(zoom_matrix, shift_matrix))<categorify>
best_params_lgb = {'lambda_l1': 0.13413394854686794,
'lambda_l2': 0.0009122197743451751,
'num_leaves': 44,
'feature_fraction': 0.4271070738920401,
'bagging_fraction': 0.9999128827046064,
'bagging_freq': 3,
"learning_rate": 0.005,
'min_child_samples': 43,
'objective': 'regression',
'metric': 'mae',
'verbosity': -1,
'boosting_type': 'gbdt',
"boost_from_average" : False,
'random_state': 42}
def batch_transform(images, labels):

 DIM = images.shape[1]
XDIM = DIM % 2
batch_size = tf.cast(tf.reduce_sum(tf.ones_like(images)) /(images.shape[1] * images.shape[2] * images.shape[3]), tf.int64)
 rot = 15.0 * tf.random.normal([batch_size], dtype='float32')
 shr = 5.0 * tf.random.normal([batch_size], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([batch_size], dtype='float32')/ 10.0
w_zoom = 1.0 + tf.random.normal([batch_size], dtype='float32')/ 10.0
h_shift = 16.0 * tf.random.normal([batch_size], dtype='float32')
 w_shift = 16.0 * tf.random.normal([batch_size], dtype='float32')
 m = get_batch_transformatioin_matrix(rot, shr, h_zoom, w_zoom, h_shift, w_shift)
 x = tf.repeat(tf.range(DIM // 2, -DIM // 2, -1), DIM)
 y = tf.tile(tf.range(-DIM // 2, DIM // 2), [DIM])
 z = tf.ones([DIM * DIM], dtype='int32')
 idx = tf.stack([x, y, z])
 idx2 = tf.linalg.matmul(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM // 2 + XDIM + 1, DIM // 2)
 idx3 = tf.stack([DIM // 2 - idx2[:, 0, ], DIM // 2 - 1 + idx2[:, 1, ]], axis=1)
 d = tf.gather_nd(images, tf.transpose(idx3, perm=[0, 2, 1]), batch_dims=1)
 new_images = tf.reshape(d,(batch_size, DIM, DIM, 3))
return new_images, labels<categorify>
feature_importance_df = pd.DataFrame()
kfold = KFold(n_splits = 5, shuffle=True, random_state=42)
 for k_fold,(tr_inds, val_inds)in enumerate(kfold.split(X_features, yards_train)) :
print(len(tr_inds), len(val_inds))
tr_x,tr_y = X_features.iloc[tr_inds],yards_train[tr_inds]
val_x,val_y = X_features.iloc[val_inds],yards_train[val_inds]
model = lgb.LGBMRegressor(**best_params_lgb, n_estimators=300, n_jobs=-1, early_stopping_rounds=100)
 model.fit(tr_x, tr_y, eval_set=[(tr_x, tr_y),(val_x, val_y)], eval_metric='mae', verbose=10)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = tr_x.columns
fold_importance_df["importance"] = model.feature_importances_[:len(tr_x.columns)]
fold_importance_df["fold"] = k_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
def onehot(image,label):
return image,tf.one_hot(label,len(CLASSES))<define_variables>
feature_name = cols_imp
def batch_cutmix(images, labels, PROBABILITY=1.0, batch_size=0):
DIM = IMAGE_SIZE[0]
CLASSES = 104
if batch_size == 0:
batch_size = AUG_BATCH
do_cutmix = tf.cast(tf.random.uniform([batch_size], 0, 1)<= PROBABILITY, tf.int32)
 new_image_indices = tf.cast(tf.random.uniform([batch_size], 0, batch_size), tf.int32)
 new_x = tf.cast(tf.random.uniform([batch_size], 0, DIM), tf.int32)
 new_y = tf.cast(tf.random.uniform([batch_size], 0, DIM), tf.int32)
 b = tf.random.uniform([batch_size], 0, 1)
 new_width = tf.cast(DIM * tf.math.sqrt(1-b), tf.int32)* do_cutmix
new_y0 = tf.math.maximum(0, new_y - new_width // 2)
 new_y1 = tf.math.minimum(DIM, new_y + new_width // 2)
 new_x0 = tf.math.maximum(0, new_x - new_width // 2)
 new_x1 = tf.math.minimum(DIM, new_x + new_width // 2)
 target = tf.broadcast_to(tf.range(DIM), shape=(batch_size, DIM))
mask_y = tf.math.logical_and(new_y0[:, tf.newaxis] <= target, target <= new_y1[:, tf.newaxis])
 mask_x = tf.math.logical_and(new_x0[:, tf.newaxis] <= target, target <= new_x1[:, tf.newaxis])
 mask = tf.cast(tf.math.logical_and(mask_y[:, :, tf.newaxis], mask_x[:, tf.newaxis, :]), tf.float32)
 new_images = images * tf.broadcast_to(1 - mask[:, :, :, tf.newaxis], [batch_size, DIM, DIM, 3])+ \
tf.gather(images, new_image_indices)* tf.broadcast_to(mask[:, :, :, tf.newaxis], [batch_size, DIM, DIM, 3])
 a = tf.cast(new_width ** 2 / DIM ** 2, tf.float32)
 if len(labels.shape)== 1:
labels = tf.one_hot(labels, CLASSES)
 new_labels =(1-a)[:, tf.newaxis] * labels + a[:, tf.newaxis] * tf.gather(labels, new_image_indices)
 return new_images, new_labels<define_variables>
X_train = X_gameid.join(X_features)
 print(X_train.shape)
 print(y_train.shape)
 X_train.head()
X_train_2017 = X_train.query("Season < 1.0")
 X_train_2018 = X_train.query("Season >= 1.0")
 X_train[feature_name].to_csv('train_feat.csv')
 X_train_2017.to_csv('train17.csv')
 X_train_2018.to_csv('train18.csv' )
def batch_mixup(images, labels, PROBABILITY=1.0, batch_size=0):
DIM = IMAGE_SIZE[0]
CLASSES = 104
if batch_size == 0:
batch_size = AUG_BATCH
do_mixup = tf.cast(tf.random.uniform([batch_size], 0, 1)<= PROBABILITY, tf.int32)
 new_image_indices = tf.cast(tf.random.uniform([batch_size], 0, batch_size), tf.int32)
 a = tf.random.uniform([batch_size], 0, 1)* tf.cast(do_mixup, tf.float32)
 new_images =(1-a)[:, tf.newaxis, tf.newaxis, tf.newaxis] * images + a[:, tf.newaxis, tf.newaxis, tf.newaxis] * tf.gather(images, new_image_indices)
 if len(labels.shape)== 1:
labels = tf.one_hot(labels, CLASSES)
 new_labels =(1-a)[:, tf.newaxis] * labels + a[:, tf.newaxis] * tf.gather(labels, new_image_indices)
 return new_images, new_labels<categorify>
class CRPSCallback(Callback):
def __init__(self,validation, predict_batch_size=20, include_on_batch=False):
super(CRPSCallback, self ).__init__()
self.validation = validation
self.predict_batch_size = predict_batch_size
self.include_on_batch = include_on_batch
print('validation shape',len(self.validation))
def on_batch_begin(self, batch, logs={}):
pass
def on_train_begin(self, logs={}):
if not('CRPS_score_val' in self.params['metrics']):
self.params['metrics'].append('CRPS_score_val')
 def on_batch_end(self, batch, logs={}):
if(self.include_on_batch):
logs['CRPS_score_val'] = float('-inf')
 def on_epoch_end(self, epoch, logs={}):
logs['CRPS_score_val'] = float('-inf')
 if(self.validation):
X_valid, y_valid = self.validation[0], self.validation[1]
y_pred = self.model.predict(X_valid)
 y_true = np.clip(np.cumsum(y_valid, axis=1), 0, 1)
 y_pred = np.clip(np.cumsum(y_pred, axis=1), 0, 1)
 val_s =(( y_true - y_pred)** 2 ).sum(axis=1 ).sum(axis=0)/(199 * X_valid.shape[0])
 val_s = np.round(val_s, 6)
 logs['CRPS_score_val'] = val_s
def transform_cut_mix(image,label):
DIM = IMAGE_SIZE[0]
CLASSES = 104
SWITCH = 0.5
CUTMIX_PROB = 0.666
MIXUP_PROB = 0.666
image2, label2 = batch_cutmix(image, label, CUTMIX_PROB)
 image3, label3 = batch_mixup(image, label, MIXUP_PROB)
 imgs = []; labs = []
for j in range(AUG_BATCH):
P = tf.cast(tf.random.uniform([],0,1)<=SWITCH, tf.float32)
 imgs.append(P*image2[j,]+(1-P)*image3[j,])
 labs.append(P*label2[j,]+(1-P)*label3[j,])
 image4 = tf.reshape(tf.stack(imgs),(AUG_BATCH,DIM,DIM,3))
label4 = tf.reshape(tf.stack(labs),(AUG_BATCH,CLASSES))
return image4,label4<categorify>
INPUT_SIZE = X_train[feature_name].shape[1]
OUTPUT_SIZE = 199
print(INPUT_SIZE)
 BATCH_SIZE = 512
EPOCHS = 50
def get_model(x_tr, y_tr, x_val, y_val):
model = Sequential()
model.add(Dense(1024, input_shape=(INPUT_SIZE,)))
 model.add(Activation('relu'))
model.add(Dropout(0.7))
model.add(BatchNormalization())
 model.add(Dense(512, input_shape=(INPUT_SIZE,)))
 model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
 model.add(Dense(384))
model.add(Activation('relu'))
model.add(Dropout(0.35))
model.add(BatchNormalization())
 model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.3))
model.add(BatchNormalization())
 model.add(Dense(OUTPUT_SIZE))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=[])
 es = EarlyStopping(monitor='CRPS_score_val',
mode='min',
restore_best_weights=True,
verbose=1,
patience=12)
 mc = ModelCheckpoint('best_model.h5',monitor='CRPS_score_val',mode='min',
save_best_only=True, verbose=1, save_weights_only=True)
 model.fit(x_tr, y_tr,
batch_size = BATCH_SIZE,
epochs=EPOCHS,
callbacks = [CRPSCallback(validation =(x_val, y_val)) , es, mc],
verbose=1)
 y_pred = model.predict(x_val)
 y_valid = y_val
y_true = np.clip(np.cumsum(y_valid, axis=1), 0, 1)
 y_pred = np.clip(np.cumsum(y_pred, axis=1), 0, 1)
 val_s =(( y_true - y_pred)** 2 ).sum(axis=1 ).sum(axis=0)/(199 * x_val.shape[0])
 crps = np.round(val_s, 6)
 return model ,crps
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
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image, label
def get_training_dataset(dataset, simple_aug=False, advance_aug=False, cut_mix_aug=0, do_onehot=True):
if simple_aug:
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.batch(AUG_BATCH)
 if advance_aug:
dataset = dataset.map(batch_transform, num_parallel_calls=AUTO)
 if do_onehot:
dataset = dataset.map(onehot, num_parallel_calls=AUTO)
 if cut_mix_aug==1:
dataset = dataset.map(batch_cutmix, num_parallel_calls=AUTO)
 elif cut_mix_aug==2:
dataset = dataset.map(batch_mixup, num_parallel_calls=AUTO)
 elif cut_mix_aug==3:
dataset = dataset.map(transform_cut_mix, num_parallel_calls=AUTO)
 dataset = dataset.unbatch()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(dataset, ordered=False, repeated=False, do_onehot=True):
if repeated:
dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 if do_onehot:
dataset = dataset.map(onehot, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE, drop_remainder=repeated)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 def int_div_round_up(a, b):
return(a + b - 1)// b 
<define_variables>
losses = []
models = []
crps_csv = []
kfold = KFold(5, shuffle=True, random_state=42)
 for k_fold,(tr_inds, val_inds)in enumerate(kfold.split(yards_train)) :
print(len(tr_inds), len(val_inds))
tr_x,tr_y = X_train.iloc[tr_inds][feature_name],y_train[tr_inds]
val_x,val_y = X_train.iloc[val_inds][feature_name],y_train[val_inds]
model,crps = get_model(tr_x,tr_y,val_x,val_y)
 models.append(model)
 print("the %d fold crps is %f"%(( k_fold+1),crps))
crps_csv.append(crps)
 print("mean crps is %f"%np.mean(crps_csv))
print(len(models))
if USE_EXTERNAL:
TRAINING_FILENAMES = TRAINING_FILENAMES + EXT_TRAINING_FILENAMES
if SKIP_VALIDATION:
TRAINING_FILENAMES = TRAINING_FILENAMES + VALIDATION_FILENAMES
NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)if not SKIP_VALIDATION else 0
NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
VALIDATION_STEPS = int_div_round_up(NUM_VALIDATION_IMAGES, BATCH_SIZE)
 print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<create_dataframe>
def predict(x_te):
model_num = len(models)
 for k,m in enumerate(models):
if k==0:
y_pred = m.predict(x_te,batch_size=BATCH_SIZE)
 else:
y_pred+=m.predict(x_te,batch_size=BATCH_SIZE)
 y_pred = y_pred / model_num
return y_pred
<train_model><EOS> 
env = nflrush.make_env()
iter_test = env.iter_test()
row_count = 0
for(test_df, sample_prediction_df)in iter_test:
basetable = get_data(test_df, mode='test')
 for c in basetable.columns:
if(basetable[c].dtype != int):
basetable[c] = basetable[c].astype('float32')
 for c in basetable.columns:
if basetable[c].isnull().any() :
print(c)
 print(basetable[c].mean())
 basetable[c] = basetable[c].fillna(0)
 basetable.drop(['PlayId', 'GameId'], axis=1, inplace=True)
 scaled_basetable = scaler.transform(basetable)
 scaled_basetable = pd.DataFrame(scaled_basetable, index=basetable.index, columns=basetable.columns)
 y_pred = predict(scaled_basetable[feature_name])
 y_pred = np.clip(np.cumsum(y_pred, axis=1), 0, 1 ).tolist() [0]
preds_df = pd.DataFrame(data=[y_pred], columns=sample_prediction_df.columns)
 env.predict(preds_df)
 row_count += 1
env.write_submission_file()