Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<load_pretrained>
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', 150)
 env = nflrush.make_env()
model_resnet.layers[2].get_weights() [0][0,0,0]<load_pretrained>
train = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', dtype={'WindSpeed': 'object'})
 outcomes = train[['GameId','PlayId','Yards']].drop_duplicates()
train_2 = train.copy()
train_3 = train.copy()
model_resnet.load_weights('.. /input/resnet50-4x256-max-1/model_1_resnet.h5')
 model_resnet.layers[2].get_weights() [0][0,0,0]<load_pretrained>
def strtoseconds(txt):
txt = txt.split(':')
 ans = int(txt[0])*60 + int(txt[1])+ int(txt[2])/60
return ans
def strtofloat(x):
try:
return float(x)
 except:
return -1
def map_weather(txt):
ans = 1
if pd.isna(txt):
return 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:
return -2*ans
if 'snow' in txt:
return -3*ans
return 0
def OffensePersonnelSplit(x):
dic = {'DB' : 0, 'DL' : 0, 'LB' : 0, 'OL' : 0, 'QB' : 0, 'RB' : 0, 'TE' : 0, 'WR' : 0}
for xx in x.split(","):
xxs = xx.split(" ")
 dic[xxs[-1]] = int(xxs[-2])
 return dic
def DefensePersonnelSplit(x):
dic = {'DB' : 0, 'DL' : 0, 'LB' : 0, 'OL' : 0}
for xx in x.split(","):
xxs = xx.split(" ")
 dic[xxs[-1]] = int(xxs[-2])
 return dic
def orientation_to_cat(x):
x = np.clip(x, 0, 360 - 1)
 try:
return str(int(x/15))
except:
return "nan"
def euclidean_distance(x1,y1,x2,y2):
x_diff =(x1-x2)**2
y_diff =(y1-y2)**2
return np.sqrt(x_diff + y_diff)
 def standardize_speed(df):
speed_std = np.where(df.Season == '2017',(( df['S'] - 2.4355)/ 1.2930),
np.where(df.Season == '2018',(( df['S'] - 2.7570)/ 1.4551), 
(( df['S'] - 2.7456)/ 1.4501)))
 return(speed_std)
model_classifier.layers[1].get_weights() [0][0]<load_pretrained>
def get_o_line_stats(df):
ol_df = df.loc[(df.Position.isin(['C','G','T','OG','OT','TE'])) &(df.IsOnOffense),
['PlayId','A','Dir_std_1','Dir_std_2','ToLeft', 'Orientation']]
ol_df['Dir_std'] = np.where(ol_df.ToLeft, ol_df['Dir_std_2'], ol_df['Dir_std_1'])
 ol_df['OlMidDir'] = abs(ol_df['Dir_std'] - 90)
 ol_df['Dir_cat'] = np.where(( ol_df.Dir_std < 150)&(ol_df.Dir_std > 30), 10,
np.where(ol_df.Dir_std > 150, 1,
np.where(( ol_df.Dir_std > 0)&(ol_df.Dir_std < 30), 1, -1)))
 ol_df['OlMovingDf'] = np.where(( ol_df.Dir_std < 150)&(ol_df.Dir_std > 30), 1, 0)
 ol_df['OlATimesDir'] = ol_df['A'] * ol_df['Dir_cat']
ol_df_grouped = ol_df.groupby('PlayId', as_index=False)[['A','OlATimesDir','OlMovingDf', 'OlMidDir', 'Orientation']].mean()
ol_df_grouped.rename(columns={'A':'OlAMean', 'Orientation': 'OlOrientationMean'}, inplace=True)
 return(ol_df_grouped)
 ol_agg_df = get_o_line_stats(standardized_dir_df )
model_classifier.load_weights('.. /input/resnet50-4x256-max-1/model_1_classifier.h5')
 model_classifier.layers[1].get_weights() [0][0]<categorify>
%time train_basetable = create_features(train, False )
THRESHOLD = 0.5
K_epsilon = K.epsilon()
def f1(y_true, y_pred):
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), THRESHOLD), K.floatx())
 tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def f1_loss(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1 )<compute_test_metric>
pos_dict= {'DefensePersonnel': ['DL','LB','DB'],
'OffensePersonnel': ['RB','TE','WR','QB','OL']}
def get_personnel(row, position):
filtered_row = [pos_group for pos_group in row if position in pos_group]
if len(filtered_row)> 0:
return(int(filtered_row[0][0]))
else:
return(0)
 def ball_carrier_features(df):
bc_df = df.loc[df['IsBallCarrier'], ['PlayId','ToLeft','X_std','Y_std','X_std_end','Y_std_end','Dir_std_1','Dir_std_2','Position', 'A']]
bc_df.rename(columns={'X_std': 'X_std_bc','Y_std':'Y_std_bc',
'X_std_end':'X_std_end_bc','Y_std_end':'Y_std_end_bc',
'Dir_std_1':'Dir_std_1_bc','Dir_std_2':'Dir_std_2_bc',
'Position':'BcPosition',
}, inplace=True)
 bc_df['BcRunRight'] = np.where(((bc_df.ToLeft)&(bc_df.Dir_std_2_bc > 90)) | 
(( bc_df.ToLeft == False)&(bc_df.Dir_std_1_bc > 90)) ,True, False)
 bc_df['BcDir'] = np.where(bc_df.ToLeft, bc_df.Dir_std_2_bc, bc_df.Dir_std_1_bc)
 bc_df['BcFast'] = np.where(( bc_df.BcPosition == 'WR')|(bc_df.BcPosition == 'CB'), 1, 0)
 bc_df['BcSlow'] = np.where(( bc_df.BcPosition == 'QB')|(bc_df.BcPosition == 'FB')| 
(bc_df.BcPosition == 'DE')|(bc_df.BcPosition == 'DT'), 1, 0)
 bc_df['BcDirCat'] = np.where(( bc_df.BcDir < 150)&(bc_df.BcDir > 30), 10,
np.where(bc_df.BcDir > 150, 1,
np.where(( bc_df.BcDir > 0)&(bc_df.BcDir < 30), 1, -1)))
 bc_df['BcMovingDf'] = np.where(( bc_df.BcDir < 150)&(bc_df.BcDir > 30), 1, 0)
 bc_df['BcATimesDir'] = bc_df['A'] * bc_df['BcDirCat']
bc_df.drop('A', axis= 1, inplace=True)
 return(bc_df)
 def calculate_defender_loc(df, ball_carrier_df):
def_df = df.loc[df['IsOnOffense']==False]
dir_df = def_df[['PlayId','DisplayName','JerseyNumber','X_std','Y_std']].merge(ball_carrier_df, on='PlayId')
 dir_df['DefenderTowardsPlayDir'] = np.where(((dir_df['BcRunRight'])&(dir_df['Y_std'] <= dir_df['Y_std_bc'])) | 
(( dir_df['BcRunRight'] == False)&(dir_df['Y_std'] >= dir_df['Y_std_bc'])) ,
1, 0)
 dir_agg_df = dir_df.groupby(['PlayId','X_std_bc','Y_std_bc','X_std_end_bc',
'Y_std_end_bc','BcRunRight','BcDir','BcFast','BcSlow',
'BcMovingDf','BcATimesDir',
], as_index=False)['DefenderTowardsPlayDir'].sum()
spread_x_df = dir_df.groupby('PlayId', as_index = False)['X_std'].agg({'DefenseMaxX': np.max,
'DefenseMinX': np.min})
 spread_y_df = dir_df.groupby('PlayId', as_index = False)['Y_std'].agg({'DefenseMaxY': np.max,
'DefenseMinY': np.min})
 spread_x_df['DefenseXSpread'] = spread_x_df.DefenseMaxX - spread_x_df.DefenseMinX
