Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<import_modules>
try:
stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
reload(sys)
 sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
sys.setdefaultencoding('utf8')
 except:
pass
random_state = 101
RS = 101
warnings.filterwarnings('ignore')
 wnl = WordNetLemmatizer()
print(numba.__version__ )<data_type_conversions>
def pre_processing_(recipe):
wnl = WordNetLemmatizer()
recipe = [str.lower(ingredient)for ingredient in recipe]
recipe = [delete_brand_(ingredient)for ingredient in recipe]
recipe = [delete_state_(ingredient)for ingredient in recipe]
recipe = [delete_comma_(ingredient)for ingredient in recipe]
recipe = [original_(ingredient)for ingredient in recipe]
recipe = [delete_space_(ingredient)for ingredient in recipe if len(delete_space_(ingredient)) >2]
return recipe
def delete_brand_(ingredient):
ingredient = re.sub("country crock|i can't believe it's not butter!|bertolli|oreo|hellmann's", '', ingredient)
 ingredient = re.sub("red gold|hidden valley|original ranch|frank's|redhot|lipton", '', ingredient)
 ingredient = re.sub("recipe secrets|eggland's best|hidden valley|best foods|knorr|land o lakes", '', ingredient)
 ingredient = re.sub("sargento|johnsonville|breyers|diamond crystal|taco bell|bacardi", '', ingredient)
 ingredient = re.sub("mccormick|crystal farms|yoplait|mazola|new york style panetini", '', ingredient)
 ingredient = re.sub("ragu|soy vay|tabasco|truvía|crescent recipe creations|spice islands", '', ingredient)
 ingredient = re.sub("wish-bone|honeysuckle white|pasta sides|fiesta sides", '', ingredient)
 ingredient = re.sub("veri veri teriyaki|artisan blends|home originals|greek yogurt|original ranch", '', ingredient)
 ingredient = re.sub("jonshonville", '', ingredient)
 ingredient = re.sub("old el paso|pillsbury|progresso|betty crocker|green giant|hellmanns|hellmannâ€", '', ingredient)
 ingredient = re.sub("oscar mayer deli fresh smoked", '', ingredient)
 return ingredient
def delete_state_(ingredient):
ingredient = re.sub('frozen|chopped|ground|fresh|powdered', '', ingredient)
 ingredient = re.sub('sharp|crushed|grilled|roasted|sliced', '', ingredient)
 ingredient = re.sub('cooked|shredded|cracked|minced|finely', '', ingredient)
 return ingredient
def delete_comma_(ingredient):
ingredient = ingredient.split(',')
 ingredient = ingredient[0]
return ingredient
def original_(ingredient):
ingredient = re.sub('[0-9]', '', ingredient)
 ingredient = ingredient.replace("oz.", '')
 ingredient = re.sub('[&%() ®™/]', '', ingredient)
 ingredient = re.sub('[-.]', '', ingredient)
 ingredient = wnl.lemmatize(ingredient)
 return ingredient
def delete_space_(ingredient):
ingredient = ingredient.strip()
return ingredient
cities = pd.read_csv('.. /input/cities.csv')
 cities["X"] = cities["X"].astype(np.float64)
 cities["Y"] = cities["Y"].astype(np.float64)
 is_not_prime = np.array([0 if isprime(i)else 1 for i in cities.index], dtype=np.int32)
 cities["is_not_prime"] = is_not_prime
