Kaggle competition: What's Cooking? (Kernels Only)
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv', index_col=['CityId'])
 XY = np.stack(( cities.X.astype(np.float32), cities.Y.astype(np.float32)) , axis=1)
 is_not_prime = np.array([0 if isprime(i)else 1 for i in cities.index], dtype=np.int32 )<compute_test_metric>
data = pd.read_json('.. /input/train.json')
 data.head()
@numba.jit('f8(i8, i8, i8)', nopython=True, parallel=False)
 def cities_distance(offset, id_from, id_to):
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 if offset % 10 == 9 and is_not_prime[id_from]:
return 1.1 * distance
return distance
@numba.jit('f8(i4, i8[:])', nopython=True, parallel=False)
 def score_chunk(offset, chunk):
pure_distance, penalty = 0.0, 0.0
penalty_modulo = 9 - offset % 10
for path_index in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[path_index], chunk[path_index+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if path_index % 10 == penalty_modulo and is_not_prime[id_from]:
penalty += distance
return pure_distance + 0.1 * penalty
@numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def score_path(path):
return score_chunk(0, path)
 @numba.jit
def chunk_scores(chunk):
scores = np.zeros(10)
 pure_distance = 0
for i in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[i], chunk[i+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if is_not_prime[id_from]:
scores[9-i%10] += distance
scores *= 0.1
scores += pure_distance
return scores
def score_compound_chunk(offset, head, chunks, tail, scores, indexes_permutation=None):
if indexes_permutation is None:
indexes_permutation = range(len(chunks))
score = 0.0
last_city_id = head
for index in indexes_permutation:
chunk, chunk_scores = chunks[index], scores[index]
score += cities_distance(offset % 10, last_city_id, chunk[0])
 score += chunk_scores[(offset + 1)% 10]
last_city_id = chunk[-1]
offset += len(chunk)
 return score + cities_distance(offset % 10, last_city_id, tail)
<choose_model_class>
n_data = len(data.id)
 r_train = 1
r_eval = 0
data = data.sample(frac=1 ).reset_index(drop=True)
 i_train = int(n_data*r_train)
 data_train = data[:i_train]
i_eval = int(n_data*(1-r_eval))
data_eval = data[i_eval:]
print('n_train, n_eval = {}, {}'.format(len(data_train), len(data_eval)) )
kdt = KDTree(XY )<load_from_csv>

 x_train_name = data_train.ingredients.values
y_train_name = data_train.cuisine.values
x_eval_name = data_eval.ingredients.values
y_eval_name = data_eval.cuisine.values
tx = Tokenizer(filters='', split=None)
 tx.fit_on_texts(x_train_name)
 x_train = tx.texts_to_matrix(x_train_name)
 x_eval = tx.texts_to_matrix(x_eval_name)
 input_dim = x_train.shape[1]
ty = Tokenizer(filters='', split=None)
 ty.fit_on_texts(y_train_name)
 y_train = ty.texts_to_matrix(y_train_name)
 y_eval = ty.texts_to_matrix(y_eval_name)
 output_dim = y_train.shape[1]
y_inv = dict(map(reversed, ty.word_index.items()))
print('Shape x_train, x_eval =', x_train.shape, x_eval.shape)
 print('Shape y_train, y_eval =', y_train.shape, y_eval.shape)
 i = 0
print('x_train_name[{}] = {}'.format(i, x_train_name[i]))
print('x_train[{}]: N_total = {}, N_0 = {}, N_1 = {}'.format(
 i, len(x_train[i]), len(x_train[i][x_train[i]==0]), len(x_train[i][x_train[i]==1])))
 print('y_train_name[{}] = {}'.format(i, y_train_name[i]))
print('y_train[{}]: N_total = {}, N_0 = {}, N_1 = {}'.format(
 i, len(y_train[i]), len(y_train[i][y_train[i]==0]), len(y_train[i][y_train[i]==1])))
 print('y_inv =', y_inv )
path = np.array(pd.read_csv('.. /input/better-input-for-aka-2-opt/1515905.0538262606.csv' ).Path )<randomize_order>
model = Sequential()
model.add(Dense(256, input_dim=input_dim, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
 model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
 model.fit(x_train, y_train, epochs=40, batch_size=128)
 if r_eval > 0:
score = model.evaluate(x_eval, y_eval, batch_size=128)
 print(model.metrics_names, '=', score)
def not_trivial_permutations(iterable):
perms = permutations(iterable)
 next(perms)
 yield from perms
@lru_cache(maxsize=None)
 def not_trivial_indexes_permutations(length):
return np.array([list(p)for p in not_trivial_permutations(range(length)) ])
 path_index = np.argsort(path[:-1])
 print(f'Total score is {score_path(path):.2f}.')
 for _ in range(3):
for ids in tqdm(triplets):
i, j, k = sorted(path_index[ids])
 head, tail = path[i-1], path[k+1]
chunks = [path[i:i+1], path[i+1:j], path[j:j+1], path[j+1:k], path[k:k+1]]
chunks = [chunk for chunk in chunks if len(chunk)]
scores = [chunk_scores(chunk)for chunk in chunks]
default_score = score_compound_chunk(i-1, head, chunks, tail, scores)
 best_score = default_score
for indexes_permutation in not_trivial_indexes_permutations(len(chunks)) :
score = score_compound_chunk(i-1, head, chunks, tail, scores, indexes_permutation)
 if score < best_score:
permutation = [chunks[i] for i in indexes_permutation]
best_chunk = np.concatenate([[head], np.concatenate(permutation), [tail]])
 best_score = score
if best_score < default_score:
path[i-1:k+2] = best_chunk
path_index = np.argsort(path[:-1])
 print(f'New total score is {score_path(path):.2f}.Permutating path at indexes {i}, {j}, {k}.')
 triplets = triplets[:10**6]<compute_test_metric>
test = pd.read_json('.. /input/test.json')
 test.head()
print(f'Final score is {score_path(path):.2f}.' )<save_to_csv>
x_test = tx.texts_to_matrix(test.ingredients)
 y_test = model.predict(x_test)
 y_test_name = [y_inv[k] for k in np.argmax(y_test, axis=1)]
out = pd.DataFrame({
'id': test.id,
'cuisine': y_test_name })
 out.to_csv('submission.csv', index=False )