spread_y_df['DefenseYSpread'] = spread_y_df.DefenseMaxY - spread_y_df.DefenseMinY
dir_agg_df = dir_agg_df.merge(spread_x_df[['PlayId','DefenseXSpread']], on = 'PlayId' ).merge(spread_y_df[['PlayId','DefenseYSpread']], on ='PlayId')
 return(dir_agg_df)
 print('Creating some more features...')
 def more_feature_engineering(df, positions_schema):
df['GameClockAdj'] = df['GameClock'].apply(strtoseconds)
 for k, v in pos_dict.items() :
df[k + 'Split'] = df[k].str.split(', ')
 for pos in v:
df[pos + 'Count'] = df[k + 'Split'].apply(lambda row: get_personnel(row, pos))
df = df.drop(list(pos_dict.keys())+ [x + 'Split' for x in pos_dict.keys() ], axis = 1)
 df['Home'] = np.where(df['TeamOnOffense'] == 'home', 1, 0)
 df['Shotgun'] = np.where(df['OffenseFormation'] == 'SHOTGUN', 1, 0)
 df['TeamAbbrOnDefense'] = np.where(df['TeamOnOffense'] == 'home', df['VisitorTeamAbbr'], df['HomeTeamAbbr'])
 df['GameWeatherPro'] = df['GameWeather'].str.lower().apply(lambda x: "indoor" if not pd.isna(x)and "indoor" in x else x)
 df['GameWeatherPro'] = df['GameWeatherPro'].apply(lambda x: x.replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly')if not pd.isna(x)else x)
 df['GameWeatherPro'] = df['GameWeatherPro'].apply(lambda x: x.replace('clear and sunny', 'sunny and clear')if not pd.isna(x)else x)
 df['GameWeatherPro'] = df['GameWeatherPro'].apply(lambda x: x.replace('skies', '' ).replace("mostly", "" ).strip() if not pd.isna(x)else x)
 df['GameWeatherPro'] = df['GameWeatherPro'].apply(map_weather)
 df["DiffScoreBeforePlay"] = df["HomeScoreBeforePlay"] - df["VisitorScoreBeforePlay"]
df["DiffScoreBeforePlayBinary"] =(df["HomeScoreBeforePlay"] > df["VisitorScoreBeforePlay"] ).astype("object")
 df = df.drop(['GameWeather','Season','TimeHandoff','TimeSnap','PlayerBirthDate','Orientation','Dir','WindSpeed','GameClock'], axis =1)
 bc_df = ball_carrier_features(df)
 merged_bc_def_df = calculate_defender_loc(df, bc_df)
 final_fe_df = df.merge(merged_bc_def_df, on ='PlayId')
 final_fe_df['DistFromBc'] = np.sqrt(( final_fe_df.X_std-final_fe_df.X_std_bc)**2
+(final_fe_df.Y_std-final_fe_df.Y_std_bc)**2)
 final_fe_df['S'] = np.where(final_fe_df['S'] == 0,.1, final_fe_df['S'])
 final_fe_df['TimeToBc'] = final_fe_df['DistFromBc'] / final_fe_df['S']
final_fe_df['DistFromBcEnd'] = np.sqrt(( final_fe_df.X_std_end-final_fe_df.X_std_end_bc)**2
+(final_fe_df.Y_std_end-final_fe_df.Y_std_end_bc)**2)
 final_fe_df['TimeToBcEnd'] = final_fe_df['DistFromBcEnd'] / final_fe_df['S']
final_fe_df['Dir_std'] = np.where(final_fe_df.ToLeft, final_fe_df.Dir_std_2, final_fe_df.Dir_std_1)
 final_fe_df['DefendersInTheBox'] = final_fe_df['DefendersInTheBox'].fillna(int(final_fe_df.DefendersInTheBox.median()))
final_fe_df['DefendersInTheBox_vs_Distance'] = final_fe_df['DefendersInTheBox'] / final_fe_df['Distance']
final_fe_df['InsideRun'] = np.where(( final_fe_df['BcDir'] > 65)&(final_fe_df['BcDir'] < 115), 1, 0)
 final_fe_df['OffTackle'] = np.where(((final_fe_df['BcDir'] <= 65)&(final_fe_df['BcDir'] > 30)) | \ 
(( final_fe_df['BcDir'] >= 115)&(final_fe_df['BcDir'] < 150)) , 1, 0)
 return(final_fe_df)
 fe_df = more_feature_engineering(standardized_dir_df, pos_dict )
gamma = 2.0
epsilon = K.epsilon()
def focal_loss(y_true, y_pred):
pt = y_pred * y_true +(1-y_pred)*(1-y_true)
 pt = K.clip(pt, epsilon, 1-epsilon)
 CE = -K.log(pt)
 FL = K.pow(1-pt, gamma)* CE
loss = K.sum(FL, axis=1)
 return loss<choose_model_class>
fe_feature_lst = ['GameId','PlayId','GameClockAdj','Shotgun','DefenseXSpread','DefenseYSpread',
'X_std_bc','Y_std_bc','BcDir','InsideRun','OffTackle','TeamOnOffense','YardsFromOwnGoal',
'BcFast','BcSlow','BcMovingDf','BcATimesDir',
]
fe_feature_df = fe_df[fe_feature_lst].drop_duplicates()
class LearningRateReducer(Callback):
def __init__(self, schedule,
cycle_epoch=2, steps_per_epoch=100,
start_lr=0.001, end_lr=0.0001, verbose=0):
super(LearningRateReducer, self ).__init__()
self.schedule = schedule
self.steps_per_epoch = steps_per_epoch
self.cycle_epoch = cycle_epoch
self.start_lr = start_lr
self.end_lr = end_lr
self.verbose = verbose
def on_train_begin(self, epoch, logs=None):
self.lrf = {'lr': [], 'loss': []}
self.epoch = None
def on_epoch_begin(self, epoch, logs=None):
self.epoch = epoch
def on_batch_begin(self, batch, logs=None):
lr = self.schedule(self.epoch, batch,
cycle_epoch=self.cycle_epoch,
steps_per_epoch=self.steps_per_epoch,
start_lr=self.start_lr, end_lr=self.end_lr)
 K.set_value(self.model.optimizer.lr, lr)
 self.lrf['lr'].append(lr)
 return
def on_batch_end(self, batch, logs={}):
loss = logs.get('loss')
 self.lrf['loss'].append(loss )<train_model>
def get_bc_voronoi_area(df, point_type='current'):
df = df.sort_values(['IsOnOffense','PlayId','IsBallCarrier','Y_std'])
 bc_v_area_lst = []
if point_type == 'current':
X_col, Y_col = 'X_std', 'Y_std'
elif point_type == 'end':
X_col, Y_col = 'X_std_end', 'Y_std_end'
else:
print('Invalid point type')
 for play_id in df.PlayId.unique() :
try:
v_fe_df = df.loc[df.PlayId == play_id]
v_fe_df = v_fe_df.loc[(v_fe_df.IsOnOffense == False)|(v_fe_df.IsBallCarrier)]
points = np.c_[v_fe_df[X_col], v_fe_df[Y_col]]
vor = Voronoi(points)
 vertices = vor.vertices
regions = vor.regions
point_regions = vor.point_region
v_perm = [vertices[r] for r in regions[point_regions[-1]]]
rusher_x = v_fe_df.loc[v_fe_df.IsBallCarrier, X_col].values[0]
rusher_y = v_fe_df.loc[v_fe_df.IsBallCarrier, Y_col].values[0]
vor_df = pd.DataFrame(np.vstack(v_perm), columns = [X_col, Y_col])
 vor_df[X_col] = np.where(vor_df[X_col] < rusher_x, rusher_x,
np.where(vor_df[X_col] > rusher_x + 20, rusher_x + 20, vor_df[X_col]))
vor_df[Y_col] = np.where(vor_df[Y_col] <(rusher_y / 2), rusher_y / 2,
np.where(vor_df[Y_col] > rusher_y * 2, rusher_y * 2, vor_df[Y_col]))
vor_area = ConvexHull(vor_df, qhull_options = 'QJ' ).area
if vor_area > 1000:
print(play_id)
 bc_v_area_lst.append(vor_area)
 except:
print('Voronoi Error for play {}'.format(play_id))
bc_v_area_lst.append(42)
 return(bc_v_area_lst )
def lr_schedule(epoch, batch,
cycle_epoch=2, steps_per_epoch=100,
start_lr=0.002, end_lr=0.0002):
by =(np.log(start_lr)- np.log(end_lr)) /(steps_per_epoch*cycle_epoch-1)
 ii = divmod(epoch, cycle_epoch)[1]*steps_per_epoch + batch
lr = np.exp(np.log(start_lr)- ii * by)
 return lr<define_variables>
%time bc_v_area = get_bc_voronoi_area(fe_df, 'current' )
LEARNING_RATE = 0.001
print(datetime.datetime.now() - now )<choose_model_class>
bc_v_area_end = get_bc_voronoi_area(fe_df, 'end' )
models['model'].compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy', f1] )<train_model>
fe_feature_df['bc_v_area'] = bc_v_area
fe_feature_df['bc_v_area_end'] = bc_v_area_end
seq = Seq(train_labels, extend=False, aug=True, batch_size=8)
 print(len(seq))
lr_scheduler = LearningRateReducer(lr_schedule,
cycle_epoch=1, steps_per_epoch=len(seq),
start_lr=LEARNING_RATE, end_lr=LEARNING_RATE/10)
 callbacks = [lr_scheduler]
hst = models['model'].fit_generator(seq, epochs=1,
steps_per_epoch=len(seq),
callbacks=callbacks )<predict_on_test>
def standardize_dataset(train):
train['ToLeft'] = train.PlayDirection == "left"
train['IsBallCarrier'] = train.NflId == train.NflIdRusher
train['TeamOnOffense'] = "home"
train.loc[train.PossessionTeam != train.HomeTeamAbbr, 'TeamOnOffense'] = "away"
train['IsOnOffense'] = train.Team == train.TeamOnOffense
train['YardLine_std'] = 100 - train.YardLine
train.loc[train.FieldPosition.fillna('')== train.PossessionTeam,
'YardLine_std'
] = train.loc[train.FieldPosition.fillna('')== train.PossessionTeam,
'YardLine']
train['X_std'] = train.X
train.loc[train.ToLeft, 'X_std'] = 120 - train.loc[train.ToLeft, 'X']
train['Y_std'] = train.Y
train.loc[train.ToLeft, 'Y_std'] = 53.3 - train.loc[train.ToLeft, 'Y']
train['Orientation_std'] = train.Orientation
train.loc[train.ToLeft, 'Orientation_std'] = np.mod(180 + train.loc[train.ToLeft, 'Orientation_std'], 360)
 train['Dir_std'] = train.Dir
train.loc[train.ToLeft, 'Dir_std'] = np.mod(180 + train.loc[train.ToLeft, 'Dir_std'], 360)
 train.loc[train['Season'] == 2017, 'Orientation'] = np.mod(90 + train.loc[train['Season'] == 2017, 'Orientation'], 360)
 return train
dominance_df = standardize_dataset(train_3 )
seq = Seq(train_labels, extend=False, aug=False, batch_size=32)
 print(len(seq))
xs, ys = next(seq)
 print(xs['input'].shape)
 xs['input'].reshape(( 32,4,-1)).shape
y_pred = models['model_cnvt'].predict(xs['input'].reshape(( 32,4,-1)) [0])
 print(y_pred.shape )<concatenate>
def radius_calc(dist_to_ball):

 return 4 + 6 *(dist_to_ball >= 15)+(dist_to_ball ** 3)/ 560 *(dist_to_ball < 15)
 @np.vectorize
def pitch_control(x_point, y_point):

 offense_ids = my_play[my_play['IsOnOffense']].index
offense_control = compute_influence(x_point, y_point, offense_ids)
 offense_score = np.sum(offense_control)
 defense_ids = my_play[~my_play['IsOnOffense']].index
defense_control = compute_influence(x_point, y_point, defense_ids)
 defense_score = np.sum(defense_control)
 return expit(offense_score - defense_score)
 class Controller:

 def __init__(self, play):
self.play = play
self.vec_influence = np.vectorize(self.compute_influence)
 self.vec_control = np.vectorize(self.pitch_control)
 def compute_influence(self, x_point, y_point, player_id):

 point = np.array([x_point, y_point])
 player_row = self.play.loc[player_id]
theta = math.radians(player_row[56])
 speed = player_row[5]
player_coords = player_row[54:56].values
ball_coords = self.play[self.play['IsBallCarrier']].iloc[:, 54:56].values
dist_to_ball = euclidean(player_coords, ball_coords)
 S_ratio =(speed / 13)** 2
RADIUS = radius_calc(dist_to_ball)
 S_matrix = np.matrix([[RADIUS *(1 + S_ratio), 0], [0, RADIUS *(1 - S_ratio)]])
 R_matrix = np.matrix([[np.cos(theta), - np.sin(theta)], [np.sin(theta), np.cos(theta)]])
 COV_matrix = np.dot(np.dot(np.dot(R_matrix, S_matrix), S_matrix), np.linalg.inv(R_matrix))
norm_fact =(1 / 2 * np.pi)*(1 / np.sqrt(np.linalg.det(COV_matrix)))
 mu_play = player_coords + speed * np.array([np.cos(theta), np.sin(theta)])/ 2
intermed_scalar_player = np.dot(np.dot(( player_coords - mu_play),
np.linalg.inv(COV_matrix)) ,
np.transpose(( player_coords - mu_play)))
 player_influence = norm_fact * np.exp(- 0.5 * intermed_scalar_player[0, 0])
 intermed_scalar_point = np.dot(np.dot(( point - mu_play),
np.linalg.inv(COV_matrix)) ,
np.transpose(( point - mu_play)))
 point_influence = norm_fact * np.exp(- 0.5 * intermed_scalar_point[0, 0])
 return point_influence / player_influence
def pitch_control(self, x_point, y_point):

 offense_ids = self.play[self.play['IsOnOffense']].index
offense_control = self.vec_influence(x_point, y_point, offense_ids)
 offense_score = np.sum(offense_control)
 defense_ids = self.play[~self.play['IsOnOffense']].index
defense_control = self.vec_influence(x_point, y_point, defense_ids)
 defense_score = np.sum(defense_control)
 return expit(offense_score - defense_score)
 def display_control(self, grid_size=(30, 15), figsize=(11, 7)) :
front, behind = 15, 5
left, right = 20, 20
colorm = ['purple'] * 11 + ['orange'] * 11
colorm[np.where(self.play.Rusher.values)[0][0]] = 'black'
player_coords = self.play[self.play['Rusher']][['X_std', 'Y_std']].values[0]
X, Y = np.meshgrid(np.linspace(player_coords[0] - behind,
player_coords[0] + front,
grid_size[0]),
np.linspace(player_coords[1] - left,
player_coords[1] + right,
grid_size[1]))
infl = self.vec_control(X, Y)
 plt.figure(figsize=figsize)
 plt.contourf(X, Y, infl, 12, cmap='bwr')
 plt.scatter(self.play['X'].values, self.play['Y'].values, c=colorm)
 plt.title('Yards gained = {}, play_id = {}'.format(self.play['Yards'].values[0],
self.play['PlayId'].unique() [0]))
plt.show()
control_lst = []
for play_id in tqdm(dominance_df.PlayId.unique()):
my_play = dominance_df[dominance_df['PlayId']==play_id]
control = Controller(my_play)
 bc_coords = list(*my_play.loc[(my_play.IsBallCarrier), ['X_std','Y_std']].values)
 pitch_control = control.vec_control(*bc_coords)
 control_lst.append(pitch_control )
tmp = np.vstack(
 [
np.hstack([y_pred[0], y_pred[1]]),
np.hstack([y_pred[2], y_pred[3]])
 ])
 tmp.shape<prepare_x_and_y>
train_basetable['BcInfl'] = [np.asscalar(i)for i in control_lst]
Image.fromarray(np.uint8(( tmp+1)/2*255))<prepare_output>
def centeroidnp(arr):
length = arr.shape[0]
sum_x = np.sum(arr[:, 0])
 sum_y = np.sum(arr[:, 1])
 return(sum_x/length, sum_y/length)
 def get_defense_centroid(df):
centroid_lst = []
defense_df = df.loc[df.IsOnOffense == False]
for play_id in defense_df.PlayId.unique() :
def_points = defense_df.loc[defense_df.PlayId == play_id, ['X_std','Y_std']]
cp = centeroidnp(np.array(def_points))
centroid_lst.append(cp)
 return(centroid_lst)
 %time train_basetable['def_centroid'] = get_defense_centroid(fe_df)
 train_basetable['def_centroid_X_std'] = train_basetable.def_centroid.apply(lambda x: x[0])
 train_basetable['def_centroid_Y_std'] = train_basetable.def_centroid.apply(lambda x: x[1] )
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=32)
 len(seq_pred )<predict_on_test>
