Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<init_hyperparams>
sns.set_style('darkgrid')
 mpl.rcParams['figure.figsize'] = [15,10]
ema_momentum_in_session=0.75
ema_momentum_between_session=0.75
class data_logger(object):
def __init__(self,event_id_to_args):
self.event_id_to_args=event_id_to_args
self.installation_status={}
def log_data(self,session):
session_status={}
update_method={}
do_count_ids=set()
for event_id,data_str in zip(session['event_id'],session['event_data']):
args=self.event_id_to_args.get(event_id)
 if args is None:
continue
event_data=json.loads(data_str)
 for arg_pair in args:
if arg_pair[2]=='count_true' or arg_pair[2]=='count_false':
do_count_ids.add(event_id)
 continue
new_val=event_data[arg_pair[0]]
if arg_pair[1] not in session_status:
if arg_pair[2]=='mean':
session_status[arg_pair[1]]=[]
else:
session_status[arg_pair[1]]=new_val
else:
if arg_pair[2]=="ema":
session_status[arg_pair[1]]=ema_momentum_in_session*session_status[arg_pair[1]]+(1-ema_momentum_in_session)*new_val
elif arg_pair[2]=='max':
session_status[arg_pair[1]]=max(session_status[arg_pair[1]],new_val)
 elif arg_pair[2]=='min':
session_status[arg_pair[1]]=min(session_status[arg_pair[1]],new_val)
 elif arg_pair[2]=='sum':
session_status[arg_pair[1]]=session_status[arg_pair[1]]+new_val
elif arg_pair[2]=='mean':
session_status[arg_pair[1]].append(new_val)
 else:
raise NotImplementedError
update_method[arg_pair[1]]=arg_pair[3]
for count_id in do_count_ids:
target_df=session[session.event_id==count_id]
args=self.event_id_to_args.get(count_id)
 num_true=0
num_false=0
save_key=""
for arg_pair in args:
if arg_pair[2]=='count_true':
num_true=target_df['event_data'].str.contains('true' ).sum()
session_status[arg_pair[1]]=num_true
update_method[arg_pair[1]]=arg_pair[3]
save_key=arg_pair[1]
elif arg_pair[2]=='count_false':
num_false=target_df['event_data'].str.contains('false' ).sum()
session_status[arg_pair[1]]=num_false
update_method[arg_pair[1]]=arg_pair[3]
session_status['_'.join(save_key.split("_")[:-2]+['accuracy'])]=num_true/(num_false+num_true)
 update_method['_'.join(save_key.split("_")[:-2]+['accuracy'])]="ema"
for key in session_status.keys() :
if type(session_status[key])is list:
session_status[key]=np.mean(session_status[key])
 if key not in self.installation_status:
if update_method[key]=='mean':
self.installation_status[key]=[]
else:
self.installation_status[key]=session_status[key]
else:
if update_method[key]=='ema':
self.installation_status[key]=ema_momentum_between_session*self.installation_status[key]+\ 
(1-ema_momentum_between_session)*session_status[key]
elif update_method[key]=='max':
self.installation_status[key]=max(self.installation_status[key],session_status[key])
 elif update_method[key]=='min':
self.installation_status[key]=min(self.installation_status[key],session_status[key])
 elif update_method[key]=='sum':
self.installation_status[key]=self.installation_status[key]+session_status[key]
elif update_method[key]=='mean':
self.installation_status[key].append(session_status[key])
 else:
raise NotImplementedError
def get_data(self):
return {key: self.installation_status[key] if type(self.installation_status[key])is not list else np.mean(self.installation_status[key])\
for key in self.installation_status}<define_variables>
train = pd.read_csv('.. /input/nfl-big-data-bowl-2020/train.csv', dtype={'WindSpeed': 'object'} )
tree_top_city = {
"Tree Top City - Level 1": 1,
"Ordering Spheres": 2,
"All Star Sorting": 3,
"Costume Box": 4,
"Fireworks(Activity)": 5,
"12 Monkeys": 6,
"Tree Top City - Level 2": 7,
"Flower Waterer(Activity)": 8,
"Pirate's Tale": 9,
"Mushroom Sorter(Assessment)": 10,
"Air Show": 11,
"Treasure Map": 12,
"Tree Top City - Level 3": 13,
"Crystals Rule": 14,
"Rulers": 15,
"Bug Measurer(Activity)": 16,
"Bird Measurer(Assessment)": 17,
}
magma_peak = {
"Magma Peak - Level 1": 1,
"Sandcastle Builder(Activity)": 2,
"Slop Problem": 3,
"Scrub-A-Dub": 4,
"Watering Hole(Activity)": 5,
"Magma Peak - Level 2": 6,
"Dino Drink": 7,
"Bubble Bath": 8,
"Bottle Filler(Activity)": 9,
"Dino Dive": 10,
"Cauldron Filler(Assessment)": 11,
}
crystal_caves = {
"Crystal Caves - Level 1": 1,
"Chow Time": 2,
"Balancing Act": 3,
"Chicken Balancer(Activity)": 4,
"Lifting Heavy Things": 5,
"Crystal Caves - Level 2": 6,
"Honey Cake": 7,
"Happy Camel": 8,
"Cart Balancer(Assessment)": 9,
"Leaf Leader": 10,
"Crystal Caves - Level 3": 11,
"Heavy, Heavier, Heaviest": 12,
"Pan Balance": 13,
"Egg Dropper(Activity)": 14,
"Chest Sorter(Assessment)": 15,
}<define_variables>
train['PlayId'].value_counts()
clip_time = {'Welcome to Lost Lagoon!':19,'Tree Top City - Level 1':17,'Ordering Spheres':61, 'Costume Box':61,
'12 Monkeys':109,'Tree Top City - Level 2':25, 'Pirate's Tale':80, 'Treasure Map':156,'Tree Top City - Level 3':26,
'Rulers':126, 'Magma Peak - Level 1':20, 'Slop Problem':60, 'Magma Peak - Level 2':22, 'Crystal Caves - Level 1':18,
'Balancing Act':72, 'Lifting Heavy Things':118,'Crystal Caves - Level 2':24, 'Honey Cake':142, 'Crystal Caves - Level 3':19,
'Heavy, Heavier, Heaviest':61}<define_variables>
off_form = train['OffenseFormation'].unique()
train['OffenseFormation'].value_counts()
def get_data(user_sample, test_set=False):
user_activities_count = {'Clip':0, 'Activity': 0, 'Assessment': 0, 'Game':0}
accuracy_groups = {"acc_group_0":0, "acc_group_1":0, "acc_group_2":0, "acc_group_3":0}
game_time_dict = {'Clip_gametime':0, 'Game_gametime':0, 'Activity_gametime':0, 'Assessment_gametime':0}
accumulated_accuracy_group = 0
accumulated_accuracy = 0
accumulated_correct_attempts = 0
accumulated_uncorrect_attempts = 0
accumulated_actions = 0
last_world='NONE'
last_activity_type='Clip'
time_last_activity=None
give_up={"give_up_"+assess:0 for assess in assess_titles}
durations ={'ema_duration_'+eve:0 for eve in list_of_user_activities}
last_accuracy_title = {'acc_' + title: -1 for title in assess_titles}
world_count={world:0 for world in list_of_worlds}
event_code_count = {ev: 0 for ev in list_of_event_code}
event_id_count = {eve: 0 for eve in list_of_event_id}
title_count = {eve: 0 for eve in list_of_user_activities}
counter = 0
assess_durations=[]
tree_top_city_list = []
magma_peak_list = []
crystal_caves_list = []
clip_durations = []
installation_logger=data_logger(event_id_to_args)
 all_assessments = []
for session_id, session in user_sample.groupby('game_session', sort=False):
session_type = session['type'].iloc[0]
session_title = session['title'].iloc[0]
session_world = session['world'].iloc[0]
if session_type == 'Clip':
clip_durations.append(( clip_time[session_title]))
if(session_type == 'Assessment')&(test_set or len(session)>1):
features={}
features['installation_id'] = session['installation_id'].iloc[-1]
features['session_id'] = session_id
features['session_title'] = session_title
features['session_world'] = session_world
all_attempts = session.query(f'event_code == {win_code[session_title]}')
 true_attempts = all_attempts['event_data'].str.contains('true' ).sum()
false_attempts = all_attempts['event_data'].str.contains('false' ).sum()
accuracy = true_attempts/(true_attempts+false_attempts)if(true_attempts+false_attempts)!= 0 else 0
if accuracy == 0:
features['accuracy_group'] = 0
elif accuracy == 1:
features['accuracy_group'] = 3
elif accuracy == 0.5:
features['accuracy_group'] = 2
else:
features['accuracy_group'] = 1
features.update(accuracy_groups.copy())
 accuracy_groups["acc_group_"+str(features['accuracy_group'])] += 1
features['accumulated_correct_attempts'] = accumulated_correct_attempts
features['accumulated_uncorrect_attempts'] = accumulated_uncorrect_attempts
features['ratio']=accumulated_correct_attempts/(accumulated_correct_attempts+accumulated_uncorrect_attempts)if(accumulated_correct_attempts+accumulated_uncorrect_attempts)!=0 else 0
accumulated_correct_attempts += true_attempts
accumulated_uncorrect_attempts += false_attempts
features['accumulated_accuracy'] = accumulated_accuracy/counter if counter > 0 else 0
accumulated_accuracy += accuracy
features['accumulated_accuracy_group'] = accumulated_accuracy_group/counter if counter > 0 else 0
accumulated_accuracy_group += features['accuracy_group']
features.update(last_accuracy_title.copy())
 last_accuracy_title['acc_' + session_title] = accuracy
features['accumulated_actions'] = accumulated_actions
features['last_world']=last_world
features['last_activity_type']=last_activity_type
features['time_to_last_activity']=(session.iloc[0, 2]-time_last_activity ).seconds if time_last_activity is not None else 0
features.update(event_code_count.copy())
 features.update(installation_logger.get_data())
 features.update(title_count.copy())
 features.update(durations.copy())
 features.update(user_activities_count.copy())
 features.update(event_id_count.copy())
 features.update(give_up.copy())
 features.update(world_count.copy())
 features.update(game_time_dict.copy())
 if assess_durations == []:
features['assess_duration_mean'] = 0
else:
features['assess_duration_mean'] = np.mean(assess_durations)
 assess_durations.append(( session.iloc[-1, 2] - session.iloc[0, 2] ).seconds)
 if true_attempts+false_attempts==0:
give_up["give_up_"+session_title]+=1
if tree_top_city_list == []:
features['tree_top_city_max'] = 0
features['tree_top_city_cnt'] = 0
features['tree_top_city_cover'] = 0
else:
features['tree_top_city_max'] = np.max(tree_top_city_list)
 features['tree_top_city_cnt'] = len(tree_top_city_list)
 features['tree_top_city_cover'] = float(len(set(tree_top_city_list)))/ len(tree_top_city)
 if magma_peak_list == []:
features['magma_peak_max'] = 0
features['magma_peak_cnt'] = 0
features['magma_peak_cover'] = 0
else:
features['magma_peak_max'] = np.max(magma_peak_list)
 features['magma_peak_cnt'] = len(magma_peak_list)
 features['magma_peak_cover'] = float(len(set(magma_peak_list)))/ len(magma_peak)
 if crystal_caves_list == []:
features['crystal_caves_max'] = 0
features['crystal_caves_cnt'] = 0
features['crystal_caves_cover'] = 0
else:
features['crystal_caves_max'] = np.max(crystal_caves_list)
 features['crystal_caves_cnt'] = len(crystal_caves_list)
 features['crystal_caves_cover'] = float(len(set(crystal_caves_list)))/ len(crystal_caves)
 if session_title in tree_top_city:
