Kaggle competition: What's Cooking? (Kernels Only)
train.drop(["timestamp", "content_type_id", "question_id", "part"], axis=1, inplace=True )<filter>
train_data = pd.read_json('.. /input/train.json')
 test_data = pd.read_json('.. /input/test.json')
 train_data['ingredients'] = train_data['ingredients'].apply(lambda list: ','.join(list ).lower())
 test_data['ingredients'] = test_data['ingredients'].apply(lambda list: ','.join(list ).lower())
 vectorizer = TfidfVectorizer(binary = True)
 train_X = vectorizer.fit_transform(train_data['ingredients'])
 test_X = vectorizer.transform(test_data['ingredients'] )
train = train[~train.index.isin(validation.index)]<groupby>
idxtuple = train_X.nonzero()
for i in range(16):
row = idxtuple[0][i]
col = idxtuple[1][i]
print('Recipe {0}: {1} = {2}'.format(row, vectorizer.get_feature_names() [col], train_X[row, col]))
results_u_val = train[["user_id", "answered_correctly"]].groupby("user_id" ).agg(["mean"])
 results_u_val.columns = ["answered_correctly_user"]
results_u2_val = train[["user_id", "prior_question_had_explanation"]].groupby("user_id" ).agg(["mean"])
 results_u2_val.columns = ["explanation_mean_user"]<groupby>
X = train.groupby("user_id" ).tail(18)
 train = train[~train.index.isin(X.index)]<groupby>
results_u_X = train[["user_id", "answered_correctly"]].groupby("user_id" ).agg(["mean"])
 results_u_X.columns = ["answered_correctly_user"]
results_u2_X = train[["user_id", "prior_question_had_explanation"]].groupby("user_id" ).agg(["mean"])
 results_u2_X.columns = ["explanation_mean_user"]<set_options>
del(train)
 gc.collect()<merge>
X = pd.merge(X, group3, left_on="task_container_id", right_index=True, how="left")
 X = pd.merge(X, results_u_X, on="user_id", how="left")
 X = pd.merge(X, results_u2_X, on="user_id", how="left")
 X = pd.merge(X, user_lecture_stats_part, on="user_id", how="left" )<merge>
train_data = pd.read_json('.. /input/train.json')
 test_data = pd.read_json('.. /input/test.json')
 cuisine = train_data['cuisine'].value_counts()
ingredients = {}
for idx, row in train_data.iterrows() :
for i in range(len(row['ingredients'])) :
ingr = row['ingredients'][i].lower()
if row['cuisine'] in ingredients:
if ingr in ingredients[row['cuisine']]:
ingredients[row['cuisine']][ingr] += 1
else:
ingredients[row['cuisine']][ingr] = 1
else:
ingredients[row['cuisine']] = {}
ingredients[row['cuisine']][ingr] = 1
ingredients = pd.DataFrame(ingredients)
 ingredients = ingredients.fillna(0 )
validation = pd.merge(validation, group3, left_on="task_container_id", right_index=True, how="left")
 validation = pd.merge(validation, results_u_val, on="user_id", how="left")
 validation = pd.merge(validation, results_u2_val, on="user_id", how="left")
 validation = pd.merge(validation, user_lecture_stats_part, on="user_id", how="left" )<categorify>
ingredients = ingredients.apply(lambda x: x.apply(lambda y: y / cuisine[x.name]))
def unit_vec(vec):
len =(vec.apply(lambda x: x * x ).sum())** 0.5
return vec.apply(lambda x: x / len)
 ingredients = ingredients.apply(unit_vec, axis = 1 )
lb_make = LabelEncoder()
X.prior_question_had_explanation.fillna(False, inplace=True)
 validation.prior_question_had_explanation.fillna(False, inplace=True)
 validation["prior_question_had_explanation_enc"] = lb_make.fit_transform(validation["prior_question_had_explanation"])
 X["prior_question_had_explanation_enc"] = lb_make.fit_transform(X["prior_question_had_explanation"] )<categorify>
train_data_vector = []
for idx, row in train_data.iterrows() :
train_data_vector.append({
'id': row['id'],
'cuisine': row['cuisine'],
'cuisine_vector': ingredients.loc[row['ingredients']].sum()
})
 total = len(train_data_vector)
 correct = 0
for i in range(total):
if train_data_vector[i]['cuisine_vector'].idxmax() == train_data_vector[i]['cuisine']:
correct += 1
print('Result on training data')
 print(f'Total: {total}, Correct: {correct}, Accuracy: {correct / total}' )
<merge><EOS> 
testing_data_vector = []
for idx, row in test_data.iterrows() :
ingredients_known = [igdt for igdt in row['ingredients'] if igdt in ingredients.index]
vector = ingredients.loc[ingredients_known].sum()
cuisine = vector.idxmax()
testing_data_vector.append({
'id': row['id'],
'cuisine_vector': vector,
'cuisine': cuisine
})
 answer = pd.DataFrame(testing_data_vector)[['id','cuisine']]
answer.to_csv('answer.csv', index = False )