Kaggle competition: Santa's Workshop Tour 2019
<SOS> metric: SantaWorkshopSchedule2019 Kaggle data source: santas-workshop-tour-2019<train_model>
import os
import ctypes
from numpy.ctypeslib import ndpointer
import pandas as pd
import numpy as np
from tqdm.notebook import tqdm
from numba import njit, prange
ran = rfgd.best_estimator_
knn = kngd.best_estimator_
log = lrgd.best_estimator_
xgb = xggd.best_estimator_
gbc = gbgd.best_estimator_
svc = svgd.best_estimator_
ext = etgd.best_estimator_
ada = adgd.best_estimator_
gpc = gpgd.best_estimator_
bag = bcgd.best_estimator_
models = [ran, knn, log, xgb, gbc, svc, ext, ada, gnb, gpc, bag]
scores_v3 = []
for mod in models:
mod.fit(X_train, y_train)
 acc = cross_val_score(mod, X_train, y_train, scoring = "accuracy", cv = 10)
 scores_v3.append(acc.mean() )<create_dataframe>
%%writefile score.c
int cost[NF][101];
int fs[NF];
int cf[NF][10];
int loaded=0;
float acc[301][301];
void precompute_acc() {
for(int i=125;i<=300;i++)
 for(int j=125;j<=300;j++)
 acc[i][j] =(i-125.0)/400.0 * pow(i , 0.5 + fabs(i-j)/ 50);
}
void read_fam() {
FILE *f;
char s[1000];
int d[101],fid,n;
int *c;
f=fopen(".. /input/santa-workshop-tour-2019/family_data.csv","r");
if(fgets(s,1000,f)==NULL)
 exit(-1);
for(int i=0;i<5000;i++){
c = &cf[i][0];
if(fscanf(f,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
&fid,&c[0],&c[1],&c[2],&c[3],&c[4],&c[5],&c[6],&c[7],&c[8],&c[9],&fs[i])!=12)
 exit(-1);
// printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d
",
//fid,c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9],fs[i]);
n = fs[i];
for(int j=1;j<=100;j++){
if(j==c[0])cost[i][j]=0;
else if(j==c[1])cost[i][j]=50;
else if(j==c[2])cost[i][j]=50 + 9 * n;
else if(j==c[3])cost[i][j]=100 + 9 * n;
else if(j==c[4])cost[i][j]=200 + 9 * n;
else if(j==c[5])cost[i][j]=200 + 18 * n;
else if(j==c[6])cost[i][j]=300 + 18 * n;
else if(j==c[7])cost[i][j]=300 + 36 * n;
else if(j==c[8])cost[i][j]=400 + 36 * n;
else if(j==c[9])cost[i][j]=500 + 36 * n + 199 * n;
else cost[i][j]=500 + 36 * n + 398 * n;
}
}
}
float max_cost=1000000000;
int day_occ[102];
static inline int day_occ_ok(int d){
return !(d <125 || d>300);
}
float score(int *pred){
float r=0;
if(!loaded){
read_fam() ;
precompute_acc() ;
loaded = 1;
}
// validate day occupancy
memset(day_occ,0,101*sizeof(int)) ;
for(int i=0;i<NF;i++){
day_occ[pred[i]]+=fs[i];
r+=cost[i][pred[i]];
}
day_occ[101]=day_occ[100];
for(int d=1;d<=100;d++){
if(day_occ[d]<125)
 r += 100000 *(125 - day_occ[d]);
else if(day_occ[d] > 300)
 r += 100000 *(day_occ[d] - 300);
r += acc[day_occ[d]][day_occ[d+1]];
}
return r;
}
results = pd.DataFrame({
'Model': ['Random Forest', 'K Nearest Neighbour', 'Logistic Regression', 'XGBoost', 'Gradient Boosting', 'SVC', 'Extra Trees', 'AdaBoost', 'Gaussian Naive Bayes', 'Gaussian Process', 'Bagging Classifier'],
'Original Score': scores,
'Score with feature selection': scores_v2,
'Score with tuned parameters': scores_v3})
 result_df = results.sort_values(by='Score with tuned parameters', ascending=False ).reset_index(drop=True)
 result_df<train_model>
lib = ctypes.CDLL('./score.so')
 cost_function = lib.score
cost_function.restype = ctypes.c_float
cost_function.argtypes = [ndpointer(ctypes.c_int)]
ran = rfgd.best_estimator_
knn = kngd.best_estimator_
log = lrgd.best_estimator_
xgb = xggd.best_estimator_
gbc = gbgd.best_estimator_
svc = svgd.best_estimator_
ext = etgd.best_estimator_
ada = adgd.best_estimator_
gpc = gpgd.best_estimator_
bag = bcgd.best_estimator_
models = [ran, knn, log, xgb, gbc, svc, ext, ada, gnb, gpc, bag]
scores_v4 = []
for mod in models:
mod.fit(X_train, y_train)
 predict = mod.predict(X_test)
 acc = accuracy_score(y_test, predict)
 scores_v4.append(acc )<create_dataframe>