cities.head(10 )<split>
lemmatizer = WordNetLemmatizer()
replaceC = [(' ', ' '),(' ', ' '),('ú', 'u'),('é', 'e'),('è', 'e'),('î', 'i'),('â', 'a'),('í', 'i'),('â€™', ''),('€', ''),('ç', 'c'), 
('half & half', 'halfandhalf'),('half half', 'halfandhalf'),('all purpose', ''), 
('seasoning mix', 'seasoning'),('style seasoning', 'seasoning'),('salt free', 'saltfree'),('taco seasoning reduced sodium', 'taco seasoning'), 
('less sodium taco seasoning', 'low sodium taco seasoning'),('old el paso', ''),('old bay','oldbay'), 
('jerk rub', 'jerk'),('taco bell home originals', ''),('taco bell', ''),('low sodium', ''), 
('frozen broccoli florets', 'broccoliflorets'),('broccoli florets', 'broccoliflorets'), 
('high gluten bread flour', 'highglutenflour'),('high gluten flour', 'highglutenflour'), 
('chapatti', 'chapati'),('corn flour', 'cornflour'),('not low fat', ''),('low fat', ''), 
('cauliflorets florets','cauliflorets'),('cauliflorets flowerets','cauliflorets'),('cauliflower','cauliflorets'),('cauliflower florets','cauliflorets'),('caulifloretsets', 'cauliflorets'), 
('bread flour','flour'),('self raising flour','selfraisingflour'),('self rising cake flour','selfraisingflour'),('self rising flour','selfraisingflour'),
]
rmC = u'®™,.;!() []'’"-_@
p0 = re.compile(r'\s*(oz|ounc|ounce|pound|lb|inch|inches|kg|to)\s*[^a-z]')
 def preprocess_ing(Y, doReplaces=True, rmSpaces=False):
if len(Y)<2: return []
if rmSpaces: Y = [_.replace(' ',' ' ).replace(' ','_')for _ in Y]
ingredients_text = ' '.join(Y)
 ingredients_text = ingredients_text.lower()
if doReplaces:
for rm in rmC:
try:
ingredients_text = ingredients_text.replace(rm, ' ')
 except Exception as e:
print(e)
 print(rm)
 print(ingredients_text)
 for rc in replaceC: ingredients_text = ingredients_text.replace(rc[0], rc[1])
 for rc in replaceC: ingredients_text = ingredients_text.replace(rc[0], rc[1])
 if rmSpaces:
for rc in replaceC[2:]:
ingredients_text = ingredients_text.replace(rc[0].replace(' ','_'), rc[1].replace(' ','_'))
words = []
if rmSpaces: split_char='_'
else: split_char=' '
for word in ingredients_text.split(split_char):
word = re.sub(p0, split_char, word.strip())
 if re.findall('[0-9]', word): continue
if len(word)<= 2: continue
if '’' in word: continue
word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return words
def preprocess_data(X, preproMode, doReplaces=True, rmSpaces=False):
_X = []
for x in X:
if preproMode=='KK': x['ingredients'] = preprocess_ing(x['ingredients'], doReplaces=doReplaces, rmSpaces=rmSpaces)
 elif preproMode=='AV': x['ingredients'] = pre_processing_(x['ingredients'])
 _X.append(x)
 return _X
def conv_secs(s):
mins, secs = divmod(s,60)
 if mins>=60: hours, mins = divmod(mins,60)
 else: hours=0
if hours<24: return '%02d:%02d:%02d' %(hours, mins, secs)
 else:
days, hours = divmod(hours,24)
 return '%02d-%02d:%02d:%02d' %(days, hours, mins, secs)
 def do_train_fit(model, model_name, USEOVR):
t0 = time.time()
if USEOVR:
model = OneVsRestClassifier(model, n_jobs=-1)
 model_name = 'OVR_'+model_name
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid)
 y_pred_train = model.predict(X_train)
 need_time = time.time() -t0
print('%s %s Acc_T: %.4f Acc_V: %.4f [%s]' %(tfidf_name, model_name, accuracy_score(y_pred_train, y_train), accuracy_score(y_pred_valid, y_valid), conv_secs(need_time)))
 return model, model_name, y_pred_valid
XY = np.stack(( cities.X, cities.Y), axis=1)
 @numba.jit('f8(i4, i8[:])', nopython=True, parallel=False)
 def distance_chunk(offset, chunk):
