Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<train_model>
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
from tensorflow import keras
import pandas as pd
import json
import numpy as np
cnc_tour = np.array(read_cnc_tour("cities.cnc.tour"))
cnc_score = score_path(cnc_tour)
 print(f'Concorde distance is {distance_path(cnc_tour):.2f} and score is {score_path(cnc_tour):.2f} in {1800/60 :.1f} minutes' )<save_to_csv>
def read_dataset(path):
return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json' )
write_lkh_tour([p+1 for p in cnc_tour], "cities.cnc.lkh.tour" )<categorify>
def generate_text(data):
text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
return text_data
lkh_tour = np.array(read_lkh_tour("cities.lkh.tour", mapping=lkh_ids_mapping(cities)) )<compute_test_metric>
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]
path = np.concatenate([lkh_tour.copy() , np.array([0])])
 print(path[0:10])
 print(path[-10:])
 print(f'LKH distance is {distance_path(path):.2f} and score is {score_path(path):.2f} in {21300/60 :.1f} minutes' )<feature_engineering>
tfidf = TfidfVectorizer(binary=True)
 def tfidf_features(txt, flag):
if flag == "train":
x = tfidf.fit_transform(txt)
 else:
x = tfidf.transform(txt)
 x = x.astype('float16')
 return x
X = tfidf_features(train_text, flag="train")
 X_test = tfidf_features(test_text, flag="test" )
t1 = time.time()<compute_train_metric>
lb = LabelEncoder()
y = lb.fit_transform(target)
 y = keras.utils.to_categorical(y )
@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
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
@numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def sum_distance(ids):
res = 0
for i in numba.prange(len(ids)) :
for j in numba.prange(i + 1, len(ids)) :
res += cities_distance(0, ids[i], ids[j])
 return res<randomize_order>
model = keras.Sequential()
model.add(keras.layers.Dense(1000, kernel_initializer=keras.initializers.he_normal(seed=1), activation='relu', input_dim=3010))
model.add(keras.layers.Dropout(0.81))
model.add(keras.layers.Dense(1000, kernel_initializer=keras.initializers.he_normal(seed=2), activation='relu'))
model.add(keras.layers.Dropout(0.81))
model.add(keras.layers.Dense(20, kernel_initializer=keras.initializers.RandomNormal(mean=0.0, stddev=0.05, seed=4), activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] )
def not_trivial_permutations(iterable):
perms = permutations(iterable)
 next(perms)
 yield from perms
def _not_trivial_indexes_permutations(length):
return np.array([list(p)for p in not_trivial_permutations(range(length)) ], dtype=np.int64)
 MAX_PERMS=11
PERMS_01 = _not_trivial_indexes_permutations(1)
 PERMS_02 = _not_trivial_indexes_permutations(2)
 PERMS_03 = _not_trivial_indexes_permutations(3)
 PERMS_04 = _not_trivial_indexes_permutations(4)
 PERMS_05 = _not_trivial_indexes_permutations(5)
 PERMS_06 = _not_trivial_indexes_permutations(6)
 PERMS_07 = _not_trivial_indexes_permutations(7)
 PERMS_08 = _not_trivial_indexes_permutations(8)
 PERMS_09 = _not_trivial_indexes_permutations(9)
 PERMS_10 = _not_trivial_indexes_permutations(10)
 PERMS_11 = _not_trivial_indexes_permutations(11)
 @numba.jit(nopython=True, parallel=False)
 def not_trivial_indexes_permutations(length):
if length == 2:
return PERMS_02
elif length == 3:
return PERMS_03
elif length == 4:
return PERMS_04
elif length == 5:
return PERMS_05
elif length == 6:
return PERMS_06
elif length == 7:
return PERMS_07
elif length == 8:
return PERMS_08
elif length == 9:
return PERMS_09
elif length == 10:
return PERMS_10
elif length == 11:
return PERMS_11<find_best_params>
history = model.fit(X, y, epochs=20, batch_size=512, validation_split=0.1)
 model.save_weights("model.h5")
 print("Saved model to disk")
@numba.jit
def chunk_scores2(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
@numba.jit(nopython=True, parallel=False)
 def range_length(length):
return [p for p in range(length)]<define_search_space>
predictions_encoded = model.predict(X_test)
 predictions_encoded.shape
kdt = KDTree(XY)
 cs = [2, 3, 4]
cs_nearest = [17, 10, 8]
cs_radius = [12, 10, 6]
tuples = []
for c, n, r in zip(cs, cs_nearest, cs_radius):
print(f'
Generating tuples for N={c} with nearest={n} and radius={r}...')
 Nuplets = set()
for city_id in tqdm(cities.index, mininterval=2.0):
dists, neibs = kdt.query([XY[city_id]], k=n)
 for Nuplet in combinations(neibs[0], c):
if all(Nuplet):
Nuplets.add(tuple(sorted(Nuplet)))
 if r > 0:
neibs = kdt.query_radius([XY[city_id]], r=r, count_only=False, return_distance=False)
 for Nuplet in combinations(neibs[0], c):
if all(Nuplet):
Nuplets.add(tuple(sorted(Nuplet)))
 tuples.append(Nuplets)
 print(f'{len(Nuplets)} cities for N={c} are selected.')
 for t,i in zip(tuples, range(len(tuples))):
t = np.array(list(t))
distances = np.array(list(map(sum_distance, tqdm(t))))
order = distances.argsort()
tuples[i] = t[order]<sort_values>
predictions = lb.inverse_transform([np.argmax(pred)for pred in predictions_encoded])
 predictions
<compute_test_metric><EOS> 
test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': predictions}, columns=['id', 'cuisine'])
 sub.to_csv('output.csv', index=False )