last_game = tree_top_city[session_title] - 1
if last_game in tree_top_city_list:
features['played_last_game'] = 1
else:
features['played_last_game'] = 0
elif session_title in magma_peak:
last_game = magma_peak[session_title] - 1
if last_game in magma_peak_list:
features['played_last_game'] = 1
else:
features['played_last_game'] = 0
elif session_title in crystal_caves:
last_game = crystal_caves[session_title] - 1
if last_game in crystal_caves_list:
features['played_last_game'] = 1
else:
features['played_last_game'] = 0
else:
features['played_last_game'] = 0
if clip_durations == []:
features['Clip_duration_mean'] = 0
features['Clip_duration_std'] = 0
else:
features['Clip_duration_mean'] = np.mean(clip_durations)
 features['Clip_duration_std'] = np.std(clip_durations)
 if test_set:
all_assessments.append(features)
 elif true_attempts+false_attempts > 0:
all_assessments.append(features)
 counter += 1
installation_logger.log_data(session)
 def update_counters(counter: dict, col: str):
num_of_session_count = Counter(session[col])
 for k in num_of_session_count.keys() :
if counter.get(k)is not None:
counter[k] += num_of_session_count[k]
return counter
event_code_count = update_counters(event_code_count, "event_code")
 event_id_count = update_counters(event_id_count, "event_id")
 title_count[session_title]+=1
session_duration=(session.iloc[-1, 2] - session.iloc[0, 2] ).seconds
durations['ema_duration_'+session_title]= session_duration if durations['ema_duration_'+session_title]==0 else(0.2*session_duration+0.8*durations['ema_duration_'+session_title])
 accumulated_actions += len(session)
 user_activities_count[session_type] += 1
world_count[session_world]+=1
last_world = session_world
last_activity_type = session_type
time_last_activity= session.iloc[-1, 2]
game_time_dict[session_type+'_gametime'] =(game_time_dict[session_type+'_gametime'] +(session['game_time'].iloc[-1]/1000.0)) /2.0
if session_title in tree_top_city:
tree_top_city_list.append(tree_top_city[session_title])
 if session_title in magma_peak:
magma_peak_list.append(magma_peak[session_title])
 if session_title in crystal_caves:
crystal_caves_list.append(crystal_caves[session_title])
 if test_set:
return all_assessments[-1]
return all_assessments
def get_train_and_test(train, test):
compiled_train = []
compiled_test = []
for i,(ins_id, user_sample)in enumerate(tqdm(train.groupby('installation_id', sort = False))):
compiled_train += get_data(user_sample)
 for ins_id, user_sample in tqdm(test.groupby('installation_id', sort = False)) :
test_data = get_data(user_sample, test_set = True)
 compiled_test.append(test_data)
 reduce_train = pd.DataFrame(compiled_train)
 reduce_test = pd.DataFrame(compiled_test)
 return reduce_train, reduce_test
reduce_train, reduce_test= get_train_and_test(train, test)
 reduce_train.columns = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in reduce_train.columns]
reduce_test.columns = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in reduce_test.columns]
assess_titles=["".join(c if c.isalnum() else "_" for c in str(x)) for x in assess_titles] 
<data_type_conversions>
train = pd.concat([train.drop(['OffenseFormation'], axis=1), pd.get_dummies(train['OffenseFormation'], prefix='Formation')], axis=1)
 dummy_col = train.columns
type_enc={'Clip':0, 'Activity': 1, 'Assessment': 2, 'Game':3}
reduce_train['session_title']=reduce_train['session_title'].replace(title_enc ).astype(int)
 reduce_train['session_world']=reduce_train['session_world'].replace(world_enc ).astype(int)
 reduce_train['last_world']=reduce_train['last_world'].replace(world_enc ).astype(int)
 reduce_train['last_activity_type']=reduce_train['last_activity_type'].replace(type_enc ).astype(int)
 reduce_test['session_title']=reduce_test['session_title'].replace(title_enc ).astype(int)
 reduce_test['session_world']=reduce_test['session_world'].replace(world_enc ).astype(int)
 reduce_test['last_world']=reduce_test['last_world'].replace(world_enc ).astype(int)
 reduce_test['last_activity_type']=reduce_test['last_activity_type'].replace(type_enc ).astype(int )<data_type_conversions>
train['GameClock'].value_counts()
freq=reduce_train['session_title'].value_counts()
frequency_enc=dict(zip(freq.index,freq))
reduce_train['session_title']=reduce_train['session_title'].replace(frequency_enc ).astype(int)
 reduce_test['session_title']=reduce_test['session_title'].replace(frequency_enc ).astype(int)
 reduce_train_true=reduce_train.copy()
reduce_test_true=reduce_test.copy()<set_options>
train['GameClock'] = train['GameClock'].apply(strtoseconds )
del train,test
gc.collect()<load_from_csv>
train['PlayerHeight'] = train['PlayerHeight'].apply(lambda x: 12*int(x.split('-')[0])+int(x.split('-')[1]))
def read_data() :
if DEBUG:
print('Reading train.csv file.... ')
 train = pd.read_csv(dirs+'train.csv', nrows=100000)
 print('Training.csv file have {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Reading test.csv file.... ')
 test = pd.read_csv(dirs+'test.csv', nrows=100000)
 print('Test.csv file have {} rows and {} columns'.format(test.shape[0], test.shape[1]))
else:
print('Reading train.csv file.... ')
 train = pd.read_csv(dirs+'train.csv')
 print('Training.csv file have {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Reading test.csv file.... ')
 test = pd.read_csv(dirs+'test.csv')
 print('Test.csv file have {} rows and {} columns'.format(test.shape[0], test.shape[1]))
print('Reading train_labels.csv file.... ')
 train_labels = pd.read_csv(dirs+'train_labels.csv')
 print('Train_labels.csv file have {} rows and {} columns'.format(train_labels.shape[0], train_labels.shape[1]))
print('Reading specs.csv file.... ')
 specs = pd.read_csv(dirs+'specs.csv')
 print('Specs.csv file have {} rows and {} columns'.format(specs.shape[0], specs.shape[1]))
print('Reading sample_submission.csv file.... ')
 sample_submission = pd.read_csv(dirs+'sample_submission.csv')
 print('Sample_submission.csv file have {} rows and {} columns'.format(sample_submission.shape[0], sample_submission.shape[1]))
return train, test, train_labels, specs, sample_submission
train, test, train_labels, specs, sample_submission = read_data()
train = train[train.installation_id.isin(train_labels.installation_id.unique())].reset_index(drop=True)
 list_of_event_args = list(set(specs['args'].unique()))
event_args_map = dict(zip(list_of_event_args, np.arange(len(list_of_event_args))))
specs["args"]=specs["args"].map(event_args_map)
 list_of_event_info = list(set(specs['info'].unique()))
event_info_map = dict(zip(list_of_event_info, np.arange(len(list_of_event_info))))
specs["info"]=specs["info"].map(event_info_map)
 args_list=specs["args"].value_counts().add_prefix('args_' ).index.tolist()
args_label=dict(zip(np.arange(len(args_list)) , args_list))
info_list=specs["info"].value_counts().add_prefix('info_' ).index.tolist()
info_label=dict(zip(np.arange(len(info_list)) , info_list))
train=pd.merge(train,specs,on=["event_id"],how="left")
 test=pd.merge(test,specs,on=["event_id"],how="left")
 print(train.shape )<count_values>
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"))
def cnt_miss(df):
cnt = 0
for e in range(len(df)) :
x = df['event_data'].iloc[e]
y = json.loads(x)['misses']
cnt += y
return cnt
def update_counters(counter: dict, col: str,session):
num_of_session_count = Counter(session[col])
 for k in num_of_session_count.keys() :
x = k
counter[x] += num_of_session_count[k]
return counter
def update_counters_event(counter: dict, col: str,session):
num_of_session_count = session[col].value_counts().add_prefix(col+'_' ).to_dict()
for k in num_of_session_count.keys() :
counter[k] += num_of_session_count[k]
return counter
def get_data123(user_sample,lab, test_set=False):
all_assessments=[]
compiled_data=[]
for i, session in user_sample.groupby('game_session', sort=False):
session_type = session['type'].iloc[0]
session_title = session['title'].iloc[0]
session_title_text = activities_labels[session_title]
Assessment_time=session['timestamp'].iloc[0]
event_code_count: Dict[str, int] = {ev: 0 for ev in list_of_event_code}
event_id_count: Dict[str, int] = {eve: 0 for eve in list_of_event_id}
event_info_count: Dict[str, int] = {eve: 0 for eve in info_list}
if session_type==lab:
features = {}
features["game_title"]=session_title
features["game_event_count"]=session['event_count'].iloc[-1]
features["game_game_time"]=session['game_time'].iloc[-1]
event_code_count = update_counters(event_code_count, "event_code",session)
 event_info_count = update_counters_event(event_info_count, "info",session)
 features.update(event_code_count.copy())
 features.update(event_info_count.copy())
 compiled_data.append(features)
 if(session_type == 'Assessment')&(test_set or len(session)>1):
all_attempts = session.query(f'event_code == {win_code[session_title]}')
 true_attempts = all_attempts['event_data'].str.contains('true' ).sum()
false_attempts = all_attempts['event_data'].str.contains('false' ).sum()
accuracy = true_attempts/(true_attempts+false_attempts)if(true_attempts+false_attempts)!= 0 else 0
if accuracy == 0:
accuracy_group = 0
elif accuracy == 1:
accuracy_group = 3
elif accuracy == 0.5:
accuracy_group = 2
else:
accuracy_group = 1
if test_set:
if true_attempts+false_attempts > 0:
reduce_game = pd.DataFrame(compiled_data)
 reduce_game['installation_id'] = session['installation_id'].iloc[-1]
reduce_game["game_session"]=session['game_session'].iloc[-1]
reduce_game["session_title"]=session_title
reduce_game["true_attempts"]=true_attempts
reduce_game["false_attempts"]=false_attempts
reduce_game["accuracy"]=accuracy
reduce_game["accuracy_group"]=accuracy_group
all_assessments.append(reduce_game)
 elif len(session)==1:
reduce_game = pd.DataFrame(compiled_data)
 reduce_game['installation_id'] = session['installation_id'].iloc[-1]
reduce_game["game_session"]=session['game_session'].iloc[-1]
reduce_game["session_title"]=session_title
reduce_game["true_attempts"]=666
reduce_game["false_attempts"]=666
reduce_game["accuracy"]=666
reduce_game["accuracy_group"]=666
all_assessments.append(reduce_game)
 elif true_attempts+false_attempts > 0:
reduce_game = pd.DataFrame(compiled_data)
 reduce_game['installation_id'] = session['installation_id'].iloc[-1]
reduce_game["game_session"]=session['game_session'].iloc[-1]
reduce_game["session_title"]=session_title
reduce_game["true_attempts"]=true_attempts
reduce_game["false_attempts"]=false_attempts
reduce_game["accuracy"]=accuracy
reduce_game["accuracy_group"]=accuracy_group
all_assessments.append(reduce_game)
 return all_assessments<count_unique_values>
train['TimeDelta'] = train.apply(lambda row:(row['TimeHandoff'] - row['TimeSnap'] ).total_seconds() , axis=1 )
compiled_data = []
for i,(ins_id, user_sample)in tqdm(enumerate(train.groupby('installation_id', sort=False)) , total=train.installation_id.nunique() , desc='Installation_id', position=0):
compiled_data += get_data123(user_sample,"Game")
 reduce_train = pd.concat(compiled_data)
 reduce_train.shape
compiled_data = []
for ins_id, user_sample in tqdm(test.groupby('installation_id', sort=False), total=test.installation_id.nunique() , desc='Installation_id', position=0):
compiled_data += get_data123(user_sample,"Game", test_set=True)
 reduce_test_all = pd.concat(compiled_data)
 reduce_test_all.shape
