Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<choose_model_class>
TRAIN_OFFLINE = False
pd.set_option('display.max_columns', 50)
 pd.set_option('display.max_rows', 150 )
def test_with_calibration(vectorizer=TfidfVectorizer(sublinear_tf=True, max_df=0.5, stop_words='english'),
C_logit = [0.3, 1, 3, 10, 30], alpha_SGD = [1e-5, 3e-5, 1e-4, 3e-4, 1e-3],
alpha_NB = [0.01, 0.03, 0.1, 0.3, 1]):
this_range = trange(10)
 model_names = []
params = []
targets = []
preds = []
preds_calib_sig = []
preds_calib_iso = []
models = [LogisticRegression(C=c)for c in C_logit] + [
SGDClassifier(loss='modified_huber', alpha=a, max_iter=10000, tol=1e-4)for a in alpha_SGD] + [
MultinomialNB(alpha=a)for a in alpha_NB] + [
BernoulliNB(alpha=a)for a in alpha_NB]
for test_loop in this_range:
test = csv.iloc[int(len(csv)*test_loop/10): int(len(csv)*(test_loop+1)/10)]
train = csv.loc[list(set(csv.index ).difference(test.index)) ]
y_train = train.author.apply(lambda auth: authors_dict[auth] ).values
y_test = test.author.apply(lambda auth: authors_dict[auth] ).values
X_train = vectorizer.fit_transform(train.text.values)
 X_test = vectorizer.transform(test.text.values)
 for m in models:
name = str(m ).split('(')[0]
if name.endswith('NB')or name == 'SGDClassifier':
param = m.alpha
elif name == 'SVC' or name == 'LogisticRegression':
param = m.C
this_range.set_postfix(working_on=name, step='base')
 m.fit(X_train, y_train)
 targets.append(y_test)
 model_names.append([name] * len(y_test))
params.append([param] * len(y_test))
preds.append(m.predict_proba(X_test))
this_range.set_postfix(working_on=name, step='sigmoid')
 m_sigmoid = CalibratedClassifierCV(m, method='sigmoid')
 m_sigmoid.fit(X_train, y_train)
 preds_calib_sig.append(m_sigmoid.predict_proba(X_test))
this_range.set_postfix(working_on=name, step='isotonic')
 m_isotonic = CalibratedClassifierCV(m, method='isotonic')
 m_isotonic.fit(X_train, y_train)
 preds_calib_iso.append(m_isotonic.predict_proba(X_test))
targets = np.concatenate(targets)
 preds = np.concatenate(preds)
 preds_calib_sig = np.concatenate(preds_calib_sig)
 preds_calib_iso = np.concatenate(preds_calib_iso)
 params = np.log10(np.concatenate(params))
model_names = np.concatenate(model_names)
 log_losses = -np.log(np.clip(preds[np.arange(len(preds)) , targets], 1e-15, 1-1e-15))
log_losses_sig = -np.log(np.clip(preds_calib_sig[np.arange(len(preds_calib_sig)) , targets], 1e-15, 1-1e-15))
log_losses_iso = -np.log(np.clip(preds_calib_iso[np.arange(len(preds_calib_iso)) , targets], 1e-15, 1-1e-15))
final_results = pd.DataFrame({'LogLoss': log_losses, 'Param': params, 'Names': model_names,
'Target': targets, 'Prediction': np.argmax(preds, axis=1),
'LogLoss Sigmoid': log_losses_sig, 'LogLoss Isotonic': log_losses_iso})
 final_results['Accuracy'] = final_results['Target'] == final_results['Prediction']
return pd.concat(( final_results[[c, 'Names', 'Param']].groupby(['Names', 'Param'] ).mean()
for c in('LogLoss', 'LogLoss Sigmoid', 'LogLoss Isotonic', 'Accuracy')) ,
axis=1 ).reset_index()<split>
if TRAIN_OFFLINE:
train = pd.read_csv('.. /input/train.csv', dtype={'WindSpeed': 'object'})
 else:
train = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', dtype={'WindSpeed': 'object'})
calibrated_results = test_with_calibration()<create_dataframe>
outcomes = train[['GameId','PlayId','Yards']].drop_duplicates()
using_countvectorizer = pd.DataFrame([['BernoulliNB', -2.0, 0.706319987941122, 0.4813479864258888, 0.43078230217300867, 0.8297155115174422], ['BernoulliNB', -1.5228787452803376, 0.6125400411922719, 0.4693183991047648, 0.4200070176089392, 0.8346697992747332], ['BernoulliNB', -1.0, 0.5318631184911502, 0.45957841327517474, 0.4124031543488292, 0.8377853822973594], ['BernoulliNB', -0.5228787452803376, 0.4897262986685063, 0.4636773714716324, 0.41451718613477395, 0.8371724807191379], ['BernoulliNB', 0.0, 0.5178300753228159, 0.5162830243131579, 0.44378952304323976, 0.8171510291639001], ['LogisticRegression', -0.5228787452803376, 0.5439896178579512, 0.5206278056309603, 0.5189494603320542, 0.799070432606364], ['LogisticRegression', 0.0, 0.4939695963535932, 0.5048351798532839, 0.5019940867981258, 0.8081618060166504], ['LogisticRegression', 0.47712125471966244, 0.49769625473481144, 0.5084358776109752, 0.5058884868022172, 0.8038204198375811], ['LogisticRegression', 1.0, 0.5655966032375217, 0.5237789358787479, 0.5223114794515888, 0.795444098268553], ['LogisticRegression', 1.4771212547196624, 0.6916563102635536, 0.5419143219215302, 0.5405129033294451, 0.7849226211757495], ['MultinomialNB', -2.0, 0.718011588213635, 0.4843884436595753, 0.4345146438138747, 0.8253230502068543], ['MultinomialNB', -1.5228787452803376, 0.6217299212692091, 0.47175176740564567, 0.4240145043205851, 0.830685939016293], ['MultinomialNB', -1.0, 0.5364765812733492, 0.45961192052773553, 0.4158300934342699, 0.8346187241432147], ['MultinomialNB', -0.5228787452803376, 0.48446107569802527, 0.45566722031583384, 0.41677265516149553, 0.8357423770366209], ['MultinomialNB', 0.0, 0.45877407274879933, 0.464806847973281, 0.43570686348654986, 0.8304305633587007], ['SGDClassifier', -5.0, 4.885052266624309, 0.5405027466565611, 0.5385120105442537, 0.7667909494866949], ['SGDClassifier', -4.522878745280337, 4.150779202151389, 0.5416658943504635, 0.5397880680058839, 0.7801215588130139], ['SGDClassifier', -4.0, 2.692209778404653, 0.5327215220018812, 0.5304161262567021, 0.793350017876296], ['SGDClassifier', -3.5228787452803374, 1.3803166326665117, 0.5182917369567382, 0.517008856903903, 0.8023392410235456], ['SGDClassifier', -3.0, 0.6988518162337475, 0.507113596537479, 0.5036106107336277, 0.8055058991776903]], columns=['Names', 'Param', 'LogLoss', 'LogLoss Sigmoid', 'LogLoss Isotonic', 'Accuracy'])
 plot_calibrated_results(using_countvectorizer, title='Using CountVectorizer' )<create_dataframe>
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 preprocess(train):
train['GameClock_sec'] = train['GameClock'].apply(strtoseconds)
 train["GameClock_minute"] = train["GameClock"].apply(lambda x : x.split(":")[0] ).astype("object")
 train['PlayerHeight_dense'] = train['PlayerHeight'].apply(lambda x: 12*int(x.split('-')[0])+int(x.split('-')[1]))
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.25
train['PlayerAge'] = train.apply(lambda row:(row['TimeHandoff']-row['PlayerBirthDate'] ).total_seconds() /seconds_in_year, axis=1)
 train["PlayerAge_ob"] = train['PlayerAge'].astype(np.int ).astype("object")
 train['WindSpeed_ob'] = train['WindSpeed'].apply(lambda x: x.lower().replace('mph', '' ).strip() if not pd.isna(x)else x)
 train['WindSpeed_ob'] = train['WindSpeed_ob'].apply(lambda x:(int(x.split('-')[0])+int(x.split('-')[1])) /2 if not pd.isna(x)and '-' in x else x)
 train['WindSpeed_ob'] = train['WindSpeed_ob'].apply(lambda x:(int(x.split() [0])+int(x.split() [-1])) /2 if not pd.isna(x)and type(x)!=float and 'gusts up to' in x else x)
 train['WindSpeed_dense'] = train['WindSpeed_ob'].apply(strtofloat)
 train['GameWeather_process'] = train['GameWeather'].str.lower()
