Kaggle competition: Traveling Santa 2018 - Prime Paths
<SOS> metric: TravelingSanta2 Kaggle data source: traveling-santa-2018-prime-paths<save_to_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sympy.ntheory.primetest import isprime
from numba import jit
from math import sqrt
sample_df.to_csv('submission.csv',index=False)
 _ = sample_df.hist()<set_options>
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv')
 cities['IsPrime'] = cities['CityId'].apply(isprime )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
def compute_score(path):
path = path[ : len(path)-1]
cities_path = cities.reindex(path)
 isPrime = cities_path.IsPrime.values
coord = cities_path[['X', 'Y']].values
return compute_score_fast(coord, isPrime )
from fastai import *
from fastai.vision import *
import pandas as pd
import matplotlib.pyplot as plt<set_options>
@jit(nopython=True, parallel=True)
 def compute_score_fast(coord, isPrime):
score = 0
city_start_coord = coord[0]
for step in range(1,len(coord)) :
city_end_coord = coord[step]
distance_step = distance(city_start_coord, city_end_coord)
 if step%10 == 0 and not isPrime[step-1]:
distance_step = 1.1 * distance_step
score = score + distance_step
city_start_coord = city_end_coord
score = score + distance(city_start_coord, coord[0])
 return score
print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )<set_options>
@jit(nopython=True, parallel=True)
 def distance(a, b):
return sqrt(pow(b[0] - a[0], 2)+ pow(b[1] - a[1], 2))
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED = 999
seed_everything(SEED )<feature_engineering>
path_init = pd.read_csv('.. /input/lkh-solver/submission.csv')['Path'].tolist()
score_init = compute_score(path_init)
 score_init
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 df.head(10 )<set_options>
path = path_init
path = path[ : len(path)-1]
cities_path = cities.reindex(path)
 cityId = cities_path.CityId.values
isPrime = cities_path.IsPrime.values
coord = cities_path[['X', 'Y']].values
cities_path.tail()
im = Image.open(df['path'][1])
 width, height = im.size
print(width,height)
 im.show()<define_variables>
for s in range(1,len(path)) :
if s%10 == 0 and not isPrime[s-1]:
t = s-1
if isPrime[t-1]:
d_10thstep_old = distance(coord[s-3], coord[s-2])+ distance(coord[s-2], coord[s-1])+ distance(coord[s-1], coord[s])* 1.1
d_10thstep_new = distance(coord[s-3], coord[s-1])+ distance(coord[s-1], coord[s-2])+ distance(coord[s-2], coord[s])
 if d_10thstep_new < d_10thstep_old :
gain = d_10thstep_new - d_10thstep_old
print(s, " gain: ", gain, " -- t = ", t)
 path[t-1] , path[s-1] = path[s-1] , path[t-1]
isPrime[t-1] , isPrime[s-1] = isPrime[s-1] , isPrime[t-1]
coord[t-1] , coord[s-1] = coord[s-1] , coord[t-1]
print("Done")
bs = 64
sz=224<define_variables>
for s in range(1,len(path)) :
if s%10 == 0 and not isPrime[s-1]:
t = s+1
if isPrime[t-1]:
d_10thstep_old = distance(coord[s-2], coord[s-1])+ distance(coord[s-1], coord[s])*1.1 + distance(coord[s], coord[s+1])
 d_10thstep_new = distance(coord[s-2], coord[s])+ distance(coord[s], coord[s-1])+ distance(coord[s-1], coord[s+1])
 if d_10thstep_new < d_10thstep_old :
gain = d_10thstep_new - d_10thstep_old
print(s, " gain: ", gain, " -- t = ", t)
 path[t-1] , path[s-1] = path[s-1] , path[t-1]
isPrime[t-1] , isPrime[s-1] = isPrime[s-1] , isPrime[t-1]
coord[t-1] , coord[s-1] = coord[s-1] , coord[t-1]
print("Done")
data.show_batch(rows=3, figsize=(7,6))<compute_test_metric>
for s in range(1,len(path)) :
if s%10 == 0 and not isPrime[s-1]:
x0 = coord[s-1][0]
y0 = coord[s-1][1]
d = distance(coord[s-2], coord[s-1])+ distance(coord[s-1], coord[s])* 1.1
cell = cities[(cities.X > x0-d)&(cities.X < x0+d)]
cell = cell[(cell.Y > y0-d)&(cell.Y < y0+d)]
cell = cell[cell.IsPrime]
coord_primes_arround = cell[['X','Y']].values
cityId_primes_arround = cell.CityId.values
for j in range(len(cell)) :
d_10thstep_old = distance(coord[s-2], coord[s-1])+ distance(coord[s-1], coord[s])* 1.1
t = path.index(cityId_primes_arround[j])+ 1
if(t%10 != 0)and(t!=s-1)and(t!=s+1):
d_prime_old = distance(coord[t-2], coord[t-1])+ distance(coord[t-1], coord[t])
 d_10thstep_new = distance(coord[s-2], coord[t-1])+ distance(coord[t-1], coord[s])
 d_prime_new = distance(coord[t-2], coord[s-1])+ distance(coord[s-1], coord[t])
 if not isprime(cityId[t-2])and(t-1)%10==0 :
d_prime_old = distance(coord[t-2], coord[t-1])*1.1 + distance(coord[t-1], coord[t])
 d_prime_new = distance(coord[t-2], coord[s-1])*1.1 + distance(coord[s-1], coord[t])
 d_old = d_10thstep_old + d_prime_old
d_new = d_10thstep_new + d_prime_new
if d_new < d_old :
gain = d_new - d_old
print(s, " gain: ", gain, " -- t = ", t)
 path[t-1] , path[s-1] = path[s-1] , path[t-1]
isPrime[t-1] , isPrime[s-1] = isPrime[s-1] , isPrime[t-1]
coord[t-1] , coord[s-1] = coord[s-1] , coord[t-1]
print("Done" )
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )<choose_model_class>
path.append(0)
 score_final = compute_score(path)
 print(score_final - score_init )
<train_model><EOS> 
pd.DataFrame({'Path':path} ).to_csv('submission.csv', index=False )