def get_qb_location(df):
qb_fe_df = df.loc[(df.Position == 'QB')].sort_values(['PlayId','S'])
 qb_fe_df['QbRank'] = qb_fe_df.groupby(by=['PlayId'])['S'].transform(lambda x: x.rank())
 qb_fe_df = qb_fe_df.loc[qb_fe_df['QbRank'] == 1, ['PlayId', 'X_std', 'Y_std']]
qb_fe_df.rename(columns={'X_std':'X_std_qb', 'Y_std': 'Y_std_qb'}, inplace=True)
 return(qb_fe_df)
 qb_location_df = get_qb_location(fe_df )
pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 pred.shape<compute_test_metric>
def get_defense_end(df):
def_df = df.loc[df.IsOnOffense==False]
def_df['DefYardsTowardsBc'] = def_df['DistFromBc'] - def_df['DistFromBcEnd']
def_dist_end_df = def_df.groupby('PlayId', as_index=False)['DistFromBcEnd'] \ 
.agg({'MinDistFromBcEnd':np.min,'MeanDistFromBcEnd':np.mean,'MedDistFromBcEnd':np.median,'StdDistFromBcEnd':np.std})
 def_toward_bc_df = def_df.groupby('PlayId', as_index = False)['DefYardsTowardsBc'] \ 
