Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<import_modules>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime, tqdm
from kaggle.competitions import nflrush
from sklearn.model_selection import KFold, RepeatedKFold
import matplotlib.pyplot as plt
from collections import Counter<load_from_csv>
env = nflrush.make_env()
fg_unet = pd.read_csv("/kaggle/input/mnist-w-fgunet-output/submission.csv")
 vgg = pd.read_csv("/kaggle/input/mnist-w-vgg16-output/submission.csv")
 resnet = pd.read_csv("/kaggle/input/mnist-w-resnet-output/submission.csv" )<load_from_csv>
train = pd.read_csv('.. /input/nfl-big-data-bowl-2020/train.csv', dtype={'WindSpeed': 'object'})
 train.head()
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 sub = pd.read_csv("/kaggle/input/mnist-w-resnet-output/submission.csv" )<categorify>
train.drop(['Season', 'DefensePersonnel', 'Temperature', 'Humidity'], axis = 1, inplace = True )
majority = sub.copy()
no_maj = []
unequal = []
for i in range(len(vgg)) :
lst = [fg_unet.iloc[i].Label, vgg.iloc[i].Label, resnet.iloc[i].Label]
if not all(ele == lst[0] for ele in lst):
unequal.append(i)
 count = Counter(lst ).most_common()
if len(count)==len(lst):
no_maj.append(i)
 majority.iloc[i].Label = lst[-1]
img = test.iloc[i,:].values
img = img.reshape(28, 28)
 print(i, lst)
 plt.imshow(img)
 plt.show()
else:
majority.iloc[i].Label = count[0][0]
else:
majority.iloc[i].Label = lst[0]
print("Number of rows not all equal: ", len(unequal))
print("Number of rows with no majority: ", len(no_maj))
majority.to_csv("submission_maj.csv", index=False )<import_modules>
off_form = train['OffenseFormation'].unique()
train = pd.concat([train.drop(['OffenseFormation'], axis=1), pd.get_dummies(train['OffenseFormation'], prefix='Formation')], axis=1)
 dummy_col = train.columns
from keras.models import load_model
from keras.utils import to_categorical<load_pretrained>
def fe(train):
train['DefendersInTheBox_vs_Distance'] = train['DefendersInTheBox'] / train['Distance']
def strtoseconds(txt):
txt = txt.split(':')
 return int(txt[0])*60 + int(txt[1])+ int(txt[2])/60
train['JerseyNumber_grouped'] = train['JerseyNumber'] // 10
train['GameClock'] = train['GameClock'].apply(strtoseconds)
 train['Height_feet'] = train['PlayerHeight'].apply(lambda x: int(x.split('-')[0]))
