Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<categorify>
warnings.filterwarnings('ignore')
 tf.disable_v2_behavior()
__print__ = print
def print(string):
os.system(f'echo "{string}"')
 __print__(string )
def transform(image,label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 15.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* tf.random.normal([1],dtype='float32')
 m = get_mat(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 = K.dot(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,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3]),label<choose_model_class>
df = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', low_memory=False)
 shape = str(df.shape)
 print(df.columns)
 print(df.shape )
<create_dataframe>
def prepare_data(df):
df.loc[df['StadiumType'].isnull() , 'StadiumType'] = 'Unknown'
df.loc[df['GameWeather'].isnull() , 'GameWeather'] = 'Unknown'
df.loc[df['WindDirection'].isnull() , 'WindDirection'] = 'Unknown'
df.loc[df['WindSpeed'].isnull() , 'WindSpeed'] = '0'
df.loc[df['Orientation'].isnull() , 'Orientation'] = 0
df.loc[df['Dir'].isnull() , 'Dir'] = 0
df.loc[df['GameWeather']== 'T: 51; H: 55; W: NW 10 mph', 'Temperature'] = 51
df.loc[df['GameWeather']== 'T: 51; H: 55; W: NW 10 mph', 'WindDirection'] = 'NW 10 mph'
df.loc[df['GameWeather']== 'T: 51; H: 55; W: NW 10 mph', 'Humidity'] = 55
df.loc[df['Temperature'].isnull() , 'Temperature'] = 70
df.loc[df['Humidity'].isnull() , 'Humidity'] = 0
df.loc[df['OffenseFormation'].isnull() , 'OffenseFormation'] = 'Unknown'
df.loc[df['PossessionTeam'] == df['HomeTeamAbbr'] , 'DefendingTeam'] = df['VisitorTeamAbbr']
df.loc[df['PossessionTeam'] != df['HomeTeamAbbr'] , 'DefendingTeam'] = df['HomeTeamAbbr']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'right')&(df['X'] >= 50), 'FieldPosition'] = df['DefendingTeam']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'right')&(df['X'] < 50), 'FieldPosition'] = df['PossessionTeam']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'left')&(df['X'] >= 50), 'FieldPosition'] = df['PossessionTeam']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'left')&(df['X'] < 50), 'FieldPosition'] = df['DefendingTeam']
df = df.dropna()
df['WindSpeed'] = df['WindSpeed'].astype('str')
 df['WindSpeed'] = df['WindSpeed'].str.replace('mph', '')
 df['WindSpeed'] = df['WindSpeed'].str.replace('Mph', '')
 df['WindSpeed'] = df['WindSpeed'].str.replace('MPH', '')
 df['WindSpeed'] = df['WindSpeed'].str.replace('MPh', '')
 df['WindSpeed'] = df['WindSpeed'].str.strip()
b = df['WindSpeed'].loc[df['WindSpeed'].str.contains('-', regex=False)==True].unique()
b1 = df['WindSpeed'].loc[df['WindSpeed'].str.contains('-', regex=False)==True].str[:2]
b2 = df['WindSpeed'].loc[df['WindSpeed'].str.contains('-', regex=False)==True].str[-2:]
b3 =(b1.astype('float64')+ b2.astype('float64')) / 2
for i in b:
df.loc[(df.WindSpeed == i), 'WindSpeed'] = b3
v = df[df['WindSpeed'].str.isnumeric() == False]
av = v['WindSpeed'].unique()
for j in av:
df.loc[(df.WindSpeed == j), 'WindSpeed'] = '0.0'
df['WindSpeed'] = df['WindSpeed'].astype('float64')
 df.X[df['PlayDirection'] == 'left'] = 120 - df['X']
df.YardLine[df['PossessionTeam'] == df['FieldPosition']] = 10 + df['YardLine']
df.YardLine[df['PossessionTeam'] != df['FieldPosition']] = 60.0 +(50 - df['YardLine'])
 df.Orientation[df['PlayDirection'] == 'left'] = 360 - df['Orientation']