reduce_test=reduce_test_all[reduce_test_all["accuracy"]==666].copy().reset_index(drop=True)
 reduce_train=pd.concat([reduce_train,reduce_test_all[reduce_test_all["accuracy"]!=666]] ).reset_index(drop=True)
 reduce_train.shape<define_variables>
train['PlayerBirthDate'] = train['PlayerBirthDate'].apply(lambda x: datetime.datetime.strptime(x, "%m/%d/%Y"))
feat=[f for f in reduce_train.columns if f not in ['installation_id', 'game_session','true_attempts', 'false_attempts', 'accuracy', 'accuracy_group']]
print(feat )<categorify>
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 )
@jit
def qwk(a1, a2):

 max_rat = 3
a1 = np.asarray(a1, dtype=int)
 a2 = np.asarray(a2, dtype=int)
 hist1 = np.zeros(( max_rat + 1,))
hist2 = np.zeros(( max_rat + 1,))
o = 0
for k in range(a1.shape[0]):
i, j = a1[k], a2[k]
hist1[i] += 1
hist2[j] += 1
o +=(i - j)*(i - j)
 e = 0
for i in range(max_rat + 1):
for j in range(max_rat + 1):
e += hist1[i] * hist2[j] *(i - j)*(i - j)
 e = e / a1.shape[0]
return 1 - o / e
def eval_qwk(y_true, y_pred):
coeff=[1.12934881,1.69659649,2.204893]
y_pred[y_pred <= coeff[0]] = 0
y_pred[np.where(np.logical_and(y_pred > coeff[0], y_pred <= coeff[1])) ] = 1
y_pred[np.where(np.logical_and(y_pred > coeff[1], y_pred <= coeff[2])) ] = 2
y_pred[y_pred > coeff[2]] = 3
return qwk(y_true, y_pred)
 def lgb_model(reduce_train,reduce_test,feature,random_state):
params = {
'boosting_type': 'gbdt',
'metric': 'rmse',
'objective': 'regression',
'eval_metric': 'cappa',
'num_threads':-1,
'seed': random_state,
'learning_rate':0.05,
'max_depth': 11,
'lambda_l1': 1,
'lambda_l2': 1,
'bagging_fraction': 0.75,
'bagging_freq': 5,
'colsample_bytree':0.6,
'verbose': 100
}
early_stop = 50
verbose_eval = 100
num_rounds = 10000
n_splits = 5
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 models = []
scores=[]
y_trian = reduce_train['accuracy_group']
oof_train = np.zeros(( reduce_train.shape[0]))
oof_test = np.zeros(( reduce_test.shape[0]))
for train_index,valid_index in kf.split(reduce_train, y_trian):
train_features = reduce_train.loc[train_index]
train_target = y_trian.loc[train_index]
val_features = reduce_train.loc[valid_index]
val_target = y_trian.loc[valid_index]
X_train = train_features[feature].values
X_val = val_features[feature].values
d_train = lgb.Dataset(X_train, label=train_target)
 d_valid = lgb.Dataset(X_val, label=val_target)
 watchlist = [d_train, d_valid]
print('training LGB:')
 model = lgb.train(params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval,
early_stopping_rounds=early_stop)
 val_pred = model.predict(X_val, num_iteration=model.best_iteration)
 test_pred = model.predict(reduce_test[feature].values, num_iteration=model.best_iteration)
 scores.append(eval_qwk(val_target,copy.deepcopy(val_pred)))
 print(scores)
 oof_train[valid_index] = val_pred
oof_test += test_pred/n_splits
print(np.mean(scores))
return oof_train,oof_test
print(reduce_train.shape,reduce_test.shape)
 oof_train_one,oof_test_one=lgb_model(reduce_train.reset_index(drop=True),reduce_test.copy().reset_index(drop=True),feat,50 )<prepare_output>
train = train.drop(['TimeHandoff', 'TimeSnap', 'PlayerBirthDate'], axis=1 )
train_feature_score = reduce_train[['installation_id', 'game_session']].copy()
train_feature_score['score'] = oof_train_one
test_feature_score = reduce_test[['installation_id', 'game_session']].copy()
test_feature_score['score'] = oof_test_one
feature_score = pd.concat([train_feature_score, test_feature_score])
 feature_agg=feature_score.groupby(["game_session","installation_id"] ).agg({'score': ['count','mean', 'sum', 'max','min','var']} ).reset_index()
feature_agg.columns=["game_session","installation_id",'score_count','score_mean', 'score_sum', 'score_max', 'score_min', 'score_var']
print(feature_agg.shape)
 feature_agg.head()<drop_column>
train['WindSpeed'].value_counts()
del train_feature_score,test_feature_score,feature_score
del reduce_train,reduce_test,reduce_test_all,compiled_data
gc.collect()<count_unique_values>
train['WindSpeed'] = train['WindSpeed'].apply(lambda x: x.lower().replace('mph', '' ).strip() if not pd.isna(x)else x )
compiled_data = []
for i,(ins_id, user_sample)in tqdm(enumerate(train.groupby('installation_id', sort=False)) , total=train.installation_id.nunique() , desc='Installation_id', position=0):
compiled_data += get_data123(user_sample,"Activity")
 reduce_train = pd.concat(compiled_data)
 compiled_data = []
for ins_id, user_sample in tqdm(test.groupby('installation_id', sort=False), total=test.installation_id.nunique() , desc='Installation_id', position=0):
compiled_data += get_data123(user_sample,"Activity", test_set=True)
 reduce_test_all = pd.concat(compiled_data)
 reduce_test=reduce_test_all[reduce_test_all["accuracy"]==666].copy().reset_index(drop=True)
 reduce_train=pd.concat([reduce_train,reduce_test_all[reduce_test_all["accuracy"]!=666]] ).reset_index(drop=True)
 reduce_train.shape
feat=[f for f in reduce_train.columns if f not in ['installation_id', 'game_session','true_attempts', 'false_attempts', 'accuracy', 'accuracy_group']]
print(feat)
 print(reduce_train.shape,reduce_test.shape)
 oof_train_one,oof_test_one=lgb_model(reduce_train.reset_index(drop=True),reduce_test.copy().reset_index(drop=True),feat,50)
 train_feature_score = reduce_train[['installation_id', 'game_session']].copy()
train_feature_score['score'] = oof_train_one
test_feature_score = reduce_test[['installation_id', 'game_session']].copy()
test_feature_score['score'] = oof_test_one
feature_score = pd.concat([train_feature_score, test_feature_score])
 feature_agg_Activity=feature_score.groupby(["game_session","installation_id"] ).agg({'score': ['count','mean', 'sum', 'max','min','var']} ).reset_index()
feature_agg_Activity.columns=["game_session","installation_id",'score_countAct','score_meanAct', 'score_sumAct', 'score_maxAct', 'score_minAct', 'score_varAct']
print(feature_agg_Activity.shape)
 feature_agg_Activity.head()<drop_column>
train['WindSpeed'].value_counts()
del train_feature_score,test_feature_score,feature_score
del reduce_train,reduce_test,reduce_test_all,compiled_data
gc.collect()<merge>
train['WindSpeed'] = train['WindSpeed'].apply(lambda x:(int(x.split('-')[0])+int(x.split('-')[1])) /2 if not pd.isna(x)and '-' in x else x)
 train['WindSpeed'] = train['WindSpeed'].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 )
reduce_train=pd.merge(reduce_train_true,feature_agg,left_on=["session_id","installation_id"],right_on=["game_session","installation_id"],how="left")
 reduce_train=pd.merge(reduce_train,feature_agg_Activity,left_on=["session_id","installation_id"],right_on=["game_session","installation_id"],how="left")
 reduce_test=pd.merge(reduce_test_true,feature_agg,left_on=["session_id","installation_id"],right_on=["game_session","installation_id"],how="left")
 reduce_test=pd.merge(reduce_test,feature_agg_Activity,left_on=["session_id","installation_id"],right_on=["game_session","installation_id"],how="left")
 reduce_test=reduce_test.drop(['game_session_y','game_session_x'],axis=1)
 reduce_train.to_csv('reduce_train.csv', index=False)
 reduce_test.to_csv('reduce_test.csv', index=False)
 reduce_train=reduce_train[reduce_test.columns]
ajusted_test=reduce_test.copy()
to_exclude=[]
for feature in ajusted_test.columns:
if feature not in ['installation_id','session_id','accuracy_group','session_title','session_world','last_world','last_activity_type','same_world_with_last']+\
["give_up_"+assess for assess in assess_titles]+['acc_' + title for title in assess_titles]:
data = reduce_train[feature]
train_mean = data.mean()
data = ajusted_test[feature]
test_mean = data.mean()
try:
ajust_factor = train_mean / test_mean
if ajust_factor > 5 or ajust_factor < 0.2:
to_exclude.append(feature)
 print(feature, train_mean, test_mean)
 else:
ajusted_test[feature] *= ajust_factor
except:
to_exclude.append(feature)
 print(feature, train_mean, test_mean)
<categorify>
def str_to_float(txt):
try:
return float(txt)
 except:
return -1
@jit
def qwk(a1, a2):

 max_rat = 3
a1 = np.asarray(a1, dtype=int)
 a2 = np.asarray(a2, dtype=int)
 hist1 = np.zeros(( max_rat + 1,))
hist2 = np.zeros(( max_rat + 1,))
o = 0
for k in range(a1.shape[0]):
i, j = a1[k], a2[k]
hist1[i] += 1
hist2[j] += 1
o +=(i - j)*(i - j)
 e = 0
for i in range(max_rat + 1):
for j in range(max_rat + 1):
e += hist1[i] * hist2[j] *(i - j)*(i - j)
 e = e / a1.shape[0]
return 1 - o / e
def eval_qwk_lgb_regr(y_true, y_pred):

 y_pred[y_pred <= regression_thresholds[0]] = 0
y_pred[np.where(np.logical_and(y_pred >regression_thresholds[0], y_pred <= regression_thresholds[1])) ] = 1
y_pred[np.where(np.logical_and(y_pred > regression_thresholds[1], y_pred <= regression_thresholds[2])) ] = 2
y_pred[y_pred > regression_thresholds[2]] = 3
return 'cappa', qwk(y_true, y_pred), True
class LGBWrapper_regr(object):

 def __init__(self):
self.model = lgb.LGBMRegressor()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
if params['objective'] == 'regression':
eval_metric = eval_qwk_lgb_regr
else:
eval_metric = 'auc'
eval_set = [(X_train, y_train)]
eval_names = ['train']
self.model = self.model.set_params(**params)
 if X_valid is not None:
eval_set.append(( X_valid, y_valid))
eval_names.append('valid')
 if X_holdout is not None:
eval_set.append(( X_holdout, y_holdout))
eval_names.append('holdout')
 if 'cat_cols' in params.keys() :
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols)> 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = 'auto'
else:
categorical_columns = 'auto'
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_names=eval_names, eval_metric=eval_metric,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
categorical_feature=categorical_columns)
 self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict(self, X_test):
return self.model.predict(X_test, num_iteration=self.model.best_iteration_)
 class RegressorModel(object):

 def __init__(self, columns: list = None, model_wrapper=None,truncate_valid=False,seed=66):

 self.columns = columns
self.model_wrapper = model_wrapper
self.result_dict = {}
self.train_one_fold = False
self.preprocesser = None
self.truncate_valid=truncate_valid
self.truncate_seed=seed
def fit(self, X: pd.DataFrame, y,
X_holdout: pd.DataFrame = None, y_holdout=None,
folds=None,
params: dict = None,
eval_metric='rmse',
cols_to_drop: list = None,
preprocesser=None,
transformers: dict = None,
adversarial: bool = False,
plot: bool = True):

 if folds is None:
folds = KFold(n_splits=3, random_state=42)
 self.train_one_fold = True
self.columns = X.columns if self.columns is None else self.columns
self.feature_importances = pd.DataFrame(columns=['feature', 'importance'])
 self.trained_transformers = {k: [] for k in transformers}
self.transformers = transformers
self.models = []
self.folds_dict = {}
self.eval_metric = eval_metric
n_target = 1
self.oof = []
self.n_target = n_target
random.seed(self.truncate_seed)
 X = X[self.columns]
if X_holdout is not None:
X_holdout = X_holdout[self.columns]
if preprocesser is not None:
self.preprocesser = preprocesser
self.preprocesser.fit(X, y)
 X = self.preprocesser.transform(X, y)
 self.columns = X.columns.tolist()
if X_holdout is not None:
X_holdout = self.preprocesser.transform(X_holdout)
 for fold_n,(train_index, valid_index)in enumerate(folds.split(X, y, X['installation_id'])) :
if X_holdout is not None:
X_hold = X_holdout.copy()
else:
X_hold = None
self.folds_dict[fold_n] = {}
if params['verbose']:
print(f'Fold {fold_n + 1} started at {time.ctime() }')
 self.folds_dict[fold_n] = {}
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if self.truncate_valid:
truncated_index=[]
for iid in sorted(list(set(X_valid['installation_id']))):
list_ = list(X_valid.loc[X_valid['installation_id'] == iid].index)
 cur = random.choices(list_, k=1)[0]
truncated_index.append(cur)
 X_valid=X_valid.loc[truncated_index]
y_valid=y_valid.loc[truncated_index]
if self.train_one_fold:
X_train = X[self.original_columns]
y_train = y
X_valid = None
y_valid = None
datasets = {'X_train': X_train, 'X_valid': X_valid, 'X_holdout': X_hold, 'y_train': y_train}
X_train, X_valid, X_hold = self.transform_(datasets, cols_to_drop)
 self.folds_dict[fold_n]['columns'] = X_train.columns.tolist()
model = copy.deepcopy(self.model_wrapper)
 if adversarial:
X_new1 = X_train.copy()
if X_valid is not None:
X_new2 = X_valid.copy()
elif X_holdout is not None:
X_new2 = X_holdout.copy()
X_new = pd.concat([X_new1, X_new2], axis=0)
 y_new = np.hstack(( np.zeros(( X_new1.shape[0])) , np.ones(( X_new2.shape[0]))))
X_train, X_valid, y_train, y_valid = train_test_split(X_new, y_new)
 model.fit(X_train, y_train, X_valid, y_valid, X_hold, y_holdout, params=params)
 self.folds_dict[fold_n]['scores'] = model.best_score_
if not adversarial:
self.oof.append([model.predict(X_valid ).reshape(-1),y_valid.values])
 fold_importance = pd.DataFrame(list(zip(X_train.columns, model.feature_importances_)) ,
columns=['feature', 'importance'])
 self.feature_importances = self.feature_importances.append(fold_importance)
 self.models.append(model)
 self.feature_importances['importance'] = self.feature_importances['importance'].astype(int)
 self.calc_scores_()
if plot:
fig, ax = plt.subplots(figsize=(16, 12))
plt.subplot(2, 2, 1)
 self.plot_feature_importance(top_n=20)
 plt.subplot(2, 2, 2)
 self.plot_metric()
if not self.truncate_valid:
plt.subplot(2, 2, 3)
 plt.hist(y.values.reshape(-1, 1)- self.oof)
 plt.title('Distribution of errors')
 plt.subplot(2, 2, 4)
 plt.hist(self.oof)
 plt.title('Distribution of oof predictions')
 def transform_(self, datasets, cols_to_drop):
for name, transformer in self.transformers.items() :
transformer.fit(datasets['X_train'], datasets['y_train'])
 datasets['X_train'] = transformer.transform(datasets['X_train'])
 if datasets['X_valid'] is not None:
datasets['X_valid'] = transformer.transform(datasets['X_valid'])
 if datasets['X_holdout'] is not None:
datasets['X_holdout'] = transformer.transform(datasets['X_holdout'])
 self.trained_transformers[name].append(transformer)
 if cols_to_drop is not None:
cols_to_drop = [col for col in cols_to_drop if col in datasets['X_train'].columns]
datasets['X_train'] = datasets['X_train'].drop(cols_to_drop, axis=1)
 if datasets['X_valid'] is not None:
datasets['X_valid'] = datasets['X_valid'].drop(cols_to_drop, axis=1)
 if datasets['X_holdout'] is not None:
datasets['X_holdout'] = datasets['X_holdout'].drop(cols_to_drop, axis=1)
 self.cols_to_drop = cols_to_drop
print("Dropping",len(set(cols_to_drop)) ,"columns First 10:",cols_to_drop[:10])
 return datasets['X_train'], datasets['X_valid'], datasets['X_holdout']
def calc_scores_(self):
print()
datasets = [k for k, v in [v['scores'] for k, v in self.folds_dict.items() ][0].items() if len(v)> 0]
self.scores = {}
for d in datasets:
scores = [v['scores'][d][self.eval_metric] for k, v in self.folds_dict.items() ]
print(f"CV mean score on {d}: {np.mean(scores):.4f} +/- {np.std(scores):.4f} std.")
 self.scores[d] = np.mean(scores)
 def predict(self, X_test, averaging: str = 'usual'):

 full_prediction = np.zeros(( X_test.shape[0],1))
if self.preprocesser is not None:
X_test = self.preprocesser.transform(X_test)
 for i in range(len(self.models)) :
X_t = X_test.copy()
for name, transformers in self.trained_transformers.items() :
X_t = transformers[i].transform(X_t)
 if self.cols_to_drop is not None:
cols_to_drop = [col for col in self.cols_to_drop if col in X_t.columns]
X_t = X_t.drop(cols_to_drop, axis=1)
 y_pred = self.models[i].predict(X_t[self.folds_dict[i]['columns']] ).reshape(-1, full_prediction.shape[1])
 if full_prediction.shape[0] != len(y_pred):
full_prediction = np.zeros(( y_pred.shape[0], 1))
if averaging == 'usual':
full_prediction += y_pred
elif averaging == 'rank':
full_prediction += pd.Series(y_pred ).rank().values
return full_prediction / len(self.models)
 def plot_feature_importance(self, drop_null_importance: bool = True, top_n: int = 10):

 top_feats = self.get_top_features(drop_null_importance, top_n)
 feature_importances = self.feature_importances.loc[self.feature_importances['feature'].isin(top_feats)]
feature_importances['feature'] = feature_importances['feature'].astype(str)
 top_feats = [str(i)for i in top_feats]
sns.barplot(data=feature_importances, x='importance', y='feature', orient='h', order=top_feats)
 plt.title('Feature importances')
 def get_top_features(self, drop_null_importance: bool = True, top_n: int = 10):

 grouped_feats = self.feature_importances.groupby(['feature'])['importance'].mean()
if drop_null_importance:
grouped_feats = grouped_feats[grouped_feats != 0]
return list(grouped_feats.sort_values(ascending=False ).index)[:top_n]
def plot_metric(self):

 full_evals_results = pd.DataFrame()
for model in self.models:
evals_result = pd.DataFrame()
for k in model.model.evals_result_.keys() :
evals_result[k] = model.model.evals_result_[k][self.eval_metric]
evals_result = evals_result.reset_index().rename(columns={'index': 'iteration'})
 full_evals_results = full_evals_results.append(evals_result)
 full_evals_results = full_evals_results.melt(id_vars=['iteration'] ).rename(columns={'value': self.eval_metric,
'variable': 'dataset'})
 sns.lineplot(data=full_evals_results, x='iteration', y=self.eval_metric, hue='dataset')
 plt.title('Training progress')
<compute_train_metric>
train['WindSpeed'] = train['WindSpeed'].apply(str_to_float )
class OptimizedRounder(object):

 def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):

 X_p = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3])
 return -qwk(y, X_p)
 def fit(self, X, y,initial_coef):

 loss_partial = partial(self._kappa_loss, X=X, y=y)
 self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):

 return pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3])
 def coefficients(self):

 return self.coef_['x']<count_values>
train['WindDirection'].value_counts()
del_cols = []
for col in reduce_train.columns.values:
if len(reduce_train[col].value_counts())==0:
del_cols.append(col)
 continue
counts = reduce_train[col].value_counts().iloc[0]
if(counts / reduce_train.shape[0])>= 0.9:
del_cols.append(col)
 print(str(len(del_cols)) + " features removed!")
 del_cols<compute_test_metric>
train.drop('WindDirection', axis=1, inplace=True )
counter = 0
to_remove = []
neglect_feat=['installation_id','session_id','accuracy_group']
features=[x for x in reduce_train.columns if x not in neglect_feat]
for feat_a in features:
for feat_b in features:
if feat_a != feat_b and feat_a not in to_remove and feat_b not in to_remove:
c = np.corrcoef(reduce_train[feat_a], reduce_train[feat_b])[0][1]
if c > 0.995:
counter += 1
to_remove.append(feat_b)
 print('{}: FEAT_A: {} FEAT_B: {} - Correlation: {}'.format(counter, feat_a, feat_b, c))<define_variables>
train['PlayDirection'].value_counts()
list_of_event_code = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in list_of_event_code]
list_of_event_id = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in list_of_event_id]
cols_to_drop = ['session_id', 'installation_id','accuracy_group',
'installation_session_count',
'installation_duration_mean',
'installation_title_nunique',
'installation_event_code_count_mean',
"4070",
]
cols_to_drop+=del_cols
cols_to_drop+=to_exclude
cols_to_drop+=to_remove
print(len(set(cols_to_drop)))
 cols_to_drop<init_hyperparams>
train['PlayDirection'] = train['PlayDirection'].apply(lambda x: x is 'right' )
categoricals=['session_title','session_world','last_world','last_activity_type']
params = {'n_estimators':2000,
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'subsample': 0.85,
'subsample_freq': 1,
'learning_rate': 0.01,
'feature_fraction': 0.75,
'max_depth': 10,
'num_leaves':31,
'min_data_in_leaf':50,
'cat_cols':categoricals,
'lambda_l1': 2,
'lambda_l2':9,
'verbose': 100,
'early_stopping_rounds': 200,
'eval_metric': 'cappa',
'seed':888,
'n_jobs':8
}
n_fold=5
folds = GroupKFold(n_splits=n_fold )<train_on_grid>
train['Team'] = train['Team'].apply(lambda x: x.strip() =='home' )
regression_thresholds=np.array([0.5,1.5,2.5])
 y = reduce_train['accuracy_group']
