Kaggle competition: Santa's Workshop Tour 2019
<SOS> metric: SantaWorkshopSchedule2019 Kaggle data source: santas-workshop-tour-2019<groupby>
from itertools import product
import pandas as pd
import numpy as np
from tqdm.notebook import tqdm
from numba import njit, prange
import santa_s_2019_faster_cost_function_24_s as utils
from santa_s_2019_faster_cost_function_24_s import build_cost_function
surnames = df[['surname','Cabin','Pclass','Name']].groupby(['surname','Pclass'] ).count()
surnames.head()<filter>
base_path = '/kaggle/input/santa-workshop-tour-2019/'
sub_path = '/kaggle/input/santa-ip/'
data = pd.read_csv(base_path + 'family_data.csv', index_col='family_id')
 submission = pd.read_csv(sub_path + 'submission.csv', index_col='family_id')
 cost_function = build_cost_function(data)
 original = submission['assigned_day'].values
original_score = cost_function(original)
 %timeit cost_function(original )
missing = surnames[(surnames.Cabin>0)&(surnames.Cabin<surnames.Name)]
missing.rename(columns={'Name': 'passenger_count'} )<filter>
def stochastic_product_search(top_k, fam_size, original, choice_matrix,
disable_tqdm=False, verbose=10000,
n_iter=500, random_state=2019):

 best = original.copy()
best_score = cost_function(best)
 np.random.seed(random_state)
 for i in tqdm(range(n_iter), disable=disable_tqdm):
fam_indices = np.random.choice(range(choice_matrix.shape[0]), size=fam_size)
 changes = np.array(list(product(*choice_matrix[fam_indices, :top_k].tolist())))
 for change in changes:
new = best.copy()
new[fam_indices] = change
new_score = cost_function(new)
 if new_score < best_score:
best_score = new_score
best = new
if new_score < best_score:
best_score = new_score
best = new
if verbose and i % verbose == 0:
print(f"Iteration
print(f"Final best score is {best_score:.2f}")
 return best
df[df.surname=='Brown']<filter>
choice_matrix = data.loc[:, 'choice_0': 'choice_9'].values
df[df.surname=='Hoyt']<groupby>
best = stochastic_product_search(
 choice_matrix=choice_matrix,
top_k=5,
fam_size=5,
original=original,
n_iter=20000,
disable_tqdm=False,
verbose=2000
)
tickets_grouped = df[['Ticket','Cabin','Name']].groupby('Ticket' ).count()
candidate_tickets = tickets_grouped[(tickets_grouped['Cabin']>=1)&(tickets_grouped['Name']>=2)&(tickets_grouped['Cabin']<tickets_grouped['Name'])]
candidate_tickets<filter>
best = stochastic_product_search(
 choice_matrix=choice_matrix,
top_k=2,
fam_size=12,
original=best,
n_iter=50000,
disable_tqdm=True,
verbose=5000
)
df[df.Ticket=='113781']<feature_engineering>
best = stochastic_product_search(
 choice_matrix=choice_matrix,
top_k=8,
fam_size=3,
original=best,
n_iter=50000,
disable_tqdm=False,
verbose=5000
)
<count_missing_values><EOS> 
submission['assigned_day'] = best
final_score = cost_function(best)
 submission.to_csv(f'submission_{final_score}.csv' )