.agg({'SumDefYardsTowardsBc':sum, 'StdDefYardsTowardsBc':np.std, 'MaxDefYardsTowardsBc': np.max})
 return(def_dist_end_df.merge(def_toward_bc_df, on = 'PlayId'))
def_dist_end_df = get_defense_end(fe_df)
def calc_threshold(pred):
threshold_dic = {}
for idx in tqdm(range(28)) :
threshold_dic[idx] = 0.5
m = 0
for ii in range(100):
threshold0 = ii*0.01
f1_val = f1_score(y_cat_train_dic[idx], threshold0<(pred[:,idx]))
if m < f1_val:
threshold_dic[idx] = threshold0+0.005
m = f1_val
return threshold_dic<set_options>
def_time_to_spot_df = fe_df.loc[fe_df.IsOnOffense == False] \ 
.groupby('PlayId', as_index=False)[['TimeToBc','TimeToBcEnd']] \ 
.agg([np.min, np.mean, np.median, np.std])
 def_time_to_spot_df.columns = def_time_to_spot_df.columns.droplevel(0)
 def_time_to_spot_df.columns = ['TimeToBc' + c for c in def_time_to_spot_df.columns[0:4]] \
+ ['TimeToBcEnd' + c for c in def_time_to_spot_df.columns[4:]]
def_time_to_spot_df.reset_index(inplace=True )
warnings.filterwarnings('ignore')
 threshold_dic = calc_threshold(pred)
 threshold_dic<compute_test_metric>
fe_defenders = fe_df.loc[fe_df.IsOnOffense == False]
fe_defenders['DistFromBcRank'] = fe_defenders.groupby('PlayId')['DistFromBc'].transform(lambda x: x.rank())
 three_def_df = fe_defenders.loc[fe_defenders.DistFromBcRank <= 3]
grouped_closest_dist_df = three_def_df.groupby('PlayId', as_index=False)['DistFromBc'].mean()
grouped_closest_dist_df.rename(columns={'DistFromBc':'Top3DistFromBcMean'}, inplace=True )
for ii in range(28):
print(ii, f1_score(y_cat_train_dic[ii], threshold_dic[ii]<pred[:,ii]))<concatenate>
merged_basetable = train_basetable.merge(def_dist_end_df, on='PlayId')\ 
.merge(ol_agg_df, on = 'PlayId')\ 
.merge(fe_feature_df, on=['GameId','PlayId'])\ 
.merge(qb_location_df, how = 'left', on = 'PlayId')\ 
.merge(def_time_to_spot_df, how = 'left', on = 'PlayId')\ 
.merge(grouped_closest_dist_df, how = 'left', on = 'PlayId')
 merged_basetable['Shotgun_Down_Distance'] = merged_basetable['Shotgun'] * merged_basetable['Down'] * merged_basetable['Distance']