pure_distance = 0.0
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
return pure_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('f8(i8[:])', nopython=True, parallel=False)
 def distance_path(path):
return distance_chunk(0, path )<save_to_csv>
print("Read Dataset...")
 def read_dataset(path):
try:return json.load(open(path, encoding='utf-8'))
except:return json.load(open(path))
_HOMEFOLDER_ = os.environ['HOME']
DATA_FOLDER = '.. /input/'
train = read_dataset(DATA_FOLDER+'train.json')
 submission = read_dataset(DATA_FOLDER+'test.json')
 target = [doc['cuisine'] for doc in train]
print("Label Encode the Target Variable...")
 lb = LabelEncoder()
y = lb.fit_transform(target)
 class_weights_ = class_weight.compute_class_weight('balanced', np.unique(y), y)
 class_weights = {}
for i in range(len(class_weights_)) : class_weights[i]=100/class_weights_[i]
X = train
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.00010, random_state=random_state)
 print(len(X_train), len(X_valid))
print("Prepare text data...")
 def generate_text(data, preproMode, doReplaces=True, rmSpaces=False):
data = preprocess_data(data, preproMode=preproMode, doReplaces=doReplaces, rmSpaces=rmSpaces)
 text_data = [" ".join(doc['ingredients'])for doc in data]
return text_data
doReplaces = False
rmSpaces = False
preproMode = 'KK'
preproMode = 'AV'
train_text = generate_text(X_train, preproMode=preproMode, doReplaces=doReplaces, rmSpaces=rmSpaces)
 valid_text = generate_text(X_valid, preproMode=preproMode, doReplaces=doReplaces, rmSpaces=rmSpaces)
 submission_text = generate_text(submission, preproMode=preproMode, doReplaces=doReplaces, rmSpaces=rmSpaces)
 print("Generation Done! " )
def write_lkh_files(lkh_df, filename_par, filename_tsp, filename_tour, filename_initial_tour=None, real=False, bw=False):
l = len(lkh_df)
 with open(filename_par, "w")as f:
f.write("PROBLEM_FILE = %s
" % filename_tsp)
 f.write("MOVE_TYPE = 8
")
 f.write("PATCHING_C = 3
")
 f.write("PATCHING_A = 2
")
 f.write("RUNS = 2
")
 f.write("OUTPUT_TOUR_FILE = %s
" % filename_tour)
 f.write("SEED = %d
" % seed)
 f.write("CANDIDATE_SET_TYPE = POPMUSIC
")
 f.write("POPMUSIC_SOLUTIONS = 12
")
 f.write("POPMUSIC_SAMPLE_SIZE = 14
")
 f.write("POPMUSIC_TRIALS = 1000
")
 f.write("POPMUSIC_MAX_NEIGHBORS = 14
")
 f.write("INITIAL_PERIOD = 2500
")
 f.write("MAX_TRIALS = 1000
")
 f.write("INITIAL_TOUR_ALGORITHM = QUICK-BORUVKA
")
 if filename_initial_tour is not None:
f.write("INITIAL_TOUR_FILE = %s
" % filename_initial_tour)
 f.write("TRACE_LEVEL = 1
")
 if bw == True:
f.write("BWTSP = %d %d %d
" %(len(lkh_df[lkh_df["is_not_prime"] == 0]), 9, 9999999999))
with open(filename_tsp, "w")as f:
f.write("NAME : SOLVER
")
 f.write("COMMENT : %d points
" % l)
 f.write("TYPE : TSP
")
 f.write("DIMENSION : %d
" % l)
 f.write("EDGE_WEIGHT_TYPE : EUC_2D
")
 f.write("NODE_COORD_TYPE : TWOD_COORDS
")
 f.write("NODE_COORD_SECTION
")
 for idx, row in lkh_df.iterrows() :
if real:
f.write("%d %.12f %.12f
" %(row["CityId"] + 1,(row["X"]),(row["Y"])))
 else:
f.write("%d %.9f %.9f
" %(row["CityId"] + 1,(row["X"])*1000.0,(row["Y"]*1000.0)))
 def read_lkh_tour(filename, mapping = None):
with open(filename, 'r')as f:
tour = []
i = 0
for line in f:
i = i + 1
if i > 6:
tour.append(line.rstrip('
'))
tour = [int(t)for t in tour if t != 'EOF']
tour = tour[:-1]
tour_map = [mapping[int(t)] for t in tour]
return tour_map
def lkh_ids_mapping(lkh_df):
mapping = {}
for idx, row in lkh_df.iterrows() :
mapping[int(row["CityId"] + 1)] = idx
return mapping
def read_cnc_tour(filename):
tour = open(filename ).read().split() [1:]
tour = list(map(int, tour))
if tour[-1] == 0: tour.pop()
return tour
def write_lkh_tour(path, filename_tour, comment="None"):
with open(filename_tour, "w")as f:
f.write("NAME : %s
" % filename_tour)
 f.write("COMMENT : %s
" % comment)
 f.write("COMMENT :
")
 f.write("TYPE : TOUR
")
 f.write("DIMENSION : %d
" % len(path))
f.write("TOUR_SECTION
")
 for p in path:
f.write("%d
" % p)
 f.write("-1
")
 f.write("EOF
" )<set_options>
model_njobs = -1
model_list = [ 
('SVC_C50_g1p4_d3_c1',
SVC(C = 50, gamma=1.4, kernel='rbf', degree=3, coef0=1, shrinking=True, tol=0.0001, probability=True,
cache_size=2000, class_weight=None, verbose=False, max_iter=-1, decision_function_shape=None, random_state=RS),
{'C':[30, 50, 100, 150, 220], 'gamma': [0.1, 0.75, 1, 3, 5], 'degree': [1,2,3,4],
'coef0': [0, 0.5, 1], 'kernel':('linear', 'rbf', 'poly')},
{}), 
('LR_C7_l2_NewCG_max200_dF',
LogisticRegression(penalty='l2', C=8,
max_iter = 200, dual = False,
solver='newton-cg',
multi_class='ovr',
n_jobs = model_njobs,
random_state=RS),
{'C': [0.1,0.5,1,5,10,15], 'solver': ['sag', 'newton-cg', 'lbfgs'],
'max_iter':[100,200,300], 'dual':[True, False], 'multi_class': ['multinomial', 'ovr']},
{}), 
('RFC_150_None',
RandomForestClassifier(n_estimators=150, criterion='gini', max_depth=None, min_samples_split=2,
max_features='log2', max_leaf_nodes=None, min_impurity_decrease=0.0,
min_impurity_split=None, bootstrap=True, oob_score=False, n_jobs=model_njobs,
random_state=RS, verbose=0, warm_start=False, class_weight=None),
{'n_estimators':[25, 50, 100, 150, 200], 'max_features':['sqrt', 'log2', None], },
{'n_estimators':150, 'max_features':'log2'}), 
('SGD_l_l2_opt_a1m5',
SGDClassifier(loss='log', penalty='l2', alpha=1e-05, l1_ratio=0.15,
fit_intercept=True, max_iter=1000, tol=1e-03, shuffle=True,
verbose=0, epsilon=0.1, n_jobs=model_njobs, random_state=RS,
learning_rate='optimal', eta0=0.0, power_t=0.5, class_weight=None,
warm_start=False, average=False, n_iter=None),
{'loss': ['hinge','log','modified_huber','squared_hinge','perceptron',],
'alpha': [1.0e-6,1.0e-5, 1.0e-4, 1.0e-3, 1.0e-2], 'learning_rate': ['optimal']},
{'loss': 'log', 'learning_rate': 'optimal', 'alpha': 1e-05}), 
('LinSVC', LinearSVC() , {}, {}), 
('ABC',
AdaBoostClassifier(base_estimator=None, n_estimators=50, learning_rate=1.0,
algorithm='SAMME.R', random_state=RS),
{'n_estimators':[25, 50, 100, 150, 200], 'algorithm': ['SAMME.R', 'SAMME']}, {}), 
('GBC',
GradientBoostingClassifier(loss='deviance', learning_rate=0.1, n_estimators=100, subsample=1.0,
criterion='friedman_mse', min_samples_split=2, max_depth=3,
min_impurity_decrease=0.0, min_impurity_split=None, init=None,
random_state=RS, max_features=None, verbose=0, max_leaf_nodes=None,
warm_start=False, presort='auto', 
),
{'loss':['deviance','exponential'], 'n_estimators':[100, 150, 200, 300], 'max_features':['sqrt', 'log2', None]},
{}), 
('Perceptron_50', Perceptron(penalty=None, alpha=0.0001, fit_intercept=True, max_iter=None, tol=1e-4, shuffle=True,
verbose=0, eta0=1.0, n_jobs=model_njobs, random_state=RS, class_weight=None,
warm_start=False, n_iter=None),
{'penalty':['l2', 'l1', 'elasticnet', None], 'max_iter':[10,100,500,1000], 'eta0':[0.5,1.0,1.5]}, {}), 
('KNC_50', KNeighborsClassifier(n_neighbors=100, weights='uniform', algorithm='auto', leaf_size=30,
p=2, metric='minkowski', metric_params=None, n_jobs=model_njobs),
{'weights':['distance', 'uniform'], 'algorithm':['ball_tree', 'kd_tree', 'brute'], 'n_neighbors': [10,20,30,50,70],
'leaf_size': [10,15,20,30]},
{}), 
('LinearSVC_l2_dF', LinearSVC(penalty='l2', loss='squared_hinge', dual=False, tol=1e-4, C=1.0, multi_class='ovr',
fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=RS, max_iter=2000),
{'dual':[True, False], 'C': [0.1, 0.25, 0.4, 0.5, 0.6, 0.75, 1.0, 1.25, 1.5, 2, 3, 4]},{}), 
('SVC_kg', SVC(C=200, kernel='rbf', degree=3,gamma=1, coef0=1, shrinking=True,tol=0.001, probability=False,
cache_size=200,class_weight=None, verbose=False, max_iter=-1,decision_function_shape=None,random_state=RS), {},{}), 
('RFC',
ExtraTreesClassifier(n_estimators=50, criterion='gini', max_depth=None, min_samples_split=2,
max_features=None, max_leaf_nodes=None, min_impurity_decrease=0.0,
min_impurity_split=None, bootstrap=False, oob_score=False, n_jobs=model_njobs, random_state=RS, verbose=0,
warm_start=False, class_weight=None),
{'max_features':['sqrt', 'log2', None],'n_estimators':[100, 500],'min_samples_leaf': [10,50,100,200,500]}, {}), 
('XGB',
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bytree=1, gamma=1, learning_rate=0.01, max_delta_step=0,
max_depth=12, min_child_weight=1, missing=None, n_estimators=1000,
n_jobs=2, nthread=None, objective='multi:softprob', random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=True, subsample=0.8),
{}, {}),
]
%%bash -e
wget http://akira.ruc.dk/~keld/research/LKH-3/LKH-3.0.5.tgz 
(tar xvfz LKH-3.0.5.tgz)
(cd LKH-3.0.5 && make CC=gcc CFLAGS='-IINCLUDE -Ofast -march=native -mtune=native -fPIC')
(cd LKH-3.0.5 && cp LKH.. /)
 rm -rf LKH-3.0.5 LKH-3.0.5.tgz<load_pretrained>
if True:
SEL_MODEL = 0
model_name, model, gs_params, best_params = model_list[SEL_MODEL]
model, model_name, y_pred_valid = do_train_fit(model, model_name, True )
write_lkh_files(cities, "cities.lkh.par", "cities.lkh.tsp", "cities.lkh.tour",
filename_initial_tour="cities.cnc.lkh.tour", real=False, bw=False )<import_modules>
RUNMODE = 'VOTE_S'
SEL_MODEL = 0
USECLASSWEIGHTS = True
USECLASSWEIGHTS = False
USEOVR = True
print("features: ", X_train.shape)
 starttime = time.time()
model_name, model, gs_params, best_params = model_list[SEL_MODEL]
if 'class_weight' in model.__doc__ and USECLASSWEIGHTS:
model_name = 'CW_'+model_name
model.__setattr__('class_weight', class_weights)
 if RUNMODE.split('_')[0] == 'DOSCAN':
if RUNMODE[-1]=='R':clf = RandomizedSearchCV(model, param_distributions=gs_params, n_iter=20, verbose=10, n_jobs=-1)
 elif RUNMODE[-1]=='G':clf = GridSearchCV(model, gs_params, cv=3, verbose=10, n_jobs=-1)
 clf.fit(X_train, y_train)
 print('best score: ', clf.best_score_)
 print("best params: ", clf.best_params_)
 print('best est: ', clf.best_estimator_)
 with open("bestparams.txt", "w")as data:
data.write(str(clf.best_params_))
elif RUNMODE == 'KFOLD':
kfold = StratifiedKFold(n_splits=5, random_state=random_state ).split(X_train, y_train)
 scores = []
for k,(train, test)in enumerate(kfold):
training_data = X_train[train]
training_data.sort_indices()
model.fit(training_data, y_train[train])
 score = model.score(X_train[test], y_train[test])
 scores.append(score)
 print('Fold: %2d, Acc: %.3f' %(k+1, score))
elif RUNMODE == 'FIT':
model, model_name, y_pred_valid = do_train_fit(model, model_name, USEOVR)
 elif RUNMODE == 'FIT_BOTH':
model, model_name, y_pred_valid = do_train_fit(model, model_name, False)
 model, model_name, y_pred_valid = do_train_fit(model, model_name, True)
 elif RUNMODE.split('_')[0] == 'VOTE':
SEL_MODELS = [0,1,3]
WEIGHTS = [5,2,2]
vote_est = []
voting = 'hard'
if RUNMODE[-1]=='S': voting = 'soft'
v_model_name = 'V' + voting[0].upper()
for _ in SEL_MODELS:
vote_est.append(( model_list[_][0], model_list[_][1]))
v_model_name += '-'+ vote_est[-1][0]
print('*'*66)
 print('* Will use total %d ests' % len(vote_est))
for x,_ in vote_est: print('* %s' % x)
 model = VotingClassifier(estimators = vote_est,
voting = voting,
weights = WEIGHTS,
n_jobs=-1)
 if USEOVR: model = OneVsRestClassifier(model, n_jobs=4)
 model.fit(X_train, y_train)
 print("Test the model on the validation data")
 y_pred_valid = model.predict(X_valid)
 print('Accuracy: %.4f' % accuracy_score(y_pred_valid, y_valid))
print('%s %s' %(tfidf_name, model_name))
print("Time: ", conv_secs(time.time() -starttime))
<install_modules>
print("Predict on submision data...")
 y_submission = model.predict(X_submission)
 y_pred = lb.inverse_transform(y_submission)
 print("Generate Submission File...")
 submission_id = [doc['id'] for doc in submission]
sub = pd.DataFrame({'id': submission_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('%s_%s_%s.csv' %(tfidf_name, model_name, str(random_state)) , index=False )
<set_options><EOS> 
if False:
im = -1
exclude_ids = [0,10]
for model_name, model, gs_params, best_params in model_list:
im+=1
if im in exclude_ids: continue
try:
print('*'*66)
 print(im, model_name)
 try:model, model_name, y_pred_valid = do_train_fit(model, model_name, True)
 except:pass
except Exception as e:
print(e)