Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<create_dataframe>
tqdm.pandas()
init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore' )
my_submission_lbgm = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions} )<compute_test_metric>
def random_colours(number_of_colors):

 colors = []
for i in range(number_of_colors):
colors.append("
return colors
print(pearsonr(test.target.rank() ,my_submission_lbgm.target.rank()))
print(pearsonr(test2.target.rank() ,my_submission_lbgm.target.rank()))<compute_test_metric>
train = pd.read_json("/kaggle/input/whats-cooking-kernels-only/train.json")
 test = pd.read_json("/kaggle/input/whats-cooking-kernels-only/test.json" )
esemble_lgbm_cat = 0.5*valid_cb+0.5*valid
print('LightBGM auc = {:<8.5f}'.format(roc_auc_score(y_valid, valid)))
 print('catboost auc = {:<8.5f}'.format(roc_auc_score(y_valid, valid_cb)))
 print('LightBGM+catboost auc = {:<8.5f}'.format(roc_auc_score(y_valid, esemble_lgbm_cat)))
<define_variables>
raw_ingredients = [ing for ingredients in train['ingredients'] for ing in ingredients]
id_code_test = test_df['ID_code']<define_variables>
train['num_ing'] = train['ingredients'].str.len()
esemble_pred_lgbm_cat = 0.5*predictions+0.5*predictions_cb 
<create_dataframe>
longrecip = train[train['num_ing'] > 30]
print(len(longrecip))
my_submission_lbgm = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions})
 my_submission_cat = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions_cb})
 my_submission_esemble_lgbm_cat = pd.DataFrame({"ID_code" : id_code_test, "target" : esemble_pred_lgbm_cat})
<save_to_csv>
longrecip['cuisine'].value_counts()
my_submission_lbgm.to_csv('submission_lbgm.csv', index = False, header = True)
 my_submission_cat.to_csv('submission_cb.csv', index = False, header = True)
 my_submission_esemble_lgbm_cat.to_csv('my_submission_esemble_lgbm_cat.csv', index = False, header = True)
<load_from_csv>
print(longrecip[longrecip['num_ing'] == 65]['ingredients'].values)
 print('Cuisine :-',longrecip[longrecip['num_ing'] == 65]['cuisine'].values )
warnings.filterwarnings('ignore')
 df_train = pd.read_csv(".. /input/santander-customer-transaction-prediction/train.csv")
 df_test = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv')
 ID_code = df_test.ID_code.values
target = df_train.target
print(os.listdir(".. /input")) 
<define_variables>
shortrecip = train[train['num_ing']<=2]
print(len(shortrecip))
pickle_list_p_01 = glob(".. /input/baysial-blcok-p-01/*.pkl" )<load_pretrained>
shortrecip['cuisine'].value_counts()
for k in tqdm(pickle_list_p_01):
pickle_file = open(k, 'rb')
 bins1 = pickle.load(pickle_file)
 Var = int(k[28:-4])+1
for count, p in enumerate(range(Var-20, Var)) :
var = "var_" + str(p)
 mag_var = "mag_var_" + str(p)
 df_train[mag_var] = 0
df_test[mag_var] = 0
binn = bins1[count]
for i in range(len(binn)) :
try:
Idx_train = df_train[(df_train[var] >= binn[i])&(df_train[var] < binn[i+1])].index
df_train[mag_var].loc[Idx_train] = binn[i]
Idx_test = df_test[(df_test[var] >= binn[i])&(df_test[var] < binn[i+1])].index
df_test[mag_var].loc[Idx_test] = binn[i]
except:
pass
for i in tqdm(range(200)) :
train_one = df_train[df_train.target ==1]
train_zero = df_train[df_train.target ==0]
s_1 = pd.Series(train_one['mag_var_'+ str(i)].value_counts())
 s_0 = pd.Series(train_zero['mag_var_'+ str(i)].value_counts())
 map_ratio = s_1/s_0
dict1 = map_ratio.to_dict()
df_train['mag_var_'+ str(i)] = pd.CategoricalIndex(df_train['mag_var_'+ str(i)] ).map(dict1)
 df_test['mag_var_'+ str(i)] = pd.CategoricalIndex(df_test['mag_var_'+ str(i)] ).map(dict1)
 for i in df_test.columns[1:]:
df_test[i] = pd.to_numeric(df_test[i] ).fillna(0)
 df_train[i] = pd.to_numeric(df_train[i] ).fillna(0)
<init_hyperparams>
train[train['num_ing'] <= 1]
features = [c for c in df_train.columns if c not in ['ID_code', 'target']]
param = {
'bagging_freq': 5, 'bagging_fraction': 0.335, 'boost_from_average':'false', 'boost': 'gbdt',
'feature_fraction': 0.041, 'learning_rate':.01 , 'max_depth': 8, 'metric':'auc',
'min_data_in_leaf': 20, 'min_sum_hessian_in_leaf': 10.0,'num_leaves': 30,
'objective': 'binary', 'verbosity': 1, 'max_bin' : 256,
} 
<split>
[ingredient for ingredient in raw_ingredients if len(ingredient)<= 2]
train_df = df_train[features]
test_df = df_test[features]
num_folds = 5
folds = StratifiedKFold(n_splits=num_folds, shuffle=False, random_state=2319)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold idx:{}".format(fold_ + 1))
trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=200, early_stopping_rounds = 400)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)))
<save_to_csv>
' '.join(sorted([char for char in set(' '.join(raw_ingredients)) if re.findall('[^A-Za-z]', char)]))
prediction = clf.predict(df_test[features])
 sub_df = pd.DataFrame({"ID_code":ID_code})
 sub_df["target"] = prediction
sub_df.to_csv("submission_mg.csv", index=False)
<import_modules>
list(set([ingredient for ingredient in raw_ingredients if re.findall('[A-Z]+', ingredient)])) [:5]
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
from sklearn import preprocessing
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn import metrics
import tensorflow as tf
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import ComplementNB
from sklearn.covariance import EllipticEnvelope
from collections import Counter
from pandas.plotting import scatter_matrix
from sklearn.decomposition import PCA
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
import lightgbm as lgb<load_from_csv>
list(set([ingredient for ingredient in raw_ingredients if '’' in ingredient]))
train_orig = pd.read_csv(".. /input/train.csv")
 test_orig = pd.read_csv(".. /input/test.csv" )<find_best_params>
list(set([ingredient for ingredient in raw_ingredients if re.findall('-', ingredient)])) [:5]
max_corr = -1
min_corr = 1
for i in range(199):
for j in range(i + 1, 200):
current = correlations.iloc[i, j]
min_corr = min(min_corr, current)
 max_corr = max_corr if max_corr > current else current
print('Minimum correlation: ', min_corr)
 print('Maximum correlation: ', max_corr )<normalization>
temp_ing = list(set([ingredient for ingredient in raw_ingredients if re.findall('[0-9]', ingredient)]))
temp_ing[:6]
scaler = preprocessing.StandardScaler()
scaler.fit(X)
 X = scaler.transform(X)
 X_test = scaler.transform(X_test )<split>
units = ['inch', 'oz', 'lb', 'ounc', '%']
@interact(unit=units)
 def f(unit):
ingredients_df = pd.DataFrame([ingredient for ingredient in raw_ingredients if unit in ingredient], columns=['ingredient'])
 return ingredients_df.groupby(['ingredient'] ).size().reset_index(name='count' ).sort_values(['count'], ascending=False )
seed = 125
X_train, X_dev, y_train, y_dev = train_test_split(X, y, test_size=0.05, random_state=seed, stratify = y)
 print('X_train shape: ', X_train.shape)
 print('y_train shape: ', y_train.shape)
 print('X_dev shape: ', X_dev.shape)
 print('y_dev shape: ', y_dev.shape )<define_variables>
keywords = ['american', 'greek', 'filipino', 'indian', 'jamaican', 'spanish', 'italian', 'mexican', 'chinese', 'thai',
'vietnamese', 'cajun', 'creole', 'french', 'japanese', 'irish', 'korean', 'moroccan', 'russian',
]
d ={}
for k in keywords:
temp = [ingredient for ingredient in raw_ingredients if k in ingredient]
d[k] = temp
models = {}<train_model>
top = Counter([item for sublist in train['ingredients'] for item in sublist])
 print(top.most_common(20))