merged_basetable['BcDistFromDefCent'] = np.sqrt(( merged_basetable.X_std_bc - merged_basetable.def_centroid_X_std)**2
+(merged_basetable.Y_std_bc - merged_basetable.def_centroid_Y_std)**2)
 merged_basetable['BcDistFromQb'] = np.sqrt(( merged_basetable.X_std_bc - merged_basetable.X_std_qb)**2
+(merged_basetable.Y_std_bc - merged_basetable.Y_std_qb)**2)
 merged_basetable['BcDistFromQb'] = merged_basetable['BcDistFromQb'].fillna(merged_basetable.BcDistFromQb.mean())
 merged_basetable['BcDirFromMid'] = abs(merged_basetable['BcDir'] - 90 )
seq_test = Seq(test_labels, test=True, aug=False, batch_size=32)
 seq_test<predict_on_test>
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 pred_test.shape<save_to_csv>
merged_basetable['Quarter4'] = np.where(merged_basetable.Quarter == 4, 1, 0)
 merged_basetable['Down4'] = np.where(merged_basetable.Down == 4, 1, 0 )
def make_test(pred):
test_labels1 = test_labels.copy()
test_labels1['Predicted'] = [str(ee)for ee in np.argmax(pred, axis=1)]
print(test_labels1.head())
 test_labels2 = test_labels1.copy()
for ii in range(test_labels2.shape[0]):
threshold = list(zip(*sorted(list(threshold_dic.items()), key=lambda x:x[0], reverse=False)))[1]
idx = threshold < pred[ii,:]
tgt = test_labels2['Predicted'][ii]
tgt = [tgt] + [str(ee)for ee in np.arange(28)[idx]]
tgt = set(tgt)
 tgt = ' '.join(tgt)
 test_labels2['Predicted'][ii] = tgt
print(test_labels2.head())
 return test_labels1, test_labels2<categorify>
cols_to_drop = ['def_centroid','X','Y','X_std_qb','Y_std_qb','def_centroid_X_std','def_centroid_Y_std',
'Dir','TeamOnOffense','Quarter','Down','YardsFromOwnGoal',
]
merged_basetable.drop(cols_to_drop, axis = 1, inplace=True )
test_labels1_1, test_labels1_2 = make_test(pred_test )<save_to_csv>
def cleanabv(train):
train['ToLeft'] = train.PlayDirection == "left"
train['IsBallCarrier'] = train.NflId == train.NflIdRusher
train.loc[train.VisitorTeamAbbr == "ARI", 'VisitorTeamAbbr'] = "ARZ"
train.loc[train.HomeTeamAbbr == "ARI", 'HomeTeamAbbr'] = "ARZ"
train.loc[train.VisitorTeamAbbr == "BAL", 'VisitorTeamAbbr'] = "BLT"
train.loc[train.HomeTeamAbbr == "BAL", 'HomeTeamAbbr'] = "BLT"
train.loc[train.VisitorTeamAbbr == "CLE", 'VisitorTeamAbbr'] = "CLV"
train.loc[train.HomeTeamAbbr == "CLE", 'HomeTeamAbbr'] = "CLV"
train.loc[train.VisitorTeamAbbr == "HOU", 'VisitorTeamAbbr'] = "HST"
train.loc[train.HomeTeamAbbr == "HOU", 'HomeTeamAbbr'] = "HST"
train['Dir_std_1'] = np.where(( train['ToLeft'])&(train['Dir'] < 90), train['Dir'] + 360, train['Dir'])
 train['Dir_std_1'] = np.where(( train['ToLeft']==False)&(train['Dir'] > 270), train['Dir'] - 360, train['Dir_std_1'])
 train['Dir_std_2'] = np.where(train['ToLeft'], train['Dir_std_1'] - 180, train['Dir_std_1'])
 train['Dir_std'] = np.where(train.ToLeft, train.Dir_std_2, train.Dir_std_1)
 train.loc[train.DisplayName == "Bradley Sowell", "Position"] = "T"
return train
def getolinesix(play):
oline = play.loc[(play.loc[:,"Position"]=="T")|(play.loc[:,"Position"]=="G")|(play.loc[:,"Position"]=="C")|(play.loc[:,"Position"]=="OT")|(play.loc[:,"Position"]=="OG"),:]
oline = oline.sort_values(by = 'Y')
 oline = oline.reset_index()
firstoline = oline.loc[0,"level_0"]
lastoline = oline.loc[4,"level_0"]
onetwo = 0
if firstoline>10:
offense = play[11:22]
onetwo = 2
else:
offense = play[0:11]
onetwo = 1
offense = offense.sort_values(by = 'Y')
 offense = offense.reset_index()
ox1 = offense.loc[offense.loc[:,"level_0"]==firstoline,:]
ox1 = ox1.index
ox5 = offense.loc[offense.loc[:,"level_0"]==lastoline,:]
ox5 = ox5.index
if len(ox1)== 0:
ox1 = pd.Series([1])
 if len(ox5)== 0:
ox5 = pd.Series([10])
 lh = 0
li = -1
rh = 0
ri = -1
for i in range(ox1[0]-1,-1,-1):
tempx = offense.loc[i,"Y"]
tempy = offense.loc[i,"X"]
oxx = offense.loc[ox1,"Y"]
oxy = offense.loc[ox1,"X"]
tempd = distance(tempx,oxx,tempy,oxy)
 if tempd <= 4:
if offense.loc[i,"Position"]=="TE":
lh = 1
li = i
break
else:
lh = 0
li = -1
for i in range(ox5[0]+1,11,1):
tempx = offense.loc[i,"Y"]
tempy = offense.loc[i,"X"]
oxx = offense.loc[ox5,"Y"]
oxy = offense.loc[ox5,"X"]
tempd = distance(tempx,oxx,tempy,oxy)
 if tempd <= 4:
if offense.loc[i,"Position"]=="TE":
rh = 1
ri = i
break
else:
rh = 0
ri = -1
play.reset_index()
play.sort_values(by = 'Y')
 return oline,ri,rh,li,lh,offense
def encodepos(play,uniqueteams):
possession = np.zeros(22)
 for i in range(0,22):
for j in range(0,32):
if play.loc[i,"PossessionTeam"]==uniqueteams[j]:
possession[i]=j
return possession
def encodefpos(play,uniqueteams):
fposition = np.zeros(22)
 for i in range(0,22):
for j in range(0,32):
if play.loc[i,"FieldPosition"]==uniqueteams[j]:
fposition[i]=j
return fposition
def changedirection(play,pos,fpos):
ha = play.loc[play.loc[:,"NflId"]==play.loc[0,"NflIdRusher"],"Team"]
bc = np.where(play.loc[:,"NflId"]==play.loc[0,"NflIdRusher"])[0]
bcy = play.loc[bc,"X"]
bcy = bcy.reset_index()
if bc > 10:
meandy = stats.mean(play.loc[11:,'X'])
 else:
meandy = stats.mean(play.loc[0:10,'X'])
 postemp = pos[0]
fpostemp = fpos[0]
if bcy.loc[0,"X"]>meandy:
play.loc[:,"X"] = 100 - play.loc[:,"X"] + 20
else:
play.loc[:,"X"] = play.loc[:,"X"]
play.loc[:,"Y"] = 53.3 - play.loc[:,"Y"]
if postemp != fpostemp:
play.loc[:,"YardLine"] = 100 - play.loc[0,"YardLine"]
ydline = play.loc[0,"YardLine"]
return play, ha, ydline, bc
def distance(x1,x2,y1,y2):
dist = math.sqrt(( x1-x2)**2+(y1-y2)**2)
 return dist
def linear(x1,x2,y1,y2):
slope =(y1-y2)/(x1-x2)
 temp = slope*x1
b = y1-temp
return slope, b
def hmengagedextra(play,ha,ydline,bc):
oline,ri,rh,li,lh,offense = getolinesix(play)
 play = play.reset_index()
if rh == 1:
rol = offense.loc[[ri]]
oline = pd.concat([oline,rol])
 else:
temp = oline.loc[[4]]
temp.loc[4,"Y"]=oline.loc[4,"Y"]+2
oline = pd.concat([oline,temp])
 if lh == 1:
lol = offense.loc[[li]]
oline = pd.concat([lol,oline])
 else:
temp = oline.loc[[0]]
temp.loc[0,"Y"] = temp.loc[0,"Y"]-2
oline = pd.concat([temp,oline])
 olx = oline.loc[:,"Y"]
olx = olx.reset_index()
oly = oline.loc[:,"X"]
oly = oly.reset_index()
bcy = play.loc[bc,"X"]
bcy = bcy.reset_index()
if ha.all() == "away":
defense = play.loc[11:21,:]
else:
defense = play.loc[0:10,:]
defense = defense.drop(columns="level_0")
 defense = defense.reset_index()
olnum = np.zeros(6)
 olnumeng = np.zeros(6)
 numdefbf = np.zeros(6)
 for i in range(0,11):
dx = defense.loc[i,"Y"]
dy = defense.loc[i,"X"]
for j in range(0,6):
olx1 = olx.loc[j,"Y"]
olx2 = olx.loc[j+1,"Y"]
oly1 = oly.loc[j,"X"]
oly2 = oly.loc[j+1,"X"]
if(dx > olx1)&(dx <= olx2):
dist1 = distance(olx1,dx,oly1,dy)
 dist2 = distance(olx2,dx,oly2,dy)
 if(dy < ydline+15)&(dy > bcy.loc[0,"X"]):
slope,b = linear(olx1,olx2,oly1,oly2)
 tempy = dx*slope+b
if dy<=tempy:
numdefbf[j] = numdefbf[j] + 1
if dist1 <=1.5 or dist2 <=1.5:
olnumeng[j] = olnumeng[j]+1
olnum[j] = olnum[j]+1
return olnum,olnumeng,numdefbf,oline
train = pd.read_csv(".. /input/nfl-big-data-bowl-2020/train.csv", low_memory=False)
 train = cleanabv(train)
 uniqueplays = np.unique(train.loc[:,"PlayId"])
 uniqueteams = np.unique(train.loc[:,"PossessionTeam"] )
test_labels1_2.to_csv('InceptionResNetV1_2.csv', index=False )<categorify>
holes_spread = []
for i in tqdm(range(0, len(uniqueplays))):
play = train.loc[train.loc[:,"PlayId"]==uniqueplays[i],:]
play = play.reset_index()
playid = play.PlayId[0]
pos = encodepos(play,uniqueteams)
 fpos = encodefpos(play,uniqueteams)
 play,ha,ydline,bc = changedirection(play,pos,fpos)
 olnum,olnumeng,numdefbf,oline = hmengagedextra(play,ha,ydline,bc)
 bc_dir = play.loc[play.IsBallCarrier, 'Dir_std'].values[0]
oline_y = oline.Y
holes = np.diff(oline_y)
 holes_spread.append(np.mean(holes))
if i == 0:
olnums = olnum
olnumseng = olnumeng
numdefsbf = numdefbf
else:
olnums = np.vstack(( olnums,olnum))
olnumseng = np.vstack(( olnumseng,olnumeng))
numdefsbf = np.vstack(( numdefsbf,numdefbf))
No = 1
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))<save_to_csv>
los_df = outcomes.copy().drop('Yards', axis = 1)
 los_df['olnums'] = [sum(i)for i in olnums]
los_df['olnumseng'] = [sum(i)for i in olnumseng]
los_df['numdefsbf'] = [sum(i)for i in numdefsbf]
los_df['avgholesize'] = holes_spread
df_pred1 = pd.DataFrame(pred)
 df_pred1.columns = ['cls'+ str(ii)for ii in range(28)]
df_pred1 = pd.concat([train_labels.copy() , df_pred1], axis=1)
 df_pred1.head()
df_pred1.to_csv('proba_{}.csv'.format(No), index=False )<save_to_csv>
merged_basetable = merged_basetable.merge(los_df, on=['GameId', 'PlayId'])
 merged_basetable['numdefsb_cube'] = merged_basetable['numdefsbf'] ** 3
merged_basetable['numdefsbf_cube_div_min_dist'] = merged_basetable['numdefsb_cube'] / merged_basetable['def_min_dist']
df_pred1_test = pd.DataFrame(pred_test)
 df_pred1_test.columns = ['cls'+ str(ii)for ii in range(28)]
df_pred1_test = pd.concat([test_labels.copy() , df_pred1_test], axis=1)
 df_pred1_test.head()
df_pred1_test.to_csv('proba_test_{}.csv'.format(No), index=False )<set_options>
merged_basetable['BcInfl'] = merged_basetable['BcInfl'].fillna (.8)
 merged_basetable['v_infl'] =(merged_basetable['BcInfl'] * merged_basetable['bc_v_area_end'] ).fillna(24 )
warnings.filterwarnings("ignore" )<load_from_csv>
lower_bound = merged_basetable.Yards.quantile(0.002)
 upper_bound = merged_basetable.Yards.quantile(0.998)
 merged_basetable = merged_basetable.loc[(merged_basetable.Yards > lower_bound)&(merged_basetable.Yards < upper_bound)]
path_to_train = '.. /input/train/'
data = pd.read_csv('.. /input/train.csv')
 train_dataset_info = []
for name, labels in zip(data['Id'], data['Target'].str.split(' ')) :
train_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label)for label in labels])})
 train_dataset_info = np.array(train_dataset_info )<train_model>
cluster_features = ['back_from_scrimmage','S','A','Shotgun','BcDistFromQb','InsideRun','OffTackle','TimeDelta','BcDirFromMid',]
cluster_df = merged_basetable[cluster_features]
class data_generator:
def create_train(dataset_info, batch_size, shape, augument=True):
while True:
random_indexes = np.random.choice(len(dataset_info), batch_size)
 batch_images = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_labels = np.zeros(( batch_size, 28))
