Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<concatenate>
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from keras.callbacks.callbacks import EarlyStopping
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
pca_data = np.vstack(( pca_data.T, mnist_train_label)).T<prepare_output>
pd.options.display.max_columns = 50
pd.options.display.max_rows = 200
df_PCA = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_PCA.head()<prepare_x_and_y>
train_data = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv' )
mnist_train_data = np.array(mnist_train_data)
 mnist_train_label = np.array(mnist_train_label )<import_modules>
def is_offense(row):
if row.Team == 'home':
if row.HomeTeamAbbr == row.PossessionTeam:
return True
else:
return False
else:
if row.VisitorTeamAbbr == row.PossessionTeam:
return True
else:
return False
def avg_dist_between_teammates(play_team):
dist_mat = distance_matrix(play_team[['X','Y']], play_team[['X','Y']])
 np.fill_diagonal(dist_mat, np.nan)
 return np.nanmean(dist_mat)
 def off_vs_def_position_stats(row):
columns = 'X_mean X_std Y_mean Y_std'.split()
stats = {}
stats['id'] = row['PlayId']
if row.is_offense:
prefix = 'Offense_'
for i in columns:
stats[prefix+i] = row[i]
else:
prefix = 'Defense_'
for i in columns:
stats[prefix+i] = row[i]
return stats
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D
from tensorflow.keras.optimizers import Adadelta
from keras.utils.np_utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import LearningRateScheduler<categorify>
def data_prep(data):
data = data.copy()
data = data.drop('WindDirection WindSpeed Temperature Humidity \
GameWeather StadiumType Location Week PlayerCollegeName \
HomeScoreBeforePlay VisitorScoreBeforePlay YardLine JerseyNumber \
DisplayName Stadium TimeHandoff TimeSnap FieldPosition Quarter Season GameClock'.split() ,
axis = 1, errors = 'ignore')
 data['is_offense'] =(data.Team == 'home')==(data.HomeTeamAbbr == data.PossessionTeam)
 data['player_age'] = np.round(( pd.to_datetime('today')- pd.to_datetime(data.PlayerBirthDate)) /pd.Timedelta('1 y'))
data = data[['NflId', 'NflIdRusher'] + [i for i in list(data.columns)if i not in ['NflId', 'NflIdRusher']]]
pos_stats = data.groupby(['PlayId','Team'])[['X','Y']].agg([np.mean, np.std])
 pos_stats.columns = [*map(lambda x: '_'.join(x), pos_stats.columns.ravel())]
data = data.merge(pos_stats, left_on = 'PlayId Team'.split() , right_index = True)
 def_vs_off_stats = data.groupby(['Team', 'PlayId'])['X_mean X_std Y_mean Y_std'.split() ].max().unstack(0)
 def_vs_off_stats.columns = [*map(lambda x: '_'.join(x), def_vs_off_stats.columns.ravel())]
data = data.merge(def_vs_off_stats, left_on = 'PlayId', right_index = True)
 data = data.drop('PlayerHeight HomeTeamAbbr VisitorTeamAbbr is_offense Dir Orientation team_distance PlayerBirthDate PossessionTeam Team'.split() , axis = 1, errors = 'ignore')
 data = data.drop('X_mean X_std Y_mean Y_std'.split() , axis = 1)
 cat_vars = data.dtypes[data.dtypes == 'object'].index
data = pd.get_dummies(data, columns = cat_vars)
 data = data[data.NflIdRusher == data.NflId]
data = data.drop('NflId NflIdRusher GameId'.split() , axis = 1)
 data = data.set_index('PlayId')
 data = data.dropna()
return data
nclasses = mnist_train_label.max() - mnist_train_label.min() + 1
mnist_train_label = to_categorical(mnist_train_label, num_classes = nclasses)
 print("Shape of ytrain after encoding: ", mnist_train_label.shape )<choose_model_class>
def train_model(data):
data = data.copy()
data = data[data.Yards < 15]
X = data.drop(['Yards'], axis = 1)
 y = pd.get_dummies(data['Yards'])
 labels = list(y.columns)
 model = Sequential()
model.add(Dense(200, input_shape=(X.shape[1],), activation='sigmoid'))
model.add(Dense(200, activation='sigmoid'))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
 model.fit(X, y, epochs=20, batch_size=32, callbacks = [EarlyStopping('accuracy', patience = 2)])
 return model, labels
def build_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
def compile_model(model, optimizer='adam', loss='categorical_crossentropy'):
model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])
 def train_model(model, train, test, epochs, split):
history = model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)
 return history<train_model>
def make_prediction(test_df, sample_sub, model, labels):
test_df = test_df.copy()
preped_df = preped_train_data.drop('Yards',axis = 1 ).align(data_prep(test_df), axis = 1, join = 'left')[1].fillna(0)
 temp_results = pd.DataFrame(model.predict_proba(preped_df), columns = ['Yards'+str(i)for i in labels])
 sample_sub.update(temp_results)
 sample_sub = sample_sub.cumsum(axis = 1)
 sample_sub[sample_sub > 1] = 1
return sample_sub
cnn_model = build_model(( 28, 28, 1))
compile_model(cnn_model, 'adam', 'categorical_crossentropy')
 model_history = train_model(cnn_model, mnist_train_data, mnist_train_label, 80, 0.2 )<predict_on_test>
env = nflrush.make_env()
predictions = cnn_model.predict(mnist_test_arr )<define_variables>
print('Preping Data')
 preped_train_data = data_prep(train_data)
 print('Training')
 model, labels = train_model(preped_train_data)
<save_to_csv><EOS> 
print('Predicting')
 for(test_df, sample_prediction_df)in env.iter_test() :
sample_prediction_df = sample_prediction_df.replace(sample_prediction_df, 0 ).reset_index(drop=True)
 predictions_df = make_prediction(test_df, sample_prediction_df, model, labels)
 env.predict(predictions_df)
 print('Writing Submissions')
 env.write_submission_file()