df.loc[(df['PlayDirection'] == 'left')&(df['Orientation'] == 0),'Orientation'] = 0
df.Orientation[df['PlayDirection'] == 'left'] = 360 - df['Dir']
df.loc[(df['PlayDirection'] == 'left')&(df['Dir'] == 0),'Dir'] = 0
nfl = {
1:	'ARI',
2:	'ATL',
3:	'BAL',
4:	'BUF',
5:	'CAR',
6:	'CHI',
7:	'CIN',
8:	'CLE',
9:	'DAL',
10:	'DEN',
11:	'DET',
12:	'GB',
13:	'HOU',
14:	'IND',
15:	'JAX',
16:	'KC',
17:	'LAC',
18:	'LAR',
19:	'MIA',
20:	'MIN',
21:	'NE',
22:	'NO',
23:	'NYG',
24:	'NYJ',
25:	'OAK',
26:	'PHI',
27:	'PIT',
28:	'SEA',
29:	'SF',
30:	'TB',
31:	'TEN',
32:	'WAS'
}
nfl_teams = pd.DataFrame.from_dict(nfl, orient='index')
 nfl_teams['team_id'] = nfl_teams.index
nfl_teams = nfl_teams.rename(columns={0: 'team_abb'})
 	
df.loc[df['HomeTeamAbbr'] == 'ARZ', 'HomeTeamAbbr'] = 'ARI'
df.loc[df['HomeTeamAbbr'] == 'BLT', 'HomeTeamAbbr'] = 'BAL'
df.loc[df['HomeTeamAbbr'] == 'CLV', 'HomeTeamAbbr'] = 'CLE'
df.loc[df['HomeTeamAbbr'] == 'HST', 'HomeTeamAbbr'] = 'HOU'
df.loc[df['HomeTeamAbbr'] == 'LA', 'HomeTeamAbbr'] = 'LAR'
df.loc[df['VisitorTeamAbbr'] == 'ARZ', 'VisitorTeamAbbr'] = 'ARI'
df.loc[df['VisitorTeamAbbr'] == 'BLT', 'VisitorTeamAbbr'] = 'BAL'
df.loc[df['VisitorTeamAbbr'] == 'CLV', 'VisitorTeamAbbr'] = 'CLE'
df.loc[df['VisitorTeamAbbr'] == 'HST', 'VisitorTeamAbbr'] = 'HOU'
df.loc[df['VisitorTeamAbbr'] == 'LA', 'VisitorTeamAbbr'] = 'LAR'
df = df.merge(nfl_teams, left_on ='HomeTeamAbbr', right_on ='team_abb', how = 'left')
 df = df.drop(['HomeTeamAbbr','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'HomeTeamAbbr'})
 df = df.merge(nfl_teams, left_on ='VisitorTeamAbbr', right_on ='team_abb', how = 'left')
 df = df.drop(['VisitorTeamAbbr','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'VisitorTeamAbbr'})
 df['HomeTeamAbbr'] = df['HomeTeamAbbr'].astype('float64')
 df['VisitorTeamAbbr'] = df['VisitorTeamAbbr'].astype('float64')
 	
df.loc[df['FieldPosition'] == 'ARZ', 'FieldPosition'] = 'ARI'
df.loc[df['FieldPosition'] == 'BLT', 'FieldPosition'] = 'BAL'
df.loc[df['FieldPosition'] == 'CLV', 'FieldPosition'] = 'CLE'
df.loc[df['FieldPosition'] == 'HST', 'FieldPosition'] = 'HOU'
df.loc[df['FieldPosition'] == 'LA', 'FieldPosition'] = 'LAR'
df.loc[df['PossessionTeam'] == 'ARZ', 'PossessionTeam'] = 'ARI'
df.loc[df['PossessionTeam'] == 'BLT', 'PossessionTeam'] = 'BAL'
df.loc[df['PossessionTeam'] == 'CLV', 'PossessionTeam'] = 'CLE'
df.loc[df['PossessionTeam'] == 'HST', 'PossessionTeam'] = 'HOU'
df.loc[df['PossessionTeam'] == 'LA', 'PossessionTeam'] = 'LAR'
df = df.merge(nfl_teams, left_on ='FieldPosition', right_on ='team_abb', how = 'left')
 df = df.drop(['FieldPosition','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'FieldPosition'})
 df = df.merge(nfl_teams, left_on ='PossessionTeam', right_on ='team_abb', how = 'left')
 df = df.drop(['PossessionTeam','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'PossessionTeam'})
 df['FieldPosition'] = df['FieldPosition'].astype('float64')
 df['PossessionTeam'] = df['PossessionTeam'].astype('float64')
 	
df['DefensePersonnel_DL'] = df['DefensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df['DefensePersonnel_LB'] = df['DefensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df['DefensePersonnel_DB'] = df['DefensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 df['DefensePersonnel_DL'] = df['DefensePersonnel_DL'].astype('int64')
 df['DefensePersonnel_LB'] = df['DefensePersonnel_LB'].astype('int64')
 df['DefensePersonnel_DB'] = df['DefensePersonnel_DB'].astype('int64')
 	
df.loc[df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(1, 3)== 'OL', 'OffensePersonnel_Flag'] = 1
df.loc[df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(1, 3)== 'QB', 'OffensePersonnel_Flag'] = 2
df.loc[df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(1, 3)== 'RB', 'OffensePersonnel_Flag'] = 0
	
df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_QB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_RB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_TE'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_WR'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(12, 13)
 df.loc[df['OffensePersonnel_Flag'] != 2, 'OffensePersonnel_QB'] = '1'
	
df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_OL'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_RB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_TE'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_WR'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(12, 13)
 	
df.loc[df['OffensePersonnel_Flag'] == 0, 'OffensePersonnel_RB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df.loc[df['OffensePersonnel_Flag'] == 0, 'OffensePersonnel_TE'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df.loc[df['OffensePersonnel_Flag'] == 0, 'OffensePersonnel_WR'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 	
df['OffensePersonnel_QB'] = df['OffensePersonnel_QB'].astype('int64')
 df['OffensePersonnel_RB'] = df['OffensePersonnel_RB'].astype('int64')
 df['OffensePersonnel_TE'] = df['OffensePersonnel_TE'].astype('int64')
 df['OffensePersonnel_WR'] = df['OffensePersonnel_WR'].astype('int64')
 df.loc[df['OffensePersonnel_Flag'] != 1, 'OffensePersonnel_OL_tmp'] = 11 -(df['OffensePersonnel_RB'] + df['OffensePersonnel_TE'] + df['OffensePersonnel_WR']+df['OffensePersonnel_QB'])
 df.loc[df['OffensePersonnel_Flag'] != 1, 'OffensePersonnel_OL'] = df['OffensePersonnel_OL_tmp']
df['OffensePersonnel_OL'] = df['OffensePersonnel_OL'].astype('int64')
 df = df.drop(['OffensePersonnel_OL_tmp'], axis=1)
 	
	
	
PlayerHeight_ft=df['PlayerHeight'].str.split('-' ).str[0].astype('float64')
 PlayerHeight_in=df['PlayerHeight'].str.split('-' ).str[1].astype('float64')
 PlayerHeight_m = PlayerHeight_ft*0.3048 + PlayerHeight_in*0.0254
df['PlayerHeight'] = PlayerHeight_m.astype('float64')
 	
time_snap_handoff = pd.to_datetime(df['TimeHandoff'])- pd.to_datetime(df['TimeSnap'])
 time_snap_handoff_sec = pd.to_timedelta(time_snap_handoff)
 df['TimeSnapHandoff'] = time_snap_handoff_sec.dt.seconds.astype('int64')
 df['GameYear'] = pd.DatetimeIndex(df['TimeSnap'] ).year
df['GameMonth'] = pd.DatetimeIndex(df['TimeSnap'] ).month
df['GameDay'] = pd.DatetimeIndex(df['TimeSnap'] ).day
today=dt.datetime.today().strftime('%Y-%m-%d')
 today=pd.to_datetime(today)
 PlayerAgeDays = today - pd.to_datetime(df['PlayerBirthDate'])
 df['PlayerAgeYears'] = PlayerAgeDays/np.timedelta64(1,'Y')
 GameClockMin = pd.DatetimeIndex(df['GameClock'] ).hour
GameClockSec = pd.DatetimeIndex(df['GameClock'] ).minute
df['GameClockSeconds'] = GameClockMin*60 + GameClockSec
columns_to_drop = ['GameClock','TimeSnap','TimeHandoff','PlayerBirthDate', 'JerseyNumber',
'OffensePersonnel', 'DefensePersonnel','PlayerCollegeName']
df = df.drop(columns_to_drop, axis=1)
 df.columns
return df
<choose_model_class>
def transform_data(df):
categorical_feature_mask = df.dtypes==object
categorical_cols = df.columns[categorical_feature_mask].tolist()
print(categorical_cols)
 le = LabelEncoder()
df[categorical_cols] = df[categorical_cols].apply(lambda col: le.fit_transform(col))
col_names = ['X', 'Y', 'S', 'A', 'Dis', 'Orientation', 'Dir', 'YardLine', 'Distance',
'HomeScoreBeforePlay', 'VisitorScoreBeforePlay',
'DefendersInTheBox', 'PlayerHeight', 'PlayerWeight', 'Temperature', 'Humidity', 'WindSpeed',
'DefensePersonnel_DL', 'DefensePersonnel_LB', 'DefensePersonnel_DB',
'OffensePersonnel_Flag', 'OffensePersonnel_QB', 'OffensePersonnel_RB',
'OffensePersonnel_TE', 'OffensePersonnel_WR', 'OffensePersonnel_OL',
'TimeSnapHandoff', 'PlayerAgeYears','GameClockSeconds']
features = df[col_names]
scaler = StandardScaler().fit(features.values)
 features = scaler.transform(features.values)
 df[col_names] = features
return df
with strategy.scope() :
enet = efn.EfficientNetB7(
 input_shape=(512, 512, 3),
weights='imagenet',
include_top=False 
)
 model = tf.keras.Sequential([
enet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax', dtype='float32')
 ])
 model.compile(
 optimizer=tf.keras.optimizers.Adam() ,
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 model.summary()<train_model>
dfa = prepare_data(df )
history = model.fit(
 get_training_dataset(do_aug=True),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback],
validation_data = None if SKIP_VALIDATION else get_validation_dataset()
)<train_model>
dfa_sc = transform_data(dfa )
history = model.fit(
 get_training_dataset(do_aug=True),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=5,
validation_data = None if SKIP_VALIDATION else get_validation_dataset()
)<choose_model_class>
feature_names = ['Team', 'X', 'Y', 'S', 'A', 'Dis', 'Orientation', 'Dir', 'NflId',
'Season', 'YardLine', 'Quarter', 'PossessionTeam', 'Down', 'Distance',
'FieldPosition', 'HomeScoreBeforePlay', 'VisitorScoreBeforePlay',
'NflIdRusher', 'OffenseFormation', 'DefendersInTheBox', 'PlayDirection',
'PlayerHeight', 'PlayerWeight', 'Position', 'HomeTeamAbbr',
'VisitorTeamAbbr', 'Week', 'Stadium', 'Location', 'StadiumType', 'Turf',
'GameWeather', 'Temperature', 'Humidity', 'WindSpeed', 'WindDirection',
'DefensePersonnel_DL', 'DefensePersonnel_LB', 'DefensePersonnel_DB',
'OffensePersonnel_Flag', 'OffensePersonnel_QB', 'OffensePersonnel_RB',
'OffensePersonnel_TE', 'OffensePersonnel_WR', 'OffensePersonnel_OL',
'TimeSnapHandoff', 'GameYear', 'GameMonth', 'GameDay', 'PlayerAgeYears',
'GameClockSeconds']
dfy = dfa_sc[['Yards']]
dfX = dfa_sc[feature_names]
yards = dfy.Yards
y = np.zeros(( yards.shape[0], 199))
for idx, target in enumerate(list(yards)) :
y[idx][99 + target] = 1
with strategy.scope() :
rnet = DenseNet201(
 input_shape=(512, 512, 3),
weights='imagenet',
include_top=False 
)
 model2 = tf.keras.Sequential([
rnet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax', dtype='float32')
 ])
 model2.compile(
 optimizer=tf.keras.optimizers.Adam() ,
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 model2.summary()<train_model>
X_train, X_test, y_train, y_test = train_test_split(dfX, y, test_size=0.33, random_state = 8)
 X_train.shape,y_train.shape, X_test.shape, y_test.shape
history2 = model2.fit(
 get_training_dataset(do_aug=True),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback],
validation_data=None if SKIP_VALIDATION else get_validation_dataset()
)<predict_on_test>
def crps(y_true, y_pred):
y_true = np.clip(np.cumsum(y_true, axis=1), 0, 1)
 y_pred = np.clip(np.cumsum(y_pred, axis=1), 0, 1)
 return(( y_true - y_pred)** 2 ).sum(axis=1 ).sum(axis=0)/(199 * y_true.shape[0] )
if not SKIP_VALIDATION:
cmdataset = get_validation_dataset(ordered=True)
 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()
m = model.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
cm_probabilities = alpha*m+(1-alpha)*m2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 scores.append(f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro'))
print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions)
 plt.plot(scores)
 best_alpha = np.argmax(scores)/100
cm_probabilities = best_alpha*m+(1-best_alpha)*m2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 else:
best_alpha = 0.44<compute_test_metric>
def build_model() :
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=[X_train.shape[1]]),
layers.BatchNormalization() ,
layers.Dense(64, activation='relu'),
layers.BatchNormalization() ,
layers.Dense(199, activation='softmax')
 ])
 model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
 return model
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')
 display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<save_to_csv>
model = build_model()
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = best_alpha*model.predict(test_images_ds)+(1-best_alpha)*model2.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='' )<install_modules>
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10 )
!pip install -q efficientnet<import_modules>
history = model.fit(X_train,
y_train,
epochs=40,
batch_size=512,
validation_data=(X_test, y_test),
verbose=1,
callbacks=[early_stop] )
print("Tensorflow version " + tf.__version__)
<set_options>
hist = pd.DataFrame(history.history)
 hist['epoch'] = history.epoch
hist.tail()
AUTO = tf.data.experimental.AUTOTUNE
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()
IMAGE_SIZE = [512, 512]
EPOCHS = 18
BATCH_SIZE = 16 * strategy.num_replicas_in_sync<init_hyperparams>
test_predictions = model.predict(X_test )
LR_START = 0.00001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.8
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
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 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]))<define_variables>
accuracy = crps(y_test, test_predictions)
 print(accuracy )
<categorify><EOS> 
env = nflrush.make_env()
for(test_df, sample_prediction_df)in env.iter_test() :
dfb = prepare_data(test_df)
 feature_names = ['Team', 'X', 'Y', 'S', 'A', 'Dis', 'Orientation', 'Dir', 'NflId',
'Season', 'YardLine', 'Quarter', 'PossessionTeam', 'Down', 'Distance',
'FieldPosition', 'HomeScoreBeforePlay', 'VisitorScoreBeforePlay',
'NflIdRusher', 'OffenseFormation', 'DefendersInTheBox', 'PlayDirection',
'PlayerHeight', 'PlayerWeight', 'Position', 'HomeTeamAbbr',
'VisitorTeamAbbr', 'Week', 'Stadium', 'Location', 'StadiumType', 'Turf',
'GameWeather', 'Temperature', 'Humidity', 'WindSpeed', 'WindDirection',
'DefensePersonnel_DL', 'DefensePersonnel_LB', 'DefensePersonnel_DB',
'OffensePersonnel_Flag', 'OffensePersonnel_QB', 'OffensePersonnel_RB',
'OffensePersonnel_TE', 'OffensePersonnel_WR', 'OffensePersonnel_OL',
'TimeSnapHandoff', 'GameYear', 'GameMonth', 'GameDay', 'PlayerAgeYears',
'GameClockSeconds']
dfb_sc = transform_data(dfb)
 dfb_sc_final = dfb[feature_names]
y_pred = np.mean([model.predict(dfb_sc_final)],axis=0)
 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)
 env.write_submission_file()