for i, idx in enumerate(random_indexes):
image = data_generator.load_image(
 dataset_info[idx]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images[i] = image
batch_labels[i][dataset_info[idx]['labels']] = 1
yield batch_images, batch_labels
def load_image(path, shape):
image_red_ch = skimage.io.imread(path+'_red.png')/255.0
image_yellow_ch = skimage.io.imread(path+'_yellow.png')/255.0
image_green_ch = skimage.io.imread(path+'_green.png')/255.0
image_blue_ch = skimage.io.imread(path+'_blue.png')/255.0
image_red_ch +=(image_yellow_ch/2 ).astype(np.uint8)
 image_blue_ch +=(image_yellow_ch/2 ).astype(np.uint8)
 image = np.stack((
image_red_ch,
image_green_ch,
image_blue_ch 
), -1)
 image = resize(image,(shape[0], shape[1]), mode='reflect')
 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<train_model>
cluster_fit = fit_clusters(data_transformed, 5)
 cluster_labels = cluster_fit.predict(data_transformed)
 merged_basetable['RunCluster'] = cluster_labels
input_shape =(256,256,3)
 train_datagen = data_generator.create_train(
 train_dataset_info, 5, input_shape, augument=True )<compute_test_metric>
X = merged_basetable.copy()
yards = X.Yards
y = np.zeros(( yards.shape[0], 199))
for idx, target in enumerate(list(yards)) :
y[idx][99 + target] = 1
X.drop(['GameId','PlayId','Yards'], axis=1, inplace=True)
 scaler = StandardScaler()
binary_features = ['back_oriented_down_field', 'back_moving_down_field', 'Shotgun', 'InsideRun', 'OffTackle',
'BcFast','BcSlow','BcMovingDf','Quarter4', 'Down4','RunCluster',
]
binary_array = np.array(X.loc[:, binary_features])
 numeric_df = X.drop(binary_features, axis = 1)
 numeric_scaled = scaler.fit_transform(numeric_df)
 X_transformed = np.hstack(( numeric_scaled, binary_array))
X_train, X_val, y_train, y_val = train_test_split(X_transformed, y, test_size=0.25, random_state=12345)
 print(X_train.shape, X_val.shape)
 print(y_train.shape, y_val.shape )
def f1(y_true, y_pred):

 def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))<compute_test_metric>
def get_rf(x_tr, y_tr, x_val, y_val, shape):
model = RandomForestRegressor(bootstrap=False, max_features=0.3, min_samples_leaf=15,
min_samples_split=8, n_estimators=25, n_jobs=-1, random_state=42)
 model.fit(x_tr, y_tr)
 y_pred = model.predict(x_val)
 y_valid = y_val
crps = crps_score(y_pred, y_valid, shape=shape)
 return model, crps, y_pred
def crps_score(y_prediction, y_valid, shape=X.shape[0]):
y_true = np.clip(np.cumsum(y_valid, axis=1), 0, 1)
 y_pred = np.clip(np.cumsum(y_prediction, axis=1), 0, 1)
 val_s =(( y_true - y_pred)** 2 ).sum(axis=1 ).sum(axis=0)/(199 * shape)
 crps = np.round(val_s, 6)
 return crps
rf, crps_rf, y_pred = get_rf(X_train, y_train, X_val, y_val, shape=X_val.shape[0])
 print("the crps(RF)is %f"%(crps_rf))
def f1_loss(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1 )<choose_model_class>
pd.DataFrame(list(zip(X.columns, rf.feature_importances_)) ,
columns = ['Feature', 'importances'] ).sort_values('importances', ascending = False )
def reduce(x):
return K.argmax(x, axis=1)
 def cast(x):
return K.cast(x, 'float32')
 def create_model(input_shape, n_out):
inp = Input(input_shape)
 pretrain_model = MobileNetV2(include_top=False, weights=None, input_tensor=inp)
 x = pretrain_model.output
x = GlobalAveragePooling2D()(x)
 x = Dropout(0.5 )(x)
 x = Dense(n_out, activation="relu" )(x)
 for layer in pretrain_model.layers:
layer.trainable = True
return Model(inp, x )<choose_model_class>
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
epochs = 5; batch_size = 64
checkpointer = ModelCheckpoint('.. /working/InceptionResNetV2.model', verbose=2, save_best_only=True)
 early_stopping = EarlyStopping(monitor='val_loss', patience=2)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=1, factor=0.1)
 np.random.seed(2018)
 indexes = np.arange(train_dataset_info.shape[0])
 np.random.shuffle(indexes)
 train_indexes = indexes[:27500]
valid_indexes = indexes[27500:]
train_steps = len(train_indexes)//batch_size
valid_steps = len(valid_indexes)//batch_size
train_generator = data_generator.create_train(train_dataset_info[train_indexes], batch_size, input_shape, augument=True)
 validation_generator = data_generator.create_train(train_dataset_info[valid_indexes], 100, input_shape, augument=False)
 history = model.fit_generator(
 train_generator,
steps_per_epoch=train_steps,
validation_data=next(validation_generator),
validation_steps=valid_steps,
epochs=epochs,
verbose=1,
callbacks=[checkpointer, reduce_lr] )<load_from_csv>
def get_model(x_tr,y_tr,x_val,y_val):
inp = Input(shape =(x_tr.shape[1],))
x = Dense(1024, input_dim=X.shape[1], activation='relu' )(inp)
 x = Dropout(0.5 )(x)
 x = BatchNormalization()(x)
 x = Dense(512, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 x = BatchNormalization()(x)
 x = Dense(256, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 x = BatchNormalization()(x)
 out = Dense(199, activation='softmax' )(x)
 model = Model(inp,out)
 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[])
 es = EarlyStopping(monitor='CRPS_score_val',
mode='min',
restore_best_weights=True,
verbose=1,
patience=15)
 mc = ModelCheckpoint('best_model.h5',monitor='CRPS_score_val',mode='min',
save_best_only=True, verbose=1, save_weights_only=True)
 bsz = 1024
steps = x_tr.shape[0]/bsz
model.fit(x_tr, y_tr,callbacks=[CRPSCallback(validation =(x_val,y_val)) ,es,mc], epochs=20, batch_size=bsz,verbose=0)
 model.load_weights("best_model.h5")
 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, y_true, y_pred
submit = pd.read_csv('.. /input/sample_submission.csv')
 model = load_model(".. /working/InceptionResNetV2.model" )<predict_on_test>
losses = []
models = []
crps_csv = []
s_time = time.time()
for k in range(1):
kfold = KFold(5, random_state = 42 + k, shuffle = True)
 for k_fold,(tr_inds, val_inds)in enumerate(kfold.split(yards)) :
print("-----------")
 print("-----------")
 tr_x,tr_y = X_transformed[tr_inds],y[tr_inds]
val_x,val_y = X_transformed[val_inds],y[val_inds]
model,crps,y_true,y_preds = 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))
%%time
predicted = []
for name in tqdm(submit['Id']):
path = os.path.join('.. /input/test/', name)
 image = data_generator.load_image(path, input_shape)
 score_predict = model.predict(image[np.newaxis])[0]