lr = LogisticRegression(random_state=0, solver='saga', C=0.1 ).fit(X_train, y_train)
 models['LogisticRegression'] = lr<train_model>
labels = ['greek', 'southern_us', 'filipino', 'indian', 'jamaican',
'spanish', 'italian', 'mexican', 'chinese', 'british', 'thai',
'vietnamese', 'cajun_creole', 'brazilian', 'french', 'japanese',
'irish', 'korean', 'moroccan', 'russian']
templist=[]
for cus in labels:
lisofing=[]
for lis in train[train['cuisine'] == cus]['ingredients']:
for ing in lis:
lisofing.append(ing)
 templist.append([cus,len(list(set(lisofing)))])
 Unique_ing = pd.DataFrame(templist,columns=['cuisine','unique_ing'] ).sort_values(by='unique_ing',ascending=False )
qd = QuadraticDiscriminantAnalysis(reg_param=0.5, tol=1e-10 ).fit(X_train, y_train)
 models['QuadraticDiscriminantAnalysis'] = qd<train_model>
train = train[train['num_ing'] > 1]
rfc = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0 ).fit(X_train, y_train)
 models['RandomForestClassifier'] = qd<train_model>
train = train[train['num_ing']<60]
gclf = GaussianNB().fit(X_train, y_train)
 models['GaussianNB'] = qd<compute_train_metric>
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text = ' '.join(ingredients)
 ingredients_text = ingredients_text.lower()
ingredients_text = ingredients_text.replace('-', ' ')
 words = []
for word in ingredients_text.split() :
word = re.sub("[0-9]"," ",word)
 word = re.sub(( r'\b(oz|ounc|ounce|pound|lb|inch|inches|kg|to)\b'), ' ', word)
 if len(word)<= 2: continue
word = unidecode.unidecode(word)
 word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return ' '.join(words )
for name, model in models.items() :
y_train_predicted = model.predict_proba(X_train)
 train_score = metrics.roc_auc_score(y_train, y_train_predicted[:, 1])
 y_dev_predicted = gclf.predict_proba(X_dev)
 dev_score = metrics.roc_auc_score(y_dev, y_dev_predicted[:, 1])
 print(name, ": ")
 print("Train score: {train}, test score: {test}".format(train=train_score, test=dev_score))<train_model>
for ingredient, expected in [ 
('Eggs', 'egg'), 
('all-purpose flour', 'all purpose flour'), 
('purée', 'puree'), 
('1% low-fat milk', 'low fat milk'), 
('half & half', 'half half'), 
('safetida(powder)', 'safetida(powder)')
 ]:
actual = preprocess([ingredient])
 assert actual == expected, f'"{expected}" is excpected but got "{actual}"'
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_dev, y_dev)
 param = {
'boost_from_average':'false',
'boost': 'goss',
'feature_fraction': 0.1,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 100,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 20,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
num_round = 150000
clf = lgb.train(param, lgb_train, num_round, valid_sets = lgb_eval, verbose_eval=100,
early_stopping_rounds = 3000 )<find_best_params>
train['x'] = train['ingredients'].progress_apply(preprocess)
 test['x'] = test['ingredients'].progress_apply(preprocess)
 train.head()
clf.best_iteration<compute_train_metric>
def apply_word2vec(sentences):
vectorizer = Word2Vec(
 sentences,
size=500,
window=20,
min_count=3,
sg=1,
iter=20 
)
 def to_vector(sentence):
words = [word for word in sentence if word in vectorizer.wv.vocab]
if words:
return np.mean(vectorizer[words], axis=0)
 else:
return np.zeros(500)
 return np.array([to_vector(sentence)for sentence in sentences])
 def apply_fasttext(sentences):
vectorizer = FastText(
 size=500,
window=20,
min_count=3,
sg=1,
iter=20 
)
 vectorizer.build_vocab(sentences)
 vectorizer.train(sentences, total_examples=vectorizer.corpus_count, epochs=vectorizer.iter)
 def to_vector(sentence):
words = [word for word in sentence if word in vectorizer.wv.vocab]
if words:
return np.mean(vectorizer.wv[words], axis=0)
 else:
return np.zeros(500)
 return np.array([to_vector(sentence)for sentence in sentences])
 def train_model(x, y, n_splits=3):
model = LogisticRegression(C=10, solver='sag', multi_class='multinomial', max_iter=300, n_jobs=-1)
 i = 0
accuracies = []
kfold = KFold(n_splits)
 for train_index, test_index in kfold.split(x):
classifier = LogisticRegression(C=10, solver='sag', multi_class='multinomial', max_iter=300, n_jobs=-1)
 classifier.fit(x[train_index], y[train_index])
 predictions = classifier.predict(x[test_index])
 accuracies.append(accuracy_score(predictions, y[test_index]))
i += 1
average_accuracy = sum(accuracies)/ len(accuracies)
 return average_accuracy
def run_experiment(preprocessor):
train = json.load(open('/kaggle/input/whats-cooking-kernels-only/train.json'))
target = [doc['cuisine'] for doc in train]
lb = LabelEncoder()
y = lb.fit_transform(target)
 x = preprocessor.fit_transform(train)
 return train_model(x, y)
y_train_predicted = clf.predict(X_train, num_iteration=clf.best_iteration)
 train_score = metrics.roc_auc_score(y_train, y_train_predicted)
 y_dev_predicted = clf.predict(X_dev, num_iteration=clf.best_iteration)
 dev_score = metrics.roc_auc_score(y_dev, y_dev_predicted)
 print("Train score: {train}, test score: {test}".format(train=train_score, test=dev_score))
print("Confsuion matrix(for treshold 0.5): ")
 print("Train set: ")
 print(metrics.confusion_matrix(y_train, [1 if x > 0.5 else 0 for x in y_train_predicted]))
print("Dev set: ")
 print(metrics.confusion_matrix(y_dev, [1 if x > 0.5 else 0 for x in y_dev_predicted]))<predict_on_test>
results = []
for(name, preprocessor)in [ 
('TfidfVectorizer() ', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer() , 
)) , 
('TfidfVectorizer(binary=True)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(binary=True), 
)) , 
('TfidfVectorizer(min_df=3)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(min_df=3), 
)) , 
('TfidfVectorizer(min_df=5)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(min_df=5), 
)) , 
('TfidfVectorizer(max_df=0.95)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(max_df=0.95), 
)) , 
('TfidfVectorizer(max_df=0.9)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(max_df=0.9), 
)) , 
('TfidfVectorizer(sublinear_tf=True)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(sublinear_tf=True), 
)) , 
('TfidfVectorizer(strip_accents=unicode)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(strip_accents='unicode'), 
)) ,
]:
start = time.time()
accuracy = run_experiment(preprocessor)
 execution_time = time.time() - start
results.append({
'name': name,
'accuracy': accuracy,
'execution time': f'{round(execution_time, 2)}s'
})
 pd.DataFrame(results, columns=['name', 'accuracy', 'execution time'] ).sort_values(by='accuracy', ascending=False )
y_test_predicted = clf.predict(X_test, num_iteration=clf.best_iteration )<save_to_csv>
vectorizer = TfidfVectorizer(sublinear_tf=True )
submission = pd.DataFrame({
"ID_code": test_orig["ID_code"],
"target": y_test_predicted
})
 submission.to_csv('sample_submission.csv', index=False )<import_modules>
X_train = vectorizer.fit_transform(train['x'].values)
 X_train.sort_indices()
X_test = vectorizer.transform(test['x'].values )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<load_from_csv>
label_encoder = LabelEncoder()
Y_train = label_encoder.fit_transform(train['cuisine'].values )
df = pd.read_csv('.. /input/train.csv',index_col='ID_code')
 df.head()<load_from_csv>
classifier = SVC(C=100,
	 			 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=None )
y = df['target']
X = df.drop('target',axis=1)
 test = pd.read_csv('.. /input/test.csv',index_col='ID_code' )<import_modules>
model = OneVsRestClassifier(classifier, n_jobs=4)
 model.fit(X_train, Y_train )
import lightgbm as lgb
from sklearn.model_selection import KFold<train_model>
print("Predict on test data...")
 Y_test = model.predict(X_test)
 Y_pred = label_encoder.inverse_transform(Y_test )
<load_from_csv><EOS> 
test_id = test['id']
sub = pd.DataFrame({'id': test_id, 'cuisine': Y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('submission.csv', index=False )