score = []
sub = []
name = os.listdir('/kaggle/input/santa-public')
 for item in name:
score.append(int(item.split('_')[1].split('.')[0]))
sub.append(pd.read_csv('.. /input/santa-public/'+item, index_col='family_id'))
print(np.min(score))
print(len(sub))
results = pd.DataFrame({
'Model': ['Random Forest', 'K Nearest Neighbour', 'Logistic Regression', 'XGBoost', 'Gradient Boosting', 'SVC', 'Extra Trees', 'AdaBoost', 'Gaussian Naive Bayes', 'Gaussian Process', 'Bagging Classifier'],
'Original Score': scores,
'Score with feature selection': scores_v2,
'Score with tuned parameters': scores_v3,
'Score with full accuracy': scores_v4})
 result_df = results.sort_values(by='Score with full accuracy', ascending=False ).reset_index(drop=True)
 result_df<predict_on_test>
top_k = 3
base_path = '/kaggle/input/santa-workshop-tour-2019/'
sub_path = '/kaggle/input/greedy-dual-and-tripple-shuffle-with-fast-scoring/'
data = pd.read_csv(base_path + 'family_data.csv', index_col='family_id')
 submission = pd.read_csv(f'.. /input/santa-public/submission_{np.min(score)}.csv',
index_col='family_id')
 original = submission['assigned_day'].values
original_score = cost_function(np.int32(original))
choice_matrix = data.loc[:, 'choice_0': 'choice_9'].values
print(cost_function(np.int32(original)) )
predictions = ext.predict(test_features )<save_to_csv>
fam_weight = []
for i, s in enumerate(submission.iterrows()):
for c in range(choice_matrix.shape[1]):
if s[1].values==choice_matrix[i, c]:
fam_weight.append(c+1)
 fam_weight = np.array(fam_weight)
 fam_weight = fam_weight / sum(fam_weight)
 print(fam_weight )
submission=pd.DataFrame()
submission['PassengerId']=TestId
submission['Survived']=predictions
submission.to_csv('submission.csv', index=False )<load_from_csv>
redundancy = 5
choice_weight = np.zeros(( 5000, top_k))
for i in tqdm(range(5000)) :
for j in range(top_k):
for s in sub:
if choice_matrix[i, j] == s.loc[i, 'assigned_day']:
choice_weight[i, j] += 1
choice_weight += redundancy
for j in range(choice_weight.shape[0]):
choice_weight[j] /= sum(choice_weight[j])
 print(choice_weight )
print('reading input files.. ')
 data = pd.read_csv('.. /input/train.csv')
 sampl = pd.read_csv('.. /input/gender_submission.csv' )<load_from_csv>
def random_choice_prob_index(a, axis=1):
r = np.expand_dims(np.random.rand(a.shape[1-axis]), axis=axis)
 return(a.cumsum(axis=axis)> r ).argmax(axis=axis )
test = pd.read_csv('.. /input/test.csv' )<concatenate>
def lucky_choice_search(top_k, fam_size, original, choice_matrix,
disable_tqdm=False, n_iter=100000000,
verbose=10000, random_state=2019):
best = original.copy()
best_score = cost_function(np.int32(best))
if random_state is not None:
np.random.seed(random_state)
 fam_indices = np.random.choice(range(choice_matrix.shape[0]), size=fam_size, p=fam_weight)
 for i in tqdm(range(n_iter), disable=disable_tqdm):
new = best.copy()
new[fam_indices] = choice_matrix[fam_indices, random_choice_prob_index(choice_weight[fam_indices])]
new_score = cost_function(np.int32(new))
if new_score < best_score:
best_score = new_score
best = new
print(f'{i} NEW BEST SCORE: ', best_score)
 submission['assigned_day'] = best
submission.to_csv(f'submission_{best_score}.csv')
 if verbose and i % verbose == 0:
print(f"Iteration
return best, best_score
df = data.append(test, sort = False )<merge>
best, best_score = lucky_choice_search(
 choice_matrix=choice_matrix,
top_k=top_k,
fam_size=20,
original=original,
n_iter=250000000,
disable_tqdm=False,
random_state=20191217,
verbose=None
)
<feature_engineering><EOS> 
submission['assigned_day'] = best
submission.to_csv(f'submission_{best_score}.csv' )