regressor_model1 = RegressorModel(model_wrapper=LGBWrapper_regr() ,truncate_valid=False,)
 regressor_model1.fit(X=reduce_train, y=y, folds=folds, params=params, preprocesser=None, transformers={},
eval_metric='cappa', cols_to_drop=cols_to_drop,plot=False )<statistical_test>
train['GameWeather'] = train['GameWeather'].str.lower()
indoor = "indoor"
train['GameWeather'] = train['GameWeather'].apply(lambda x: indoor if not pd.isna(x)and indoor in x else x)
 train['GameWeather'] = train['GameWeather'].apply(lambda x: x.replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly')if not pd.isna(x)else x)
 train['GameWeather'] = train['GameWeather'].apply(lambda x: x.replace('clear and sunny', 'sunny and clear')if not pd.isna(x)else x)
 train['GameWeather'] = train['GameWeather'].apply(lambda x: x.replace('skies', '' ).replace("mostly", "" ).strip() if not pd.isna(x)else x )
oof_predicts=np.concatenate([x[0] for x in regressor_model1.oof],axis=0)
 oof_y=np.concatenate([x[1] for x in regressor_model1.oof],axis=0)
 coefficients=[0.5,1.5,2.5]
for i in range(8):
optR = OptimizedRounder()
optR.fit(oof_predicts.reshape(-1,), oof_y,initial_coef=coefficients)
 coefficients = optR.coefficients()
oof_rounded=optR.predict(oof_predicts.reshape(-1,),coefficients)
 qwk_score=qwk(oof_y, oof_rounded)
 print("Round",i+1," Rounding Coefficients:",coefficients,"QWK score:",qwk_score)
 coef1=coefficients
qwkscore1=qwk_score
oof_predicts=np.concatenate([x[0] for x in regressor_model1.oof],axis=0)
 oof_y=np.concatenate([x[1] for x in regressor_model1.oof],axis=0)
 coefficients=[1.1,1.7,2.2]
for i in range(8):
optR = OptimizedRounder()
optR.fit(oof_predicts.reshape(-1,), oof_y,initial_coef=coefficients)
 coefficients = optR.coefficients()
oof_rounded=optR.predict(oof_predicts.reshape(-1,),coefficients)
 qwk_score=qwk(oof_y, oof_rounded)
 print("Round",i+1," Rounding Coefficients:",coefficients,"QWK score:",qwk_score)
 coef2=coefficients
qwkscore2=qwk_score
if qwkscore2>qwkscore1:
print("use coefficient2",qwkscore2)
 coefficients=coef2
else:
print("use coefficient1",qwkscore1)
 coefficients=coef1<train_on_grid>
weather_count = Counter()
for weather in train['GameWeather']:
if pd.isna(weather):
continue
for word in weather.split() :
weather_count[word]+=1
weather_count.most_common() [:15]
class Base_Model(object):
def __init__(self, train_df, test_df, features, categoricals=[], n_splits=5, verbose=True):
self.train_df = train_df
self.test_df = test_df
self.features = features
self.n_splits = n_splits
self.categoricals = categoricals
self.target = 'accuracy_group'
self.cv = self.get_cv()
self.verbose = verbose
self.params = self.get_params()
self.y_pred, self.score, self.model = self.fit()
def train_model(self, train_set, val_set):
raise NotImplementedError
def get_cv(self):
cv = StratifiedKFold(n_splits=self.n_splits, shuffle=True, random_state=42)
 return cv.split(self.train_df, self.train_df[self.target])
 def get_params(self):
raise NotImplementedError
def convert_dataset(self, x_train, y_train, x_val, y_val):
raise NotImplementedError
def convert_x(self, x):
return x
def fit(self):
oof_pred = np.zeros(( len(reduce_train),))
y_pred = np.zeros(( len(reduce_test),))
for fold,(train_idx, val_idx)in enumerate(self.cv):
x_train, x_val = self.train_df[self.features].iloc[train_idx], self.train_df[self.features].iloc[val_idx]
y_train, y_val = self.train_df[self.target][train_idx], self.train_df[self.target][val_idx]
train_set, val_set = self.convert_dataset(x_train, y_train, x_val, y_val)
 model = self.train_model(train_set, val_set)
 conv_x_val = self.convert_x(x_val)
 oof_pred[val_idx] = model.predict(conv_x_val ).reshape(oof_pred[val_idx].shape)
 x_test = self.convert_x(self.test_df[self.features])
 y_pred += model.predict(x_test ).reshape(y_pred.shape)/ self.n_splits
print('Partial score of fold {} is: {}'.format(fold, eval_qwk_lgb_regr(y_val, oof_pred[val_idx])[1]))
_, loss_score, _ = eval_qwk_lgb_regr(self.train_df[self.target], oof_pred)
 if self.verbose:
print('Our oof cohen kappa score is: ', loss_score)
 return y_pred, loss_score, model<train_model>
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
class Xgb_Model(Base_Model):
def train_model(self, train_set, val_set):
verbosity = 100 if self.verbose else 0
return xgb.train(self.params, train_set,
num_boost_round=5000, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=verbosity, early_stopping_rounds=100)
 def convert_dataset(self, x_train, y_train, x_val, y_val):
train_set = xgb.DMatrix(x_train, y_train)
 val_set = xgb.DMatrix(x_val, y_val)
 return train_set, val_set
def convert_x(self, x):
return xgb.DMatrix(x)
 def get_params(self):
params = {'colsample_bytree': 0.8,
'learning_rate': 0.01,
'max_depth': 10,
'subsample': 1,
'objective':'reg:squarederror',
'min_child_weight':3,
'gamma':0.25,
'n_estimators':5000}
return params<prepare_x_and_y>
train['GameWeather'] = train['GameWeather'].apply(map_weather )
xgb_feature=[x for x in reduce_train.columns if x not in cols_to_drop]
xgb_model = Xgb_Model(reduce_train, ajusted_test, features, categoricals=categoricals )<train_model>
train['IsRusher'] = train['NflId'] == train['NflIdRusher']
class Catb_Model(Base_Model):
def train_model(self, train_set, val_set):
verbosity = 100 if self.verbose else 0
clf = CatBoostRegressor(**self.params)
 clf.fit(train_set['X'],
train_set['y'],
eval_set=(val_set['X'], val_set['y']),
verbose=verbosity,
cat_features=self.categoricals 
)
 return clf
def convert_dataset(self, x_train, y_train, x_val, y_val):
train_set = {'X': x_train, 'y': y_train}
val_set = {'X': x_val, 'y': y_val}
return train_set, val_set
def get_params(self):
params = {'loss_function': 'RMSE',
'task_type': "CPU",
'iterations': 5000,
'od_type': "Iter",
'depth': 10,
'colsample_bylevel': 0.5,
'early_stopping_rounds': 100,
'l2_leaf_reg': 18,
'random_seed': 42,
'use_best_model': True
}
return params<create_dataframe>
train.drop(['NflId', 'NflIdRusher'], axis=1, inplace=True )
ctb_feature=[x for x in reduce_train.columns if x not in cols_to_drop]
ctb_model = Catb_Model(reduce_train, ajusted_test, features, categoricals=categoricals )<predict_on_test>
train = train.sort_values(by=['PlayId', 'Team', 'IsRusher'] ).reset_index()
final_pred = 0.7*regressor_model1.predict(ajusted_test)+0.2*xgb_model.y_pred.reshape(-1,1)+0.1*ctb_model.y_pred.reshape(-1,1)
 dist = Counter(reduce_train['accuracy_group'])
 for k in dist:
dist[k] /= len(reduce_train['accuracy_group'])
 acum = 0
bound = np.zeros(3 ).astype(np.float)
 for i in range(3):
acum += dist[i]
bound[i] = np.percentile(final_pred, acum * 100)
 print(bound)
 def classify(x):
if x <= bound[0]:
return 0
elif x <= bound[1]:
return 1
elif x <= bound[2]:
return 2
else:
return 3
final_pred = np.array(list(map(classify, final_pred)))
 sample_submission['accuracy_group'] = final_pred.astype(int)
 sample_submission.to_csv('./submission.csv', index=False)
 sample_submission['accuracy_group'].value_counts(normalize=True )<compute_test_metric>
train.drop(['GameId', 'PlayId', 'index', 'IsRusher', 'Team', 'Season'], axis=1, inplace=True )
oof_predicts=np.concatenate([x[0] for x in regressor_model1.oof],axis=0)
 oof_y=np.concatenate([x[1] for x in regressor_model1.oof],axis=0)
 oof_final_pred=np.array(list(map(classify, oof_predicts)))
 qwk(oof_final_pred,oof_y )<set_options>
cat_features = []
for col in train.columns:
if train[col].dtype =='object':
cat_features.append(col)
 train = train.drop(cat_features, axis=1 )
%matplotlib inline
pd.options.display.precision = 15
warnings.filterwarnings("ignore")
 %matplotlib inline
pd.set_option('max_rows', 500)
<categorify>
train.fillna(-999, inplace=True )
def add_datepart(df: pd.DataFrame, field_name: str,
prefix: str = None, drop: bool = True, time: bool = True, date: bool = True):

 field = df[field_name]
prefix = ifnone(prefix, re.sub('[Dd]ate$', '', field_name))
attr = ['Year', 'Month', 'Week', 'Day', 'Dayofweek', 'Is_month_end', 'Is_month_start']
if date:
attr.append('Date')
 if time:
attr = attr + ['Hour', 'Minute']
for n in attr:
df[prefix + n] = getattr(field.dt, n.lower())
 if drop:
df.drop(field_name, axis=1, inplace=True)
 return df
def ifnone(a: Any, b: Any)-> Any: 
<categorify>
X_train = np.array(train[players_col] ).reshape(-1, 11*22 )
@jit
def qwk(a1, a2):

 max_rat = 3
a1 = np.asarray(a1, dtype=int)
 a2 = np.asarray(a2, dtype=int)
 hist1 = np.zeros(( max_rat + 1,))
hist2 = np.zeros(( max_rat + 1,))
o = 0
for k in range(a1.shape[0]):
i, j = a1[k], a2[k]
hist1[i] += 1
hist2[j] += 1
o +=(i - j)*(i - j)
 e = 0
for i in range(max_rat + 1):
for j in range(max_rat + 1):
e += hist1[i] * hist2[j] *(i - j)*(i - j)
 e = e / a1.shape[0]
return 1 - o / e
def eval_qwk_lgb(y_true, y_pred):

 y_pred = y_pred.reshape(len(np.unique(y_true)) , -1 ).argmax(axis=0)
 return 'cappa', qwk(y_true, y_pred), True
def eval_qwk_lgb_regr(y_true, y_pred):

 y_pred[y_pred <= 1.12232214] = 0
y_pred[np.where(np.logical_and(y_pred > 1.12232214, y_pred <= 1.73925866)) ] = 1
y_pred[np.where(np.logical_and(y_pred > 1.73925866, y_pred <= 2.22506454)) ] = 2
y_pred[y_pred > 2.22506454] = 3
return 'cappa', qwk(y_true, y_pred), True
class LGBWrapper_regr(object):

 def __init__(self):
self.model = lgb.LGBMRegressor()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
if params['objective'] == 'regression':
eval_metric = eval_qwk_lgb_regr
else:
eval_metric = 'auc'
eval_set = [(X_train, y_train)]
eval_names = ['train']
self.model = self.model.set_params(**params)
 if X_valid is not None:
eval_set.append(( X_valid, y_valid))
eval_names.append('valid')
 if X_holdout is not None:
eval_set.append(( X_holdout, y_holdout))
eval_names.append('holdout')
 if 'cat_cols' in params.keys() :
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols)> 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = 'auto'
else:
categorical_columns = 'auto'
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_names=eval_names, eval_metric=eval_metric,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
categorical_feature=categorical_columns)
 self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict(self, X_test):
return self.model.predict(X_test, num_iteration=self.model.best_iteration_)
 def eval_qwk_xgb(y_pred, y_true):

 y_true = y_true.get_label()
y_pred = y_pred.argmax(axis=1)
 return 'cappa', -qwk(y_true, y_pred)
 class LGBWrapper(object):

 def __init__(self):
self.model = lgb.LGBMClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
eval_names = ['train']
self.model = self.model.set_params(**params)
 if X_valid is not None:
eval_set.append(( X_valid, y_valid))
eval_names.append('valid')
 if X_holdout is not None:
eval_set.append(( X_holdout, y_holdout))
eval_names.append('holdout')
 if 'cat_cols' in params.keys() :
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols)> 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = 'auto'
else:
categorical_columns = 'auto'
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_names=eval_names, eval_metric=eval_qwk_lgb,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
categorical_feature=categorical_columns)
 self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if self.model.objective == 'binary':
return self.model.predict_proba(X_test, num_iteration=self.model.best_iteration_)[:, 1]
else:
return self.model.predict_proba(X_test, num_iteration=self.model.best_iteration_)
 class CatWrapper(object):

 def __init__(self):