train['PlayerHeight'] = train['PlayerHeight'].apply(lambda x: 12*int(x.split('-')[0])+int(x.split('-')[1]))
train['BMI'] =(train['PlayerWeight'] * 703)/(train['PlayerHeight'] ** 2)
 train['TimeHandoff'] = train['TimeHandoff'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ"))
train['TimeSnap'] = train['TimeSnap'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ"))
train['TimeDelta'] = train.apply(lambda row:(row['TimeHandoff'] - row['TimeSnap'] ).total_seconds() , axis=1)
 train['PlayerBirthDate'] = train['PlayerBirthDate'].apply(lambda x: datetime.datetime.strptime(x, "%m/%d/%Y"))
seconds_in_year = 60*60*24*365
train = train.drop(['TimeHandoff', 'TimeSnap', 'PlayerBirthDate'], axis=1)
 def give_me_WindSpeed(txt):
txt = str(txt ).lower().replace('mph', '' ).strip()
if '-' in txt:
txt =(int(txt.split('-')[0])+ int(txt.split('-')[1])) / 2
try:
return float(txt)
 except:
return -1
train['WindSpeed'] = train['WindSpeed'].apply(give_me_WindSpeed)
 train['PlayDirection'] = train['PlayDirection'].apply(lambda x: x is 'right')
 train['Team'] = train['Team'].apply(lambda x: x.strip() =='home')
 train['Field_eq_Possession'] = train['FieldPosition'] == train['PossessionTeam']
def give_me_GameWeather(txt):
txt = str(txt ).lower().replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly' ).replace('clear and sunny', 'sunny and clear')
 txt = txt.replace('skies', '' ).replace("mostly", "" ).strip()
if "indoor" in txt:
txt = "indoor"
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*5
if 'sunny' in txt or 'sun' in txt:
return ans*3
if 'clear' in txt:
return ans
if 'cloudy' in txt:
return -ans
if 'rain' in txt or 'rainy' in txt:
return -3*ans
if 'snow' in txt:
return -5*ans
return 0
train['GameWeather'] = train['GameWeather'].apply(give_me_GameWeather)
 train['IsRusher'] = train['NflId'] == train['NflIdRusher']
train = train.sort_values(by=['PlayId', 'Team', 'IsRusher'] ).reset_index()
train.drop(['GameId', 'PlayId', 'index', 'IsRusher', 'Team', 'NflId', 'NflIdRusher'], axis=1, inplace=True)
 cat_features = []
for col in train.columns:
if train[col].dtype =='object':
cat_features.append(col)
 train = train.drop(cat_features, axis=1)
 train.fillna(-999, inplace=True)
 return train
fg_unet = load_model("/kaggle/input/mnist-w-fgunet-output/best_model.h5")
 vgg = load_model("/kaggle/input/mnist-w-vgg16-output/best_model.h5")
 resnet = load_model("/kaggle/input/mnist-w-resnet-output/best_model.h5" )<prepare_x_and_y>
train = fe(train )
X = [train.iloc[i,1:].values for i in range(len(train)) ]
X = [x.reshape(28,28)for x in X]
X_28 = [x.reshape(28,28,1,1)for x in X]
X_28 = np.array(X_28)
 X = [np.pad(x, 2)for x in X]
X = np.array(X)
 X = X.reshape(X.shape[0],X.shape[1], X.shape[2],1)
 X = np.repeat(X, 3, axis=-1 )<normalization>
X_train = np.array(train[players_col] ).reshape(-1,(len(players_col)) *22)
 play_col = train.drop(players_col+['Yards'], axis=1 ).columns
X_play_col = np.zeros(shape=(X_train.shape[0], len(play_col)))
 for i, col in enumerate(play_col):
X_play_col[:, i] = train[col][::22]
X_test = [test.iloc[i,:].values for i in range(len(test)) ]
X_test = [x.reshape(28,28)for x in X_test]
X_test_28 = [x.reshape(28,28,1,1)for x in X_test]
X_test_28 = np.array(X_test_28)
 X_test = [np.pad(x, 2)for x in X_test]
X_test = np.array(X_test)
 X_test = X_test.reshape(X_test.shape[0],X_test.shape[1], X_test.shape[2],1)
 X_test = np.repeat(X_test, 3, axis=-1 )<predict_on_test>
X_train = np.concatenate([X_train, X_play_col], axis=1)
 y_train = np.zeros(shape=(X_train.shape[0], 199))
for i,yard in enumerate(train['Yards'][::22]):
y_train[i, yard+99:] = np.ones(shape=(1, 100-yard))
f_y_train = fg_unet.predict(X_28, verbose=1)
 v_y_train = vgg.predict(X, verbose=1)
 r_y_train = resnet.predict(X, verbose=1 )<predict_on_test>
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Embedding, Concatenate, Flatten, BatchNormalization, Dropout
from keras.callbacks import ModelCheckpoint
from keras.utils.vis_utils import plot_model
f_y_test = fg_unet.predict(X_test_28, verbose=1)
 v_y_test = vgg.predict(X_test, verbose=1)
 r_y_test = resnet.predict(X_test, verbose=1 )<categorify>
folds = 10
seed = 1997
kf = KFold(n_splits = folds, shuffle = True, random_state=seed)
 models_1, models_2 = [], []
def give_me_model_1() :
numerical_inputs = Input(shape=(X_train.shape[1],))
x = Dense(X_train.shape[1], activation='relu' )(numerical_inputs)
 x = BatchNormalization()(x)
 logits1 = Dense(512,activation='relu' )(x)
 logits = Concatenate()([x, logits1])
 logits = Dropout(0.3 )(logits)
 logits1 = Dense(512,activation='relu' )(logits)
 logits = Concatenate()([logits, logits1])
 logits = Dropout(0.2 )(logits)
 logits1 = Dense(256,activation='relu' )(logits)
 logits1 = Concatenate()([logits, logits1])
 logits1 = Dropout(0.1 )(logits1)
 logits1 = Dense(256,activation='relu' )(logits1)
 out = Dense(199, activation='sigmoid' )(logits1)
 model = Model(inputs = numerical_inputs, outputs=out)
 model.compile(optimizer='adam',loss='mse')
 return model
def give_me_model_2() :
numerical_inputs = Input(shape=(X_train.shape[1],))
x = Dense(X_train.shape[1], activation='relu' )(numerical_inputs)
 x = BatchNormalization()(x)
 logits = Dense(512,activation='relu' )(x)
 logits = Dropout(0.3 )(logits)
 logits = Dense(256,activation='relu' )(logits)
 logits = Dropout(0.2 )(logits)
 logits = Dense(256,activation='relu' )(logits)
 out = Dense(199, activation='sigmoid' )(logits)
 model = Model(inputs = numerical_inputs, outputs=out)
 model.compile(optimizer='adam',loss='mse')
 return model
for tr_idx, val_idx in kf.split(X_train, y_train):
tr_x, tr_y = X_train[tr_idx,:], y_train[tr_idx]
vl_x, vl_y = X_train[val_idx,:], y_train[val_idx]
checkpoint_1 = ModelCheckpoint('keras_model_1.hdf5', monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
 callbacks_list_1 = [checkpoint_1]
checkpoint_2 = ModelCheckpoint('keras_model_2.hdf5', monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
 callbacks_list_2 = [checkpoint_2]
model_1 = give_me_model_1()
model_2 = give_me_model_2()
model_1.fit(tr_x, tr_y,
epochs=10,
verbose=0,
callbacks=callbacks_list_1,
validation_data=(vl_x, vl_y))
model_2.fit(tr_x, tr_y,
epochs=10,
verbose=0,
callbacks=callbacks_list_2,
validation_data=(vl_x, vl_y))
model_1.load_weights('keras_model_1.hdf5')
 model_2.load_weights('keras_model_2.hdf5')
 models_1.append(model_1)
 models_2.append(model_2 )
n_classes = 10
y = [train.iloc[i,0] for i in range(len(train)) ]
y = np.array(y)
 print(np.unique(y, return_counts=True))
y = to_categorical(y, num_classes=n_classes)
 y.shape<train_model>
X = np.hstack([f_y_train, v_y_train, r_y_train])
 rid = Ridge()
rid.fit(X, y )<concatenate>
plot_model(model_1, to_file='model_plot_1.png', show_shapes=True )
X_pred = np.hstack([f_y_test, v_y_test, r_y_test] )<predict_on_test>
plot_model(model_2, to_file='model_plot_2.png', show_shapes=True )
<save_to_csv><EOS> 
for df, sample in tqdm.tqdm(env.iter_test()):
df.drop(['Season', 'DefensePersonnel', 'Temperature', 'Humidity'], axis = 1, inplace = True)
 df['OffenseFormation'] = df['OffenseFormation'].apply(lambda x: x if x in off_form else np.nan)
 df = pd.concat([df.drop(['OffenseFormation'], axis=1), pd.get_dummies(df['OffenseFormation'], prefix='Formation')], axis=1)
 missing_cols = set(dummy_col)- set(df.columns)-set('Yards')
 for c in missing_cols:
df[c] = 0
df = df[dummy_col]
df.drop(['Yards'], axis=1, inplace=True)
 df = fe(df)
 X = np.array(df[players_col] ).reshape(-1,(len(players_col)) *22)
 play_col = df.drop(players_col, axis=1 ).columns
X_play_col = np.zeros(shape=(X.shape[0], len(play_col)))
 for i, col in enumerate(play_col):
X_play_col[:, i] = df[col][::22]
X = np.concatenate([X, X_play_col], axis=1)
 y_pred_1 = np.sum([model.predict(X)for model in models_1], axis=0)/ kf.n_splits
y_pred_2 = np.sum([model.predict(X)for model in models_2], axis=0)/ kf.n_splits
y_pred = y_pred_1 * 0.5 + y_pred_2 * 0.5
for pred in y_pred:
prev = 0
for i in range(len(pred)) :
if pred[i]<prev:
pred[i]=prev
prev=pred[i]
env.predict(pd.DataFrame(data=y_pred,columns=sample.columns))
env.write_submission_file()