train['GameWeather_process'] = train['GameWeather_process'].apply(lambda x: "indoor" if not pd.isna(x)and "indoor" in x else x)
 train['GameWeather_process'] = train['GameWeather_process'].apply(lambda x: x.replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly')if not pd.isna(x)else x)
 train['GameWeather_process'] = train['GameWeather_process'].apply(lambda x: x.replace('clear and sunny', 'sunny and clear')if not pd.isna(x)else x)
 train['GameWeather_process'] = train['GameWeather_process'].apply(lambda x: x.replace('skies', '' ).replace("mostly", "" ).strip() if not pd.isna(x)else x)
 train['GameWeather_dense'] = train['GameWeather_process'].apply(map_weather)
 train['IsRusher'] =(train['NflId'] == train['NflIdRusher'])
 train['IsRusher_ob'] =(train['NflId'] == train['NflIdRusher'] ).astype("object")
 temp = train[train["IsRusher"]][["Team", "PlayId"]].rename(columns={"Team":"RusherTeam"})
 train = train.merge(temp, on = "PlayId")
 train["IsRusherTeam"] = train["Team"] == train["RusherTeam"]
train["Quarter_ob"] = train["Quarter"].astype("object")
 train["Down_ob"] = train["Down"].astype("object")
 train["JerseyNumber_ob"] = train["JerseyNumber"].astype("object")
 train["YardLine_ob"] = train["YardLine"].astype("object")
 train["Orientation_ob"] = train["Orientation"].apply(lambda x : orientation_to_cat(x)).astype("object")
 train["Dir_ob"] = train["Dir"].apply(lambda x : orientation_to_cat(x)).astype("object")
 train["Orientation_sin"] = train["Orientation"].apply(lambda x : np.sin(x/360 * 2 * np.pi))
train["Orientation_cos"] = train["Orientation"].apply(lambda x : np.cos(x/360 * 2 * np.pi))
train["Dir_sin"] = train["Dir"].apply(lambda x : np.sin(x/360 * 2 * np.pi))
train["Dir_cos"] = train["Dir"].apply(lambda x : np.cos(x/360 * 2 * np.pi))
train["diffScoreBeforePlay"] = train["HomeScoreBeforePlay"] - train["VisitorScoreBeforePlay"]
train["diffScoreBeforePlay_binary_ob"] =(train["HomeScoreBeforePlay"] > train["VisitorScoreBeforePlay"] ).astype("object")
 Turf = {'Field Turf':'Artificial', 'A-Turf Titan':'Artificial', 'Grass':'Natural', 'UBU Sports Speed S5-M':'Artificial', 'Artificial':'Artificial', 'DD GrassMaster':'Artificial', 'Natural Grass':'Natural', 'UBU Speed Series-S5-M':'Artificial', 'FieldTurf':'Artificial', 'FieldTurf 360':'Artificial', 'Natural grass':'Natural', 'grass':'Natural', 'Natural':'Natural', 'Artifical':'Artificial', 'FieldTurf360':'Artificial', 'Naturall Grass':'Natural', 'Field turf':'Artificial', 'SISGrass':'Artificial', 'Twenty-Four/Seven Turf':'Artificial', 'natural grass':'Natural'}
train['Turf'] = train['Turf'].map(Turf)
 temp = train["OffensePersonnel"].iloc[np.arange(0, len(train), 22)].apply(lambda x : pd.Series(OffensePersonnelSplit(x)))
 temp.columns = ["Offense" + c for c in temp.columns]
temp["PlayId"] = train["PlayId"].iloc[np.arange(0, len(train), 22)]
train = train.merge(temp, on = "PlayId")
 temp = train["DefensePersonnel"].iloc[np.arange(0, len(train), 22)].apply(lambda x : pd.Series(DefensePersonnelSplit(x)))
 temp.columns = ["Defense" + c for c in temp.columns]
temp["PlayId"] = train["PlayId"].iloc[np.arange(0, len(train), 22)]
train = train.merge(temp, on = "PlayId")
 train = train.sort_values(by = ['X'] ).sort_values(by = ['Dis'] ).sort_values(by=['PlayId', 'IsRusherTeam', 'IsRusher'] ).reset_index(drop = True)
 return train
custom_token_pattern = pd.DataFrame([['BernoulliNB', -2.0, 0.6635507999191133, 0.44788502436384, 0.39315348567426417, 0.8456509525512028], ['BernoulliNB', -1.5228787452803376, 0.5788288927657376, 0.435446783320394, 0.3830651254165367, 0.8497369630726799], ['BernoulliNB', -1.0, 0.5078835075618886, 0.42725610915188744, 0.3759747559507382, 0.8528014709637878], ['BernoulliNB', -0.5228787452803376, 0.47314354690823984, 0.4340981141919862, 0.37993489818399007, 0.8516778180703816], ['BernoulliNB', 0.0, 0.5019734806584952, 0.48646348803130657, 0.41263612786031756, 0.8354359262475101], ['LogisticRegression', -0.5228787452803376, 0.7026691796469897, 0.5869410485168362, 0.5905181513515557, 0.7765462996067215], ['LogisticRegression', 0.0, 0.5692719996490762, 0.5083127564046314, 0.5092162033832176, 0.8146483477194953], ['LogisticRegression', 0.47712125471966244, 0.4792948573113742, 0.45906101187564213, 0.46239255113852323, 0.8330353950661423], ['LogisticRegression', 1.0, 0.4290701429588527, 0.4375943934428565, 0.43948342537860563, 0.8397773124265795], ['LogisticRegression', 1.4771212547196624, 0.43300570627987706, 0.4385391369298042, 0.4408027000067626, 0.8358445272996577], ['MultinomialNB', -2.0, 0.4125941620437241, 0.4190215749565747, 0.40828040804656934, 0.8393687113744318], ['MultinomialNB', -1.5228787452803376, 0.40714249110758255, 0.40129174326712436, 0.39360636427980883, 0.8455998774196843], ['MultinomialNB', -1.0, 0.4259516450862784, 0.3884868773956777, 0.383110501574009, 0.8498901884672353], ['MultinomialNB', -0.5228787452803376, 0.47630251030213644, 0.39167800671646685, 0.3901468642181604, 0.8466724551815721], ['MultinomialNB', 0.0, 0.5812538136471457, 0.42287631504181267, 0.4247365110254928, 0.8193983349507125], ['SGDClassifier', -5.0, 2.644901132462858, 0.47825526055714196, 0.47794256422203635, 0.8129117932478677], ['SGDClassifier', -4.522878745280337, 1.2549952199372436, 0.4619323425203225, 0.4603783489936237, 0.8290004596761836], ['SGDClassifier', -4.0, 0.6341338181070553, 0.4339657370855429, 0.4342455996997695, 0.8413095663721334], ['SGDClassifier', -3.5228787452803374, 0.5985628556207552, 0.4483802897245992, 0.44981922818186937, 0.8316052913836253], ['SGDClassifier', -3.0, 0.6680392917833494, 0.5073434790084068, 0.5091260173524894, 0.8030032177332856]], columns=['Names', 'Param', 'LogLoss', 'LogLoss Sigmoid', 'LogLoss Isotonic', 'Accuracy'])
 plot_calibrated_results(custom_token_pattern, title='Custom token_pattern' )<create_dataframe>
def create_features(df, deploy=False):
def new_X(x_coordinate, play_direction):
if play_direction == 'left':
return 120.0 - x_coordinate
else:
return x_coordinate
def new_line(rush_team, field_position, yardline):
if rush_team == field_position:
return 10.0 + yardline
else:
return 60.0 +(50 - yardline)
 def new_orientation(angle, play_direction):
if play_direction == 'left':
new_angle = 360.0 - angle
if new_angle == 360.0:
new_angle = 0.0
return new_angle
else:
return angle
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 back_direction(orientation):
if orientation > 180.0:
return 1
else:
return 0
def update_yardline(df):
new_yardline = df[df['NflId'] == df['NflIdRusher']]
new_yardline['YardLine'] = new_yardline[['PossessionTeam','FieldPosition','YardLine']].apply(lambda x: new_line(x[0],x[1],x[2]), axis=1)
 new_yardline = new_yardline[['GameId','PlayId','YardLine']]
return new_yardline
def update_orientation(df, yardline):
df['X'] = df[['X','PlayDirection']].apply(lambda x: new_X(x[0],x[1]), axis=1)
 df['Orientation'] = df[['Orientation','PlayDirection']].apply(lambda x: new_orientation(x[0],x[1]), axis=1)
 df['Dir'] = df[['Dir','PlayDirection']].apply(lambda x: new_orientation(x[0],x[1]), axis=1)
 df = df.drop('YardLine', axis=1)
 df = pd.merge(df, yardline, on=['GameId','PlayId'], how='inner')
 return df
def back_features(df):
carriers = df[df['NflId'] == df['NflIdRusher']][['GameId','PlayId','NflIdRusher','X','Y','Orientation','Dir','YardLine']]
carriers['back_from_scrimmage'] = carriers['YardLine'] - carriers['X']
carriers['back_oriented_down_field'] = carriers['Orientation'].apply(lambda x: back_direction(x))
carriers['back_moving_down_field'] = carriers['Dir'].apply(lambda x: back_direction(x))
carriers = carriers.rename(columns={'X':'back_X',
'Y':'back_Y'})
 carriers = carriers[['GameId','PlayId','NflIdRusher','back_X','back_Y','back_from_scrimmage','back_oriented_down_field','back_moving_down_field']]
return carriers
def features_relative_to_back(df, carriers):
player_distance = df[['GameId','PlayId','NflId','X','Y']]
player_distance = pd.merge(player_distance, carriers, on=['GameId','PlayId'], how='inner')
 player_distance = player_distance[player_distance['NflId'] != player_distance['NflIdRusher']]
player_distance['dist_to_back'] = player_distance[['X','Y','back_X','back_Y']].apply(lambda x: euclidean_distance(x[0],x[1],x[2],x[3]), axis=1)
 player_distance = player_distance.groupby(['GameId','PlayId','back_from_scrimmage','back_oriented_down_field','back_moving_down_field'])\ 
.agg({'dist_to_back':['min','max','mean','std']})\ 
.reset_index()
player_distance.columns = ['GameId','PlayId','back_from_scrimmage','back_oriented_down_field','back_moving_down_field',
'min_dist','max_dist','mean_dist','std_dist']
return player_distance
def defense_features(df):
rusher = df[df['NflId'] == df['NflIdRusher']][['GameId','PlayId','Team','X','Y']]
rusher.columns = ['GameId','PlayId','RusherTeam','RusherX','RusherY']
defense = pd.merge(df,rusher,on=['GameId','PlayId'],how='inner')
 defense = defense[defense['Team'] != defense['RusherTeam']][['GameId','PlayId','X','Y','RusherX','RusherY']]
defense['def_dist_to_back'] = defense[['X','Y','RusherX','RusherY']].apply(lambda x: euclidean_distance(x[0],x[1],x[2],x[3]), axis=1)
 defense = defense.groupby(['GameId','PlayId'])\ 
.agg({'def_dist_to_back':['min','max','mean','std']})\ 
.reset_index()
defense.columns = ['GameId','PlayId','def_min_dist','def_max_dist','def_mean_dist','def_std_dist']
return defense
def rusher_features(df):
rusher = df[df['NflId'] == df['NflIdRusher']][['GameId','PlayId','Dir', 'S', 'A', 'X', 'Y']]
rusher.columns = ['GameId','PlayId', 'RusherDir', 'RusherS', 'RusherA', 'RusherX', 'RusherY']
radian_angle =(90 - rusher['RusherDir'])* np.pi / 180.0
v_horizontal = np.abs(rusher['RusherS'] * np.cos(radian_angle))
v_vertical = np.abs(rusher['RusherS'] * np.sin(radian_angle))
rusher['v_horizontal'] = v_horizontal
rusher['v_vertical'] = v_vertical
rusher.columns = ['GameId','PlayId', 'RusherDir', 'RusherS','RusherA','RusherX', 'RusherY','v_horizontal', 'v_vertical']
for feat1 in ['RusherS','RusherA','RusherX', 'RusherY']:
for feat2 in ['RusherS','RusherA','RusherX', 'RusherY']:
if feat1 == feat2:
continue
new_feature = feat1 + '_' + feat2
new_feature_reversed = feat2 + '_' + feat1
if new_feature in rusher.columns or new_feature_reversed in rusher.columns:
continue
rusher[new_feature] = rusher[feat1].div(rusher[feat2] + 10)
 rusher['v_horizontal_vertical'] = rusher['v_horizontal'].div(rusher['v_vertical'] + 10)
 return rusher
def static_features(df):
add_new_feas = []
df['PlayerHeight_dense'] = df['PlayerHeight'].apply(lambda x: 12*int(x.split('-')[0])+int(x.split('-')[1]))
add_new_feas.append('PlayerHeight_dense')
 df['TimeHandoff'] = df['TimeHandoff'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ"))
df['TimeSnap'] = df['TimeSnap'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ"))
df['TimeDelta'] = df.apply(lambda row:(row['TimeHandoff'] - row['TimeSnap'] ).total_seconds() , axis=1)
 df['PlayerBirthDate'] =df['PlayerBirthDate'].apply(lambda x: datetime.datetime.strptime(x, "%m/%d/%Y"))
seconds_in_year = 60*60*24*365.25
df['PlayerAge'] = df.apply(lambda row:(row['TimeHandoff']-row['PlayerBirthDate'] ).total_seconds() /seconds_in_year, axis=1)
 add_new_feas.append('PlayerAge')
 df['WindSpeed_ob'] = df['WindSpeed'].apply(lambda x: x.lower().replace('mph', '' ).strip() if not pd.isna(x)else x)
 df['WindSpeed_ob'] = df['WindSpeed_ob'].apply(lambda x:(int(x.split('-')[0])+int(x.split('-')[1])) /2 if not pd.isna(x)and '-' in x else x)
 df['WindSpeed_ob'] = df['WindSpeed_ob'].apply(lambda x:(int(x.split() [0])+int(x.split() [-1])) /2 if not pd.isna(x)and type(x)!=float and 'gusts up to' in x else x)
 df['WindSpeed_dense'] = df['WindSpeed_ob'].apply(strtofloat)
 df["Orientation_ob"] = df["Orientation"].apply(lambda x : orientation_to_cat(x)).astype("object")
 df["Dir_ob"] = df["Dir"].apply(lambda x : orientation_to_cat(x)).astype("object")
 df["Orientation_sin"] = df["Orientation"].apply(lambda x : np.sin(x/360 * 2 * np.pi))
df["Orientation_cos"] = df["Orientation"].apply(lambda x : np.cos(x/360 * 2 * np.pi))
df["Dir_sin"] = df["Dir"].apply(lambda x : np.sin(x/360 * 2 * np.pi))
df["Dir_cos"] = df["Dir"].apply(lambda x : np.cos(x/360 * 2 * np.pi))
add_new_feas.append("Dir_sin")
 add_new_feas.append("Dir_cos")
 df["diffScoreBeforePlay"] = df["HomeScoreBeforePlay"] - df["VisitorScoreBeforePlay"]
add_new_feas.append("diffScoreBeforePlay")
 static_features = df[df['NflId'] == df['NflIdRusher']][add_new_feas+['GameId','PlayId','X','Y','S','A','Dis','Orientation','Dir',
'YardLine','Quarter']].drop_duplicates()
static_features.fillna(-999,inplace=True)
 return static_features
def combine_features(relative_to_back, defense,rushing, static, deploy=deploy):
df = pd.merge(relative_to_back,defense,on=['GameId','PlayId'],how='inner')
 df = pd.merge(df,rushing,on=['GameId','PlayId'],how='inner')
 df = pd.merge(df,static,on=['GameId','PlayId'],how='inner')
 if not deploy:
df = pd.merge(df, outcomes, on=['GameId','PlayId'], how='inner')
 return df
yardline = update_yardline(df)
 df = update_orientation(df, yardline)
 back_feats = back_features(df)
 rel_back = features_relative_to_back(df, back_feats)
 def_feats = defense_features(df)
 rush_feats = rusher_features(df)
 static_feats = static_features(df)
 basetable = combine_features(rel_back, def_feats,rush_feats,static_feats, deploy=deploy)
 return basetable
using_2grams = pd.DataFrame([['BernoulliNB', -2.0, 1.1829537822268923, 0.42882184350651664, 0.3574336854854024, 0.8605648909545942], ['BernoulliNB', -1.5228787452803376, 1.0034099170691535, 0.4238300133137205, 0.3521584466856666, 0.8617396189795189], ['BernoulliNB', -1.0, 0.8584855983611743, 0.4329739587553948, 0.35381801965661336, 0.8620971449001481], ['BernoulliNB', -0.5228787452803376, 0.907776895154225, 0.5143400658599186, 0.38069723614289763, 0.8418713928188365], ['BernoulliNB', 0.0, 2.5284020189561827, 0.8665639570532223, 0.48821064408572684, 0.6911997548393687], ['LogisticRegression', 0.47712125471966244, 0.5166478797790038, 0.48547839857229863, 0.5037727689750268, 0.8290515348077021], ['LogisticRegression', 1.0, 0.4392387650003737, 0.45016674100276843, 0.461878375648529, 0.8435057970274273], ['LogisticRegression', 1.4771212547196624, 0.40915645510723675, 0.4352609278613576, 0.4472301715832008, 0.8477450329434598], ['LogisticRegression', 2.0, 0.40765827822750805, 0.42700452030205444, 0.4389093348868385, 0.8498901884672353], ['LogisticRegression', 2.4771212547196626, 0.42449890413885477, 0.42276079942698247, 0.43303743984503246, 0.850962766229123], ['MultinomialNB', -2.0, 0.3578113672136049, 0.37545929030279757, 0.36030367415241854, 0.8583175851677818], ['MultinomialNB', -1.5228787452803376, 0.3475764766027269, 0.36321418518356896, 0.3577637730813878, 0.8640890750293682], ['MultinomialNB', -1.0, 0.37991004939354456, 0.36445150749047184, 0.37139598970530746, 0.8616885438480004], ['MultinomialNB', -0.5228787452803376, 0.472218907236443, 0.3899353777583827, 0.40325147650530285, 0.835538076510547], ['MultinomialNB', 0.0, 0.6367635084522356, 0.4483081235646716, 0.46652352411338466, 0.7606108585729608], ['SGDClassifier', -5.0, 1.875315599401462, 0.4362438404927373, 0.43556807875290676, 0.8412074161090964], ['SGDClassifier', -4.522878745280337, 0.7585651162455638, 0.4224121891513341, 0.427068782985493, 0.8499923387302722], ['SGDClassifier', -4.0, 0.5828856388906344, 0.4246164926556892, 0.4358121664644361, 0.8480514837325707], ['SGDClassifier', -3.5228787452803374, 0.6079532216632112, 0.4659431255610908, 0.48242465809839463, 0.8286940088870729], ['SGDClassifier', -3.0, 0.7164340295311806, 0.547004057582182, 0.562190687042716, 0.7859951989376373]], columns=['Names', 'Param', 'LogLoss', 'LogLoss Sigmoid', 'LogLoss Isotonic', 'Accuracy'])
 plot_calibrated_results(using_2grams, title='Using 2-grams' )<choose_model_class>
%time train_basetable = create_features(train, False )
previous_best = [('MultiNB', MultinomialNB(alpha=0.03)) , 
('Calibrated MultiNB', CalibratedClassifierCV(MultinomialNB(alpha=0.03), method='isotonic')) , 
('Calibrated BernoulliNB', CalibratedClassifierCV(BernoulliNB(alpha=0.03), method='isotonic')) , 
('Calibrated Huber', CalibratedClassifierCV(
 SGDClassifier(loss='modified_huber', alpha=1e-4, max_iter=10000, tol=1e-4), method='sigmoid')) , 
('Logit', LogisticRegression(C=30)) ]
def test_ensembling() :

 this_range = trange(10)
 model_names = []
targets = []
preds = []
vectorizer=TfidfVectorizer(token_pattern=r'\w{1,}', sublinear_tf=True, ngram_range=(1,2))
voting_model_uniform = VotingClassifier(previous_best, voting='soft')
 voting_model_NB = VotingClassifier([(s,m)for s,m in previous_best if 'NB' in s], voting='soft')
 voting_model = VotingClassifier(previous_best, voting='soft', weights=[3,3,3,1,1])
 bagging_model = BaggingClassifier(MultinomialNB(alpha=0.03))
bagging_calibrated_model = BaggingClassifier(
 CalibratedClassifierCV(MultinomialNB(alpha=0.03), method='isotonic'))
models = [voting_model_uniform, voting_model_NB,
voting_model, bagging_model, bagging_calibrated_model]
these_names = ['Voting Uniform Weights', 'Voting NB Only',
'Voting Merit Weights', 'MultinomialNB Bagging', 'Calibration + Bagging']
for test_loop in this_range:
test = csv.iloc[int(len(csv)*test_loop/10): int(len(csv)*(test_loop+1)/10)]
train = csv.loc[list(set(csv.index ).difference(test.index)) ]
y_train = train.author.apply(lambda auth: authors_dict[auth] ).values
y_test = test.author.apply(lambda auth: authors_dict[auth] ).values
vectorizer = TfidfVectorizer(token_pattern=r'\w{1,}', sublinear_tf=True, ngram_range=(1,2))
X_train = vectorizer.fit_transform(train.text.values)
 X_test = vectorizer.transform(test.text.values)
 for m,name in zip(models, these_names):
this_range.set_postfix(working_on=name)
 m.fit(X_train, y_train)
 targets.append(y_test)
 model_names.append([name] * len(y_test))
preds.append(m.predict_proba(X_test))
targets = np.concatenate(targets)
 preds = np.concatenate(preds)
 model_names = np.concatenate(model_names)
 log_losses = -np.log(np.clip(preds[np.arange(len(preds)) , targets], 1e-15, 1-1e-15))
final_results = pd.DataFrame({'LogLoss': log_losses, 'Names': model_names,
'Target': targets, 'Prediction': np.argmax(preds, axis=1)})
 final_results['Accuracy'] = final_results['Target'] == final_results['Prediction']
return final_results[['LogLoss', 'Names']].groupby('Names' ).mean().reset_index()<compute_train_metric>
X = train_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 )
ensembling_results = test_ensembling()<save_to_csv>
scaler = StandardScaler()
X = scaler.fit_transform(X )
vectorizer=TfidfVectorizer(token_pattern=r'\w{1,}', sublinear_tf=True, ngram_range=(1,2))
clf = VotingClassifier(previous_best, voting='soft', weights=[3,3,3,1,1])
 X_train = vectorizer.fit_transform(csv.text.values)
 y_train = csv.author.apply(lambda auth: authors_dict[auth] ).values
clf.fit(X_train, y_train)
 test = pd.read_csv('.. /input/test.csv', index_col=0)
 X_test = vectorizer.transform(test.text.values)
 result = clf.predict_proba(X_test)
 pd.DataFrame(result, index=test.index, columns=authors ).to_csv('tfidf_results.csv' )<feature_engineering>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.15, random_state=12345 )
for df in [csv, test]:
df['split'] = df.text.apply(nltk.word_tokenize)
 df['PosTag'] = [[x[1] for x in nltk.pos_tag(y)] for y in df.split]<load_from_csv>
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
tags_exp = nltk.data.load('help/tagsets/upenn_tagset.pickle')
 tags = pd.Series([x for y in csv.PosTag for x in y] ).value_counts().to_frame()
tags.columns = ['Count']
tags['Explanation'] = [tags_exp[i][0] for i in tags.index]<merge>
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=10)
 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=100, batch_size=bsz,verbose=1)
 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