self.model = cat.CatBoostClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
self.model = self.model.set_params(**{k: v for k, v in params.items() if k != 'cat_cols'})
 if X_valid is not None:
eval_set.append(( X_valid, y_valid))
if X_holdout is not None:
eval_set.append(( X_holdout, y_holdout))
if 'cat_cols' in params.keys() :
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols)> 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = None
else:
categorical_columns = None
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
cat_features=categorical_columns)
 self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if 'MultiClass' not in self.model.get_param('loss_function'):
return self.model.predict_proba(X_test, ntree_end=self.model.best_iteration_)[:, 1]
else:
return self.model.predict_proba(X_test, ntree_end=self.model.best_iteration_)
 class XGBWrapper(object):

 def __init__(self):
self.model = xgb.XGBClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
self.model = self.model.set_params(**params)
 if X_valid is not None:
eval_set.append(( X_valid, y_valid))
if X_holdout is not None:
eval_set.append(( X_holdout, y_holdout))
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_metric=eval_qwk_xgb,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'])
 scores = self.model.evals_result()
self.best_score_ = {k: {m: m_v[-1] for m, m_v in v.items() } for k, v in scores.items() }
self.best_score_ = {k: {m: n if m != 'cappa' else -n for m, n in v.items() } for k, v in self.best_score_.items() }
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if self.model.objective == 'binary':
return self.model.predict_proba(X_test, ntree_limit=self.model.best_iteration)[:, 1]
else:
return self.model.predict_proba(X_test, ntree_limit=self.model.best_iteration)
 class MainTransformer(BaseEstimator, TransformerMixin):
def __init__(self, convert_cyclical: bool = False, create_interactions: bool = False, n_interactions: int = 20):

 self.convert_cyclical = convert_cyclical
self.create_interactions = create_interactions
self.feats_for_interaction = None
self.n_interactions = n_interactions
def fit(self, X, y=None):
if self.create_interactions:
self.feats_for_interaction = [col for col in X.columns if 'sum' in col
or 'mean' in col or 'max' in col or 'std' in col
or 'attempt' in col]
self.feats_for_interaction1 = np.random.choice(self.feats_for_interaction, self.n_interactions)
 self.feats_for_interaction2 = np.random.choice(self.feats_for_interaction, self.n_interactions)
 return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
 if self.create_interactions:
for col1 in self.feats_for_interaction1:
for col2 in self.feats_for_interaction2:
data[f'{col1}_int_{col2}'] = data[col1] * data[col2]
if self.convert_cyclical:
data['timestampHour'] = np.sin(2 * np.pi * data['timestampHour'] / 23.0)
 data['timestampMonth'] = np.sin(2 * np.pi * data['timestampMonth'] / 23.0)
 data['timestampWeek'] = np.sin(2 * np.pi * data['timestampWeek'] / 23.0)
 data['timestampMinute'] = np.sin(2 * np.pi * data['timestampMinute'] / 23.0)
 return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
 self.fit(data)
 return self.transform(data)
 class FeatureTransformer(BaseEstimator, TransformerMixin):
def __init__(self, main_cat_features: list = None, num_cols: list = None):

 self.main_cat_features = main_cat_features
self.num_cols = num_cols
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
 return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
 self.fit(data)
 return self.transform(data )<train_model>
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]
class RegressorModel(object):

 def __init__(self, columns: list = None, model_wrapper=None):

 self.columns = columns
self.model_wrapper = model_wrapper
self.result_dict = {}
self.train_one_fold = False
self.preprocesser = None
def fit(self, X: pd.DataFrame, y,
X_holdout: pd.DataFrame = None, y_holdout=None,
folds=None,
params: dict = None,
eval_metric='rmse',
cols_to_drop: list = None,
preprocesser=None,
transformers: dict = None,
adversarial: bool = False,
plot: bool = True):

 if folds is None:
folds = KFold(n_splits=3, random_state=42)
 self.train_one_fold = True
self.columns = X.columns if self.columns is None else self.columns
self.feature_importances = pd.DataFrame(columns=['feature', 'importance'])
 self.trained_transformers = {k: [] for k in transformers}
self.transformers = transformers
self.models = []
self.folds_dict = {}
self.eval_metric = eval_metric
n_target = 1
self.oof = np.zeros(( len(X), n_target))
self.n_target = n_target
X = X[self.columns]
if X_holdout is not None:
X_holdout = X_holdout[self.columns]
if preprocesser is not None:
self.preprocesser = preprocesser
self.preprocesser.fit(X, y)
 X = self.preprocesser.transform(X, y)
 self.columns = X.columns.tolist()
if X_holdout is not None:
X_holdout = self.preprocesser.transform(X_holdout)
 for fold_n,(train_index, valid_index)in enumerate(folds.split(X, y, X['installation_id'])) :
if X_holdout is not None:
X_hold = X_holdout.copy()
else:
X_hold = None
self.folds_dict[fold_n] = {}
if params['verbose']:
print(f'Fold {fold_n + 1} started at {time.ctime() }')
 self.folds_dict[fold_n] = {}
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if self.train_one_fold:
X_train = X[self.original_columns]
y_train = y
X_valid = None
y_valid = None
datasets = {'X_train': X_train, 'X_valid': X_valid, 'X_holdout': X_hold, 'y_train': y_train}
X_train, X_valid, X_hold = self.transform_(datasets, cols_to_drop)
 self.folds_dict[fold_n]['columns'] = X_train.columns.tolist()
model = copy.deepcopy(self.model_wrapper)
 if adversarial:
X_new1 = X_train.copy()
if X_valid is not None:
X_new2 = X_valid.copy()
elif X_holdout is not None:
X_new2 = X_holdout.copy()
X_new = pd.concat([X_new1, X_new2], axis=0)
 y_new = np.hstack(( np.zeros(( X_new1.shape[0])) , np.ones(( X_new2.shape[0]))))
X_train, X_valid, y_train, y_valid = train_test_split(X_new, y_new)
 model.fit(X_train, y_train, X_valid, y_valid, X_hold, y_holdout, params=params)
 self.folds_dict[fold_n]['scores'] = model.best_score_
if self.oof.shape[0] != len(X):
self.oof = np.zeros(( X.shape[0], self.oof.shape[1]))
if not adversarial:
self.oof[valid_index] = model.predict(X_valid ).reshape(-1, n_target)
 fold_importance = pd.DataFrame(list(zip(X_train.columns, model.feature_importances_)) ,
columns=['feature', 'importance'])
 self.feature_importances = self.feature_importances.append(fold_importance)
 self.models.append(model)
 self.feature_importances['importance'] = self.feature_importances['importance'].astype(int)
 self.calc_scores_()
if plot:
fig, ax = plt.subplots(figsize=(16, 12))
plt.subplot(2, 2, 1)
 self.plot_feature_importance(top_n=20)
 plt.subplot(2, 2, 2)
 self.plot_metric()
plt.subplot(2, 2, 3)
 plt.hist(y.values.reshape(-1, 1)- self.oof)
 plt.title('Distribution of errors')
 plt.subplot(2, 2, 4)
 plt.hist(self.oof)
 plt.title('Distribution of oof predictions');
def transform_(self, datasets, cols_to_drop):
for name, transformer in self.transformers.items() :
transformer.fit(datasets['X_train'], datasets['y_train'])
 datasets['X_train'] = transformer.transform(datasets['X_train'])
 if datasets['X_valid'] is not None:
datasets['X_valid'] = transformer.transform(datasets['X_valid'])
 if datasets['X_holdout'] is not None:
datasets['X_holdout'] = transformer.transform(datasets['X_holdout'])
 self.trained_transformers[name].append(transformer)
 if cols_to_drop is not None:
cols_to_drop = [col for col in cols_to_drop if col in datasets['X_train'].columns]
datasets['X_train'] = datasets['X_train'].drop(cols_to_drop, axis=1)
 if datasets['X_valid'] is not None:
datasets['X_valid'] = datasets['X_valid'].drop(cols_to_drop, axis=1)
 if datasets['X_holdout'] is not None:
datasets['X_holdout'] = datasets['X_holdout'].drop(cols_to_drop, axis=1)
 self.cols_to_drop = cols_to_drop
return datasets['X_train'], datasets['X_valid'], datasets['X_holdout']
def calc_scores_(self):
print()
datasets = [k for k, v in [v['scores'] for k, v in self.folds_dict.items() ][0].items() if len(v)> 0]
self.scores = {}
for d in datasets:
scores = [v['scores'][d][self.eval_metric] for k, v in self.folds_dict.items() ]
print(f"CV mean score on {d}: {np.mean(scores):.4f} +/- {np.std(scores):.4f} std.")
 self.scores[d] = np.mean(scores)
 def predict(self, X_test, averaging: str = 'usual'):

 full_prediction = np.zeros(( X_test.shape[0], self.oof.shape[1]))
if self.preprocesser is not None:
X_test = self.preprocesser.transform(X_test)
 for i in range(len(self.models)) :
X_t = X_test.copy()
for name, transformers in self.trained_transformers.items() :
X_t = transformers[i].transform(X_t)
 if self.cols_to_drop is not None:
cols_to_drop = [col for col in self.cols_to_drop if col in X_t.columns]
X_t = X_t.drop(cols_to_drop, axis=1)
 y_pred = self.models[i].predict(X_t[self.folds_dict[i]['columns']] ).reshape(-1, full_prediction.shape[1])
 if full_prediction.shape[0] != len(y_pred):
full_prediction = np.zeros(( y_pred.shape[0], self.oof.shape[1]))
if averaging == 'usual':
full_prediction += y_pred
elif averaging == 'rank':
full_prediction += pd.Series(y_pred ).rank().values
return full_prediction / len(self.models)
 def plot_feature_importance(self, drop_null_importance: bool = True, top_n: int = 10):

 top_feats = self.get_top_features(drop_null_importance, top_n)
 feature_importances = self.feature_importances.loc[self.feature_importances['feature'].isin(top_feats)]
feature_importances['feature'] = feature_importances['feature'].astype(str)
 top_feats = [str(i)for i in top_feats]
sns.barplot(data=feature_importances, x='importance', y='feature', orient='h', order=top_feats)
 plt.title('Feature importances')
 def get_top_features(self, drop_null_importance: bool = True, top_n: int = 10):

 grouped_feats = self.feature_importances.groupby(['feature'])['importance'].mean()
if drop_null_importance:
grouped_feats = grouped_feats[grouped_feats != 0]
return list(grouped_feats.sort_values(ascending=False ).index)[:top_n]
def plot_metric(self):

 full_evals_results = pd.DataFrame()
for model in self.models:
evals_result = pd.DataFrame()
for k in model.model.evals_result_.keys() :
evals_result[k] = model.model.evals_result_[k][self.eval_metric]
evals_result = evals_result.reset_index().rename(columns={'index': 'iteration'})
 full_evals_results = full_evals_results.append(evals_result)
 full_evals_results = full_evals_results.melt(id_vars=['iteration'] ).rename(columns={'value': self.eval_metric,
'variable': 'dataset'})
 sns.lineplot(data=full_evals_results, x='iteration', y=self.eval_metric, hue='dataset')
 plt.title('Training progress')
<categorify>
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))
class CategoricalTransformer(BaseEstimator, TransformerMixin):
def __init__(self, cat_cols=None, drop_original: bool = False, encoder=OrdinalEncoder()):

 self.cat_cols = cat_cols
self.drop_original = drop_original
self.encoder = encoder
self.default_encoder = OrdinalEncoder()
def fit(self, X, y=None):
if self.cat_cols is None:
kinds = np.array([dt.kind for dt in X.dtypes])
 is_cat = kinds == 'O'
