Kaggle competition: NFL Big Data Bowl
class NeuralNet(nn.Module):
def __init__(self, embedding_matrix, num_aux_targets):
super(NeuralNet, self ).__init__()
embed_size = embedding_matrix.shape[1]
self.embedding = nn.Embedding(max_features, embed_size)
 self.embedding.weight = nn.Parameter(torch.tensor(embedding_matrix, dtype=torch.float32))
self.embedding.weight.requires_grad = False
self.embedding_dropout = SpatialDropout(0.3)
 self.lstm1 = nn.LSTM(embed_size, LSTM_UNITS, bidirectional=True, batch_first=True)
 self.lstm2 = nn.LSTM(LSTM_UNITS * 2, LSTM_UNITS, bidirectional=True, batch_first=True)
 self.linear1 = nn.Linear(DENSE_HIDDEN_UNITS, DENSE_HIDDEN_UNITS)
 self.linear2 = nn.Linear(DENSE_HIDDEN_UNITS, DENSE_HIDDEN_UNITS)
 self.linear_out = nn.Linear(DENSE_HIDDEN_UNITS, 1)
 self.linear_aux_out = nn.Linear(DENSE_HIDDEN_UNITS, num_aux_targets)
 def forward(self, x):
h_embedding = self.embedding(x)
 h_embedding = self.embedding_dropout(h_embedding)
 h_lstm1, _ = self.lstm1(h_embedding)
 h_lstm2, _ = self.lstm2(h_lstm1)
 avg_pool = torch.mean(h_lstm2, 1)
 max_pool, _ = torch.max(h_lstm2, 1)
 h_conc = torch.cat(( max_pool, avg_pool), 1)
 h_conc_linear1 = F.relu(self.linear1(h_conc))
h_conc_linear2 = F.relu(self.linear2(h_conc))
hidden = h_conc + h_conc_linear1 + h_conc_linear2
result = self.linear_out(hidden)
 aux_result = self.linear_aux_out(hidden)
 out = torch.cat([result, aux_result], 1)
 return out
def preprocess(data):

 punct = "/-'?!.,
def clean_special_chars(text, punct):
for p in punct:
text = text.replace(p, ' ')
 return text
data = data.astype(str ).apply(lambda x: clean_special_chars(x, punct))
return data<load_from_csv>
def extract_feature(play, is_train=True):
if play['PlayDirection'].iloc[0] == 'right':
direction = 1
else:
direction = -1
home, away = play['Team'].values == 'home', play['Team'].values == 'away'
indRusher = np.where(play['NflId'].values == play['NflIdRusher'].iloc[0])[0][0]
if play['FieldPosition'].iloc[0] == play['PossessionTeam'].iloc[0]:
yardToGoal = 100 - play['YardLine'].iloc[0]
start = np.array([120 +(play['YardLine'].iloc[0] + 10)* direction, 53.3 / 2])% 120
else:
yardToGoal = play['YardLine'].iloc[0]
start = np.array([120 -(play['YardLine'].iloc[0] + 10)* direction, 53.3 / 2])% 120
Dir = play['Dir'].values
rad = np.nan_to_num(2 * np.pi *(90 - Dir)/ 360)
 x, y = play['X'].values, play['Y'].values
S = play['S'].values * np.logical_not(np.isnan(Dir))
A = play['A'].values * np.logical_not(np.isnan(Dir))
loc = np.vstack([x - start[0], y - start[1]] ).T * direction
vel =(S * np.vstack([np.cos(rad), np.sin(rad)])).T * direction
acc =(A * np.vstack([np.cos(rad), np.sin(rad)])).T * direction
locRusher, velRusher, accRusher = loc[indRusher], vel[indRusher], acc[indRusher]
diff = np.hstack([np.square(loc - locRusher), np.square(vel - velRusher), np.square(acc - accRusher)])
 scrimWidth = 5
inTheBox =(play['NflId'].values != play['NflIdRusher'].iloc[0])*(np.abs(loc[:, 0])< scrimWidth)
 locDet = np.linalg.slogdet(np.exp(- np.square(loc[inTheBox, np.newaxis] - loc[inTheBox][np.newaxis] ).sum(2)/ 2.))[1]
locHomeDet = np.linalg.slogdet(np.exp(- np.square(loc[home * inTheBox][:, np.newaxis] - loc[home * inTheBox][np.newaxis] ).sum(2)/ 2.))[1]
locAwayDet = np.linalg.slogdet(np.exp(- np.square(loc[away * inTheBox][:, np.newaxis] - loc[away * inTheBox][np.newaxis] ).sum(2)/ 2.))[1]
if play['PossessionTeam'].iloc[0] == play['HomeTeamAbbr'].iloc[0]:
x = np.hstack([diff[home].sum(0), diff[away].sum(0), locDet, locHomeDet, locAwayDet])
 else:
x = np.hstack([diff[away].sum(0), diff[home].sum(0), locDet, locAwayDet, locHomeDet])
 x = np.hstack([locRusher, velRusher, accRusher, x,(downs == play['Down'].iloc[0] ).astype(np.float),(teams == play['PossessionTeam'].iloc[0] ).astype(np.float)])
 offset = locRusher[0] - 5
threshold = play['Distance'].iloc[0] - offset
if is_train:
yard = play['Yards'].iloc[0] - offset
c = yard < threshold
y = np.minimum(yard, threshold)
 return x, y, c, offset
else:
return x, offset
train = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv')
 test = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv')
 x_train = preprocess(train['comment_text'])
 y_aux_train = train[['target', 'severe_toxicity', 'obscene', 'identity_attack', 'insult', 'threat']]
x_test = preprocess(test['comment_text'] )<define_variables>
data = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', low_memory=False)
 data.loc[data.HomeTeamAbbr.values == "ARI", 'HomeTeamAbbr'] = "ARZ"
data.loc[data.HomeTeamAbbr.values == "BAL", 'HomeTeamAbbr'] = "BLT"
data.loc[data.HomeTeamAbbr.values == "CLE", 'HomeTeamAbbr'] = "CLV"
data.loc[data.HomeTeamAbbr.values == "HOU", 'HomeTeamAbbr'] = "HST"
data.loc[data['Season'] == 2017, 'S'] =(data['S'][data['Season'] == 2017] - 2.4355)/ 1.2930 * 1.4551 + 2.7570
downs = np.array([1, 2, 3])
 teams = pd.get_dummies(data['PossessionTeam'] ).columns[:-1]
train = data
n_train = train.shape[0] // 22
identity_columns = [
'male', 'female', 'homosexual_gay_or_lesbian', 'christian', 'jewish',
'muslim', 'black', 'white', 'psychiatric_or_mental_illness']<data_type_conversions>
inds = list(train.groupby('PlayId' ).groups.values())
 xs, ys, cs = [], [], []
for i in range(n_train):
ind = inds[i]
play = train.loc[ind]
x, y, c, _ = extract_feature(play)
 xs.append(x)
 ys.append(y)
 cs.append(c)
 xs, ys, cs = np.vstack(xs), np.hstack(ys), np.array(cs ).astype(np.int)
 ys = np.maximum(0, ys )
weights = np.ones(( len(x_train),)) / 4
weights +=(train[identity_columns].fillna(0 ).values>=0.5 ).sum(axis=1 ).astype(bool ).astype(np.int)/ 4
weights +=(((train['target'].values>=0.5 ).astype(bool ).astype(np.int)+ 
(train[identity_columns].fillna(0 ).values<0.5 ).sum(axis=1 ).astype(bool ).astype(np.int)) > 1 ).astype(bool ).astype(np.int)/ 4
weights +=(((train['target'].values<0.5 ).astype(bool ).astype(np.int)+ 
(train[identity_columns].fillna(0 ).values>=0.5 ).sum(axis=1 ).astype(bool ).astype(np.int)) > 1 ).astype(bool ).astype(np.int)/ 4
loss_weight = 1.0 / weights.mean()
y_train = np.vstack([(train['target'].values>=0.5 ).astype(np.int),weights] ).T
max_features = None
tokenizer = text.Tokenizer()
tokenizer.fit_on_texts(list(x_train)+ list(x_test))<string_transform>
model = sm.PHReg(ys, xs, cs)
 result = model.fit()
baseline_cum_hazard_func = result.baseline_cumulative_hazard_function[0]
pred_index = np.arange(-99, 100 )
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN )<concatenate>
env = nflrush.make_env()
embedding_matrix = np.concatenate([crawl_matrix, glove_matrix], axis=-1)
 embedding_matrix.shape
del crawl_matrix
del glove_matrix
gc.collect()<prepare_x_and_y>
for(play, prediction_df)in env.iter_test() :
play.loc[play.HomeTeamAbbr.values == "ARI", 'HomeTeamAbbr'] = "ARZ"
play.loc[play.HomeTeamAbbr.values == "BAL", 'HomeTeamAbbr'] = "BLT"
play.loc[play.HomeTeamAbbr.values == "CLE", 'HomeTeamAbbr'] = "CLV"
play.loc[play.HomeTeamAbbr.values == "HOU", 'HomeTeamAbbr'] = "HST"
x, offset = extract_feature(play, False)
 cum_hazard = np.exp(result.params.dot(x)) * baseline_cum_hazard_func(pred_index - offset)
 pred = 1 - np.exp(- cum_hazard)
 if play['FieldPosition'].iloc[0] == play['PossessionTeam'].iloc[0]:
yardToGoal = 100 - play['YardLine'].iloc[0]
else:
yardToGoal = play['YardLine'].iloc[0]
pred /= pred[pred_index <= yardToGoal][-1]
pred[pred_index > yardToGoal] = 1.
 prediction_df = pd.DataFrame(pred[np.newaxis], columns=prediction_df.columns)
 env.predict(prediction_df )
<choose_model_class><EOS> 
env.write_submission_file()