label_predict = np.arange(28)[score_predict>=0.5]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label )<save_to_csv>
print('mean crps is {} and std is {}'.format(np.mean(crps_csv), np.std(crps_csv)) )
submit['Predicted'] = predicted
submit.to_csv('submission.csv', index=False )<set_options>
def nn_predict(x_te):
model_num = len(models)
 for k,m in enumerate(models):
if k==0:
y_pred = m.predict(x_te,batch_size=1024)
 else:
y_pred+=m.predict(x_te,batch_size=1024)
 y_pred = y_pred / model_num
return y_pred
warnings.filterwarnings("ignore")
 SIZE = 512<load_from_csv>
iter_test = env.iter_test()
path_to_train = '.. /input/human-protein-atlas-image-classification/train/'
data = pd.read_csv('.. /input/human-protein-atlas-image-classification/train.csv')
 train_dataset_info = []
for name, labels in zip(data['Id'], data['Target'].str.split(' ')) :
train_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label)for label in labels])})
 train_dataset_info = np.array(train_dataset_info )<train_model>
%%time
test_x_lst = []
test_df_2 = test_df.copy()
test_df_3 = test_df.copy()
test_df_4 = test_df.copy()
test_basetable = create_features(test_df, deploy=True)
 test_adjusted_df = adjust_players_and_teams(test_df_2, player_schema, team_schema)
 test_standardized_coor_df = standardize_coordinates(test_adjusted_df)
 test_standardized_dir_df = standardize_direction(test_standardized_coor_df)
 test_ol_agg_df = get_o_line_stats(test_standardized_dir_df)
 test_merged_basetable = test_basetable.merge(test_ol_agg_df, on='PlayId')
 test_fe_df = more_feature_engineering(test_standardized_dir_df, pos_dict)
 test_fe_feature_df = test_fe_df[fe_feature_lst].drop_duplicates()
test_bc_v_area = get_bc_voronoi_area(test_fe_df, 'current')
 test_bc_v_area_end = get_bc_voronoi_area(test_fe_df, 'end')
 test_fe_feature_df['bc_v_area'] = test_bc_v_area[0]
test_fe_feature_df['bc_v_area_end'] = test_bc_v_area_end[0]
test_def_dist_end_df = get_defense_end(test_fe_df)
 test_fe_df[['X_std_end','Y_std_end']] = test_fe_df[['X_std_end','Y_std_end']].fillna(25)
 test_qb_location_df = get_qb_location(test_fe_df)
 test_def_time_to_spot_df = test_fe_df.loc[test_fe_df.IsOnOffense == False] \ 
.groupby('PlayId', as_index=False)[['TimeToBc','TimeToBcEnd']] \ 
.agg([np.min, np.mean, np.median, np.std])
 test_def_time_to_spot_df.columns = test_def_time_to_spot_df.columns.droplevel(0)
 test_def_time_to_spot_df.columns = ['TimeToBc' + c for c in test_def_time_to_spot_df.columns[0:4]] \
+ ['TimeToBcEnd' + c for c in test_def_time_to_spot_df.columns[4:]]
test_def_time_to_spot_df.reset_index(inplace=True)
 test_fe_defenders = test_fe_df.loc[test_fe_df.IsOnOffense == False]
test_fe_defenders['DistFromBcRank'] = test_fe_defenders.groupby('PlayId')['DistFromBc'].transform(lambda x: x.rank())
 test_three_def_df = test_fe_defenders.loc[test_fe_defenders.DistFromBcRank <= 3]
test_grouped_closest_dist_df = test_three_def_df.groupby('PlayId', as_index=False)['DistFromBc'].mean()
test_grouped_closest_dist_df.rename(columns={'DistFromBc':'Top3DistFromBcMean'}, inplace=True)
 test_merged_basetable = test_merged_basetable.merge(test_def_dist_end_df, how = 'left', on='PlayId')\ 
.merge(test_fe_feature_df, how = 'left', on=['GameId','PlayId'])\ 
.merge(test_qb_location_df, how = 'left', on = 'PlayId')\ 
.merge(test_def_time_to_spot_df, how = 'left', on = 'PlayId')\ 
.merge(test_grouped_closest_dist_df, how = 'left', on = 'PlayId' ).fillna(25)
 test_merged_basetable['Shotgun_Down_Distance'] = test_merged_basetable['Shotgun'] * test_merged_basetable['Down'] * test_merged_basetable['Distance']
test_merged_basetable['def_centroid'] = get_defense_centroid(test_fe_df)
 test_merged_basetable['def_centroid_X_std'] = test_merged_basetable.def_centroid.apply(lambda x: x[0])
 test_merged_basetable['def_centroid_Y_std'] = test_merged_basetable.def_centroid.apply(lambda x: x[1])
 test_merged_basetable['BcDistFromDefCent'] = np.sqrt(( test_merged_basetable.X_std_bc - test_merged_basetable.def_centroid_X_std)**2
+(test_merged_basetable.Y_std_bc - test_merged_basetable.def_centroid_Y_std)**2)
 test_merged_basetable['BcDistFromQb'] = np.sqrt(( test_merged_basetable.X_std_bc - test_merged_basetable.X_std_qb)**2
+(test_merged_basetable.Y_std_bc - test_merged_basetable.Y_std_qb)**2)
 test_merged_basetable['BcDirFromMid'] = abs(test_merged_basetable['BcDir'] - 90)
 test_merged_basetable['Down4'] = np.where(test_merged_basetable.Down == 4, 1, 0)
 test_merged_basetable['Quarter4'] = np.where(test_merged_basetable.Quarter == 4, 1, 0)
 test_merged_basetable.drop(['GameId','PlayId'] + cols_to_drop, axis=1, inplace=True)
 test_cluster_df = test_merged_basetable[cluster_features]
test_data_transformed = scale_data(test_cluster_df)
 test_merged_basetable['RunCluster'] = cluster_fit.predict(test_data_transformed)
 test_merged_basetable = test_merged_basetable[list(merged_basetable.drop(['GameId','PlayId','Yards'], axis=1 ).columns)].fillna(1)
 test_x_lst.append(test_merged_basetable)
 test_binary_array = np.array(test_merged_basetable.loc[:, binary_features])
 test_numeric_df = test_merged_basetable.drop(binary_features, axis = 1)
 test_numeric_scaled = scaler.transform(test_numeric_df)
 test_scaled_basetable = np.hstack(( test_numeric_scaled, test_binary_array))
nn_y_pred = nn_predict(test_scaled_basetable)
 nn_y_pred = np.clip(np.cumsum(nn_y_pred, axis=1), 0, 1 ).tolist() [0]
rf_y_pred = rf.predict(test_scaled_basetable)
 rf_y_pred = np.clip(np.cumsum(rf_y_pred, axis=1), 0, 1)
 blended_y_pred =(nn_y_pred + rf_y_pred)/ 2
preds_df = pd.DataFrame(data=[nn_y_pred], columns=sample_prediction_df.columns)
 env.predict(preds_df)