self.cat_cols = list(X.columns[is_cat])
 self.encoder.set_params(cols=self.cat_cols)
 self.default_encoder.set_params(cols=self.cat_cols)
 self.encoder.fit(X[self.cat_cols], y)
 self.default_encoder.fit(X[self.cat_cols], y)
 return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
 new_cat_names = [f'{col}_encoded' for col in self.cat_cols]
encoded_data = self.encoder.transform(data[self.cat_cols])
 if encoded_data.shape[1] == len(self.cat_cols):
data[new_cat_names] = encoded_data
else:
pass
if self.drop_original:
data = data.drop(self.cat_cols, axis=1)
 else:
data[self.cat_cols] = self.default_encoder.transform(data[self.cat_cols])
 return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
 self.fit(data)
 return self.transform(data )<load_from_csv>
from keras.callbacks import EarlyStopping
def read_data() :
print('Reading train.csv file.... ')
 train = pd.read_csv('/kaggle/input/data-science-bowl-2019/train.csv')
 print('Training.csv file have {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Reading test.csv file.... ')
 test = pd.read_csv('/kaggle/input/data-science-bowl-2019/test.csv')
 print('Test.csv file have {} rows and {} columns'.format(test.shape[0], test.shape[1]))
print('Reading train_labels.csv file.... ')
 train_labels = pd.read_csv('/kaggle/input/data-science-bowl-2019/train_labels.csv')
 print('Train_labels.csv file have {} rows and {} columns'.format(train_labels.shape[0], train_labels.shape[1]))
print('Reading specs.csv file.... ')
 specs = pd.read_csv('/kaggle/input/data-science-bowl-2019/specs.csv')
 print('Specs.csv file have {} rows and {} columns'.format(specs.shape[0], specs.shape[1]))
print('Reading sample_submission.csv file.... ')
 sample_submission = pd.read_csv('/kaggle/input/data-science-bowl-2019/sample_submission.csv')
 print('Sample_submission.csv file have {} rows and {} columns'.format(sample_submission.shape[0], sample_submission.shape[1]))
return train, test, train_labels, specs, sample_submission
def encode_title(train, test, train_labels):
train['title_event_code'] = list(map(lambda x, y: str(x)+ '_' + str(y), train['title'], train['event_code']))
test['title_event_code'] = list(map(lambda x, y: str(x)+ '_' + str(y), test['title'], test['event_code']))
all_title_event_code = list(set(train["title_event_code"].unique() ).union(test["title_event_code"].unique()))
list_of_user_activities = list(set(train['title'].unique() ).union(set(test['title'].unique())))
 list_of_event_code = list(set(train['event_code'].unique() ).union(set(test['event_code'].unique())))
 list_of_event_id = list(set(train['event_id'].unique() ).union(set(test['event_id'].unique())))
 list_of_worlds = list(set(train['world'].unique() ).union(set(test['world'].unique())))
 activities_map = dict(zip(list_of_user_activities, np.arange(len(list_of_user_activities))))
activities_labels = dict(zip(np.arange(len(list_of_user_activities)) , list_of_user_activities))
activities_world = dict(zip(list_of_worlds, np.arange(len(list_of_worlds))))
assess_titles = list(set(train[train['type'] == 'Assessment']['title'].value_counts().index ).union(set(test[test['type'] == 'Assessment']['title'].value_counts().index)))
 train['title'] = train['title'].map(activities_map)
 test['title'] = test['title'].map(activities_map)
 train['world'] = train['world'].map(activities_world)
 test['world'] = test['world'].map(activities_world)
 train_labels['title'] = train_labels['title'].map(activities_map)
 win_code = dict(zip(activities_map.values() ,(4100*np.ones(len(activities_map)) ).astype('int')))
 win_code[activities_map['Bird Measurer(Assessment)']] = 4110
train['timestamp'] = pd.to_datetime(train['timestamp'])
 test['timestamp'] = pd.to_datetime(test['timestamp'])
 return train, test, train_labels, win_code, list_of_user_activities, list_of_event_code, activities_labels, assess_titles, list_of_event_id, all_title_event_code
def get_data(user_sample, test_set=False):

 last_activity = 0
user_activities_count = {'Clip':0, 'Activity': 0, 'Assessment': 0, 'Game':0}
last_session_time_sec = 0
accuracy_groups = {0:0, 1:0, 2:0, 3:0}
all_assessments = []
accumulated_accuracy_group = 0
accumulated_accuracy = 0
accumulated_correct_attempts = 0
accumulated_uncorrect_attempts = 0
accumulated_actions = 0
counter = 0
time_first_activity = float(user_sample['timestamp'].values[0])
 durations = []
last_accuracy_title = {'acc_' + title: -1 for title in assess_titles}
event_code_count: Dict[str, int] = {ev: 0 for ev in list_of_event_code}
event_id_count: Dict[str, int] = {eve: 0 for eve in list_of_event_id}
title_count: Dict[str, int] = {eve: 0 for eve in activities_labels.values() }
title_event_code_count: Dict[str, int] = {t_eve: 0 for t_eve in all_title_event_code}
for i, session in user_sample.groupby('game_session', sort=False):
session_type = session['type'].iloc[0]
session_title = session['title'].iloc[0]
session_title_text = activities_labels[session_title]
if(session_type == 'Assessment')&(test_set or len(session)>1):
all_attempts = session.query(f'event_code == {win_code[session_title]}')
 true_attempts = all_attempts['event_data'].str.contains('true' ).sum()
false_attempts = all_attempts['event_data'].str.contains('false' ).sum()
features = user_activities_count.copy()
features.update(last_accuracy_title.copy())
 features.update(event_code_count.copy())
 features.update(event_id_count.copy())
 features.update(title_count.copy())
 features.update(title_event_code_count.copy())
 features.update(last_accuracy_title.copy())
 features['installation_id'] = session['installation_id'].iloc[-1]
features['session_title'] = session['title'].iloc[0]
features['accumulated_correct_attempts'] = accumulated_correct_attempts
features['accumulated_uncorrect_attempts'] = accumulated_uncorrect_attempts
accumulated_correct_attempts += true_attempts
accumulated_uncorrect_attempts += false_attempts
if durations == []:
features['duration_mean'] = 0
else:
features['duration_mean'] = np.mean(durations)
 durations.append(( session.iloc[-1, 2] - session.iloc[0, 2] ).seconds)
 features['accumulated_accuracy'] = accumulated_accuracy/counter if counter > 0 else 0
accuracy = true_attempts/(true_attempts+false_attempts)if(true_attempts+false_attempts)!= 0 else 0
accumulated_accuracy += accuracy
last_accuracy_title['acc_' + session_title_text] = accuracy
if accuracy == 0:
features['accuracy_group'] = 0
elif accuracy == 1:
features['accuracy_group'] = 3
elif accuracy == 0.5:
features['accuracy_group'] = 2
else:
features['accuracy_group'] = 1
features.update(accuracy_groups)
 accuracy_groups[features['accuracy_group']] += 1
features['accumulated_accuracy_group'] = accumulated_accuracy_group/counter if counter > 0 else 0
accumulated_accuracy_group += features['accuracy_group']
features['accumulated_actions'] = accumulated_actions
if test_set:
all_assessments.append(features)
 elif true_attempts+false_attempts > 0:
all_assessments.append(features)
 counter += 1
def update_counters(counter: dict, col: str):
num_of_session_count = Counter(session[col])
 for k in num_of_session_count.keys() :
x = k
if col == 'title':
x = activities_labels[k]
counter[x] += num_of_session_count[k]
return counter
event_code_count = update_counters(event_code_count, "event_code")
 event_id_count = update_counters(event_id_count, "event_id")
 title_count = update_counters(title_count, 'title')
 title_event_code_count = update_counters(title_event_code_count, 'title_event_code')
 accumulated_actions += len(session)
 if last_activity != session_type:
user_activities_count[session_type] += 1
last_activitiy = session_type
if test_set:
return all_assessments[-1]
return all_assessments
def get_train_and_test(train, test):
compiled_train = []
compiled_test = []
for i,(ins_id, user_sample)in tqdm(enumerate(train.groupby('installation_id', sort = False)) , total = 17000):
compiled_train += get_data(user_sample)
 for ins_id, user_sample in tqdm(test.groupby('installation_id', sort = False), total = 1000):
test_data = get_data(user_sample, test_set = True)
 compiled_test.append(test_data)
 reduce_train = pd.DataFrame(compiled_train)
 reduce_test = pd.DataFrame(compiled_test)
 categoricals = ['session_title']
return reduce_train, reduce_test, categoricals
train, test, train_labels, specs, sample_submission = read_data()
train, test, train_labels, win_code, list_of_user_activities, list_of_event_code, activities_labels, assess_titles, list_of_event_id, all_title_event_code = encode_title(train, test, train_labels)
 reduce_train, reduce_test, categoricals = get_train_and_test(train, test )<categorify>
__all__ = ['RAdam']
class RAdam(keras.optimizers.Optimizer):

 def __init__(self, learning_rate=0.001, beta_1=0.9, beta_2=0.999,
epsilon=None, decay=0., weight_decay=0., amsgrad=False,
total_steps=0, warmup_proportion=0.1, min_lr=0., **kwargs):
learning_rate = kwargs.pop('lr', learning_rate)
 super(RAdam, self ).__init__(**kwargs)
 with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
 self.learning_rate = K.variable(learning_rate, name='learning_rate')
 self.beta_1 = K.variable(beta_1, name='beta_1')
 self.beta_2 = K.variable(beta_2, name='beta_2')
 self.decay = K.variable(decay, name='decay')
 self.weight_decay = K.variable(weight_decay, name='weight_decay')
 self.total_steps = K.variable(total_steps, name='total_steps')
 self.warmup_proportion = K.variable(warmup_proportion, name='warmup_proportion')
 self.min_lr = K.variable(min_lr, name='min_lr')
 if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
self.initial_weight_decay = weight_decay
self.initial_total_steps = total_steps
self.amsgrad = amsgrad
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
 self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr = lr *(1./(1.+ self.decay * K.cast(self.iterations, K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx())+ 1
if self.initial_total_steps > 0:
warmup_steps = self.total_steps * self.warmup_proportion
decay_steps = K.maximum(self.total_steps - warmup_steps, 1)
 decay_rate =(self.min_lr - lr)/ decay_steps
lr = K.switch(
 t <= warmup_steps,
lr *(t / warmup_steps),
lr + decay_rate * K.minimum(t - warmup_steps, decay_steps), 
)
 ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='m_' + str(i)) for(i, p)in enumerate(params)]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='v_' + str(i)) for(i, p)in enumerate(params)]
if self.amsgrad:
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='vhat_' + str(i)) for(i, p)in enumerate(params)]
else:
vhats = [K.zeros(1, name='vhat_' + str(i)) for i in range(len(params)) ]
self.weights = [self.iterations] + ms + vs + vhats
beta_1_t = K.pow(self.beta_1, t)
 beta_2_t = K.pow(self.beta_2, t)
 sma_inf = 2.0 /(1.0 - self.beta_2)- 1.0
sma_t = sma_inf - 2.0 * t * beta_2_t /(1.0 - beta_2_t)
 for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
m_t =(self.beta_1 * m)+(1.- self.beta_1)* g
v_t =(self.beta_2 * v)+(1.- self.beta_2)* K.square(g)
 m_corr_t = m_t /(1.0 - beta_1_t)
 if self.amsgrad:
vhat_t = K.maximum(vhat, v_t)
 v_corr_t = K.sqrt(vhat_t /(1.0 - beta_2_t))