tags['Percentage'] = tags['Count'] / tags['Count'].sum()
for aut in authors:
these_tags = pd.Series([x for y in csv.loc[csv.author==aut, 'PosTag'] for x in y] ).value_counts().to_frame()
these_tags.columns = ['Percentage_' + aut]
these_tags.iloc[:,0] = these_tags.iloc[:,0] / these_tags.iloc[:,0].sum()
tags = pd.merge(tags, these_tags, left_index=True, right_index=True, how='left' ).fillna(0 )<define_variables>
losses = []
models = []
crps_csv = []
s_time = time.time()
for k in range(2):
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[tr_inds],y[tr_inds]
val_x,val_y = X[val_inds],y[val_inds]
model,crps = 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))
def 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
max_diff = np.max(tags[['Percentage_'+x for x in ['MWS', 'HPL', 'EAP']]].values - np.expand_dims(tags['Percentage'].values, 1))
print('The maximum difference between any author percentage and the overall is {:.3f} percentage points'.format(
 max_diff*100))
tags.head()<define_variables>
print("mean crps is %f"%np.mean(crps_csv))
<define_variables><EOS> 
%%time
if TRAIN_OFFLINE==False:
env = nflrush.make_env()
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in iter_test:
basetable = create_features(test_df, deploy=True)
 basetable.drop(['GameId','PlayId'], axis=1, inplace=True)
 scaled_basetable = scaler.transform(basetable)
 y_pred = predict(scaled_basetable)
 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()