self.updates.append(K.update(vhat, vhat_t))
else:
v_corr_t = K.sqrt(v_t /(1.0 - beta_2_t))
r_t = K.sqrt(( sma_t - 4.0)/(sma_inf - 4.0)* 
(sma_t - 2.0)/(sma_inf - 2.0)*
sma_inf / sma_t)
 p_t = K.switch(sma_t >= 5, r_t * m_corr_t /(v_corr_t + self.epsilon), m_corr_t)
 if self.initial_weight_decay > 0:
p_t += self.weight_decay * p
p_t = p - lr * p_t
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
new_p = p_t
if getattr(p, 'constraint', None)is not None:
new_p = p.constraint(new_p)
 self.updates.append(K.update(p, new_p))
return self.updates
@property
def lr(self):
return self.learning_rate
@lr.setter
def lr(self, learning_rate):
self.learning_rate = learning_rate
def get_config(self):
config = {
'learning_rate': float(K.get_value(self.learning_rate)) ,
'beta_1': float(K.get_value(self.beta_1)) ,
'beta_2': float(K.get_value(self.beta_2)) ,
'decay': float(K.get_value(self.decay)) ,
'weight_decay': float(K.get_value(self.weight_decay)) ,
'epsilon': self.epsilon,
'amsgrad': self.amsgrad,
'total_steps': float(K.get_value(self.total_steps)) ,
'warmup_proportion': float(K.get_value(self.warmup_proportion)) ,
'min_lr': float(K.get_value(self.min_lr)) ,
}
base_config = super(RAdam, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
def preprocess(reduce_train, reduce_test):
for df in [reduce_train, reduce_test]:
df['installation_session_count'] = df.groupby(['installation_id'])['Clip'].transform('count')
 df['installation_duration_mean'] = df.groupby(['installation_id'])['duration_mean'].transform('mean')
 df['installation_title_nunique'] = df.groupby(['installation_id'])['session_title'].transform('nunique')
 df['sum_event_code_count'] = df[[2050, 4100, 4230, 5000, 4235, 2060, 4110, 5010, 2070, 2075, 2080, 2081, 2083, 3110, 4010, 3120, 3121, 4020, 4021,
4022, 4025, 4030, 4031, 3010, 4035, 4040, 3020, 3021, 4045, 2000, 4050, 2010, 2020, 4070, 2025, 2030, 4080, 2035,
2040, 4090, 4220, 4095]].sum(axis = 1)
 df['installation_event_code_count_mean'] = df.groupby(['installation_id'])['sum_event_code_count'].transform('mean')
 features = reduce_train.loc[(reduce_train.sum(axis=1)!= 0),(reduce_train.sum(axis=0)!= 0)].columns
features = [x for x in features if x not in ['accuracy_group', 'installation_id']] + ['acc_' + title for title in assess_titles]
return reduce_train, reduce_test, features
reduce_train, reduce_test, features = preprocess(reduce_train, reduce_test )<init_hyperparams>
class Mish(Layer):
def __init__(self, **kwargs):
super(Mish, self ).__init__(**kwargs)
 def build(self, input_shape):
super(Mish, self ).build(input_shape)
 def call(self, x):
return x * K.tanh(K.softplus(x))
def compute_output_shape(self, input_shape):
return input_shape
params = {'n_estimators':2000,
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'subsample': 0.75,
'subsample_freq': 1,
'learning_rate': 0.04,
'feature_fraction': 0.9,
'max_depth': 15,
'lambda_l1': 1,
'lambda_l2': 1,
'verbose': 100,
'early_stopping_rounds': 100, 'eval_metric': 'cappa'
}<prepare_x_and_y>
class CyclicLR(Callback):

 def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',
gamma=1., scale_fn=None, scale_mode='cycle'):
super(CyclicLR, self ).__init__()
self.base_lr = base_lr
self.max_lr = max_lr
self.step_size = step_size
self.mode = mode
self.gamma = gamma
if scale_fn == None:
if self.mode == 'triangular':
self.scale_fn = lambda x: 1.
 self.scale_mode = 'cycle'
elif self.mode == 'triangular2':
self.scale_fn = lambda x: 1/(2.**(x-1))
self.scale_mode = 'cycle'
elif self.mode == 'exp_range':
self.scale_fn = lambda x: gamma**(x)
 self.scale_mode = 'iterations'
else:
self.scale_fn = scale_fn
self.scale_mode = scale_mode
self.clr_iterations = 0.
 self.trn_iterations = 0.
 self.history = {}
self._reset()
def _reset(self, new_base_lr=None, new_max_lr=None,
new_step_size=None):

 if new_base_lr != None:
self.base_lr = new_base_lr
if new_max_lr != None:
self.max_lr = new_max_lr
if new_step_size != None:
self.step_size = new_step_size
self.clr_iterations = 0.
 def clr(self):
cycle = np.floor(1+self.clr_iterations/(2*self.step_size))
x = np.abs(self.clr_iterations/self.step_size - 2*cycle + 1)
 if self.scale_mode == 'cycle':
return self.base_lr +(self.max_lr-self.base_lr)*np.maximum(0,(1-x)) *self.scale_fn(cycle)
 else:
return self.base_lr +(self.max_lr-self.base_lr)*np.maximum(0,(1-x)) *self.scale_fn(self.clr_iterations)
 def on_train_begin(self, logs={}):
logs = logs or {}
if self.clr_iterations == 0:
K.set_value(self.model.optimizer.lr, self.base_lr)
 else:
K.set_value(self.model.optimizer.lr, self.clr())
 def on_batch_end(self, epoch, logs=None):
logs = logs or {}
self.trn_iterations += 1
self.clr_iterations += 1
self.history.setdefault('lr', [] ).append(K.get_value(self.model.optimizer.lr))
self.history.setdefault('iterations', [] ).append(self.trn_iterations)
 for k, v in logs.items() :
self.history.setdefault(k, [] ).append(v)
 K.set_value(self.model.optimizer.lr, self.clr() )
y = reduce_train['accuracy_group']<choose_model_class>
import tensorflow as tf
n_fold = 5
folds = GroupKFold(n_splits=n_fold )<define_variables>
def train_model(x_tr, y_tr, x_vl, y_vl):
inp = keras.layers.Input([X_train.shape[1]])
 x = keras.layers.Dense(units=324 )(inp)
 x = keras.layers.BatchNormalization(momentum=0.8, axis=1 )(x)
 x = Mish()(x)
 x = keras.layers.Dropout(0.35 )(x)
 x = keras.layers.Dense(units=512 )(x)
 x = keras.layers.BatchNormalization(momentum=0.8, axis=1 )(x)
 x = Mish()(x)
 x = keras.layers.Dropout(0.5 )(x)
 x = keras.layers.Dense(units=324 )(x)
 x = keras.layers.BatchNormalization(momentum=0.8, axis=1 )(x)
 x = Mish()(x)
 x = keras.layers.Dropout(0.35 )(x)
 x = keras.layers.Dense(units=199, activation='sigmoid' )(x)
 model = keras.Model(inp, x)
 er = EarlyStopping(patience=15, min_delta=1e-4, restore_best_weights=True, monitor='val_loss')
 clr = CyclicLR(base_lr=1e-3, max_lr=5*1e-3, step_size = 1000, gamma = 0.99)
 model.compile(optimizer=RAdam(warmup_proportion=0.1, min_lr=1e-5),
loss='mse',
metrics = ['mse'] 
)
 model.fit(x_tr, y_tr, epochs=100, callbacks=[clr, er], validation_data=[x_vl, y_vl])
 return model
cols_to_drop = ['game_session', 'installation_id', 'timestamp', 'accuracy_group', 'timestampDate']<train_model>
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train )
mt = MainTransformer()
ft = FeatureTransformer()
transformers = {'ft': ft}
regressor_model1 = RegressorModel(model_wrapper=LGBWrapper_regr())
 regressor_model1.fit(X=reduce_train, y=y, folds=folds, params=params, preprocesser=mt, transformers=transformers,
eval_metric='cappa', cols_to_drop=cols_to_drop )<compute_test_metric>
def make_pred(df, sample, env, models):
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(test.columns)-set('Yards')
 for c in missing_cols:
df[c] = 0
df = df[dummy_col]
df.drop(['Yards'], axis=1, inplace=True)
 df['GameClock'] = df['GameClock'].apply(strtoseconds)
 df['PlayerHeight'] = df['PlayerHeight'].apply(lambda x: 12*int(x.split('-')[0])+int(x.split('-')[1]))
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)
 df['WindSpeed'] = df['WindSpeed'].apply(lambda x: x.lower().replace('mph', '' ).strip() if not pd.isna(x)else x)
 df['WindSpeed'] = df['WindSpeed'].apply(lambda x:(int(x.split('-')[0])+int(x.split('-')[1])) /2 if not pd.isna(x)and '-' in x else x)
 df['WindSpeed'] = df['WindSpeed'].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'] = df['WindSpeed'].apply(str_to_float)
 df['PlayDirection'] = train['PlayDirection'].apply(lambda x: x is 'right')
 df['Team'] = df['Team'].apply(lambda x: x.strip() =='home')
 indoor = "indoor"
df['GameWeather'] = df['GameWeather'].apply(lambda x: indoor if not pd.isna(x)and indoor in x else x)
 df['GameWeather'] = df['GameWeather'].apply(lambda x: x.lower().replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly' ).replace('clear and sunny', 'sunny and clear' ).replace('skies', '' ).replace("mostly", "" ).strip() if not pd.isna(x)else x)
 df['GameWeather'] = df['GameWeather'].apply(map_weather)
 df['IsRusher'] = df['NflId'] == df['NflIdRusher']
df = df.sort_values(by=['PlayId', 'Team', 'IsRusher'] ).reset_index()
df = df.drop(['TimeHandoff', 'TimeSnap', 'PlayerBirthDate', 'WindDirection', 'NflId', 'NflIdRusher', 'GameId', 'PlayId', 'index', 'IsRusher', 'Team', 'Season'], axis=1)
 cat_features = []
for col in df.columns:
if df[col].dtype =='object':
cat_features.append(col)
 df = df.drop(cat_features, axis=1)
 df.fillna(-999, inplace=True)
 X = np.array(df[players_col] ).reshape(-1, 11*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 = scaler.transform(np.concatenate([X, X_play_col], axis=1))
y_pred = np.mean([model.predict(X)for model in models], axis=0)
 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))
return y_pred
class OptimizedRounder(object):

 def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):

 X_p = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3])
 return -qwk(y, X_p)
 def fit(self, X, y):

 loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):

 return pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3])
 def coefficients(self):

 return self.coef_['x']<predict_on_test>
rkf = RepeatedKFold(n_splits=5, n_repeats=5 )
opt_preds = optR.predict(pr1.reshape(-1,), coefficients)
 qwk(y, opt_preds )<predict_on_test>
from keras import backend as K
pr1 = regressor_model1.predict(reduce_test)
 pr1[pr1 <= 1.12232214] = 0
pr1[np.where(np.logical_and(pr1 > 1.12232214, pr1 <= 1.73925866)) ] = 1
pr1[np.where(np.logical_and(pr1 > 1.73925866, pr1 <= 2.22506454)) ] = 2
pr1[pr1 > 2.22506454] = 3<save_to_csv>
models = []
for tr_idx, vl_idx in rkf.split(X_train, y_train):
x_tr, y_tr = X_train[tr_idx], y_train[tr_idx]
x_vl, y_vl = X_train[vl_idx], y_train[vl_idx]
model = train_model(x_tr, y_tr, x_vl, y_vl)
 models.append(model )
sample_submission['accuracy_group'] = pr1.astype(int)
 sample_submission.to_csv('submission.csv', index=False )<count_values>
env = nflrush.make_env()
sample_submission['accuracy_group'].value_counts(normalize=True )<categorify>
for test, sample in tqdm.tqdm(env.iter_test()):
make_pred(test, sample, env, models )
<create_dataframe><EOS> 
env.write_submission_file()