Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<merge>
warnings.filterwarnings("ignore" )
train_lectures = train[train.content_type_id==True].merge(lectures_df,
left_on="content_id",
right_on="lecture_id",
how="left" )<groupby>
train_data = pd.read_json('.. /input/train.json')
 test_data = pd.read_json('.. /input/test.json' )
user_lecture_stats_part = train_lectures.groupby("user_id")[part_lectures_columns + type_of_lectures_columns].sum()<data_type_conversions>
print("Number of cuisine classes: {}".format(len(train_data.cuisine.unique())))
 train_data.cuisine.unique()
for column in user_lecture_stats_part.columns:
bool_column = column + "_boolean"
user_lecture_stats_part[bool_column] =(user_lecture_stats_part[column] > 0 ).astype(int )<set_options>
init_notebook_mode(connected=True )
del train_lectures
gc.collect()<sort_values>
def random_colors(n):
colors = []
for i in range(n):
colors.append("
return colors
train = train[train.content_type_id == False].sort_values("timestamp" ).reset_index(drop=True )<groupby>
long_recipes = train_data[train_data['ingredients'].str.len() > 30]
print("There are {} recipes consist of more than 30 ingredients.".format(len(long_recipes)))
 short_recipes = train_data[train_data['ingredients'].str.len() < 2]
print("There are {} recipes consist of less than 2 ingredients.".format(len(short_recipes)) )
group1 = train[["task_container_id", "user_id"]].groupby("task_container_id" ).agg(["count"])
 group1.columns = ["avg_questions"]
group2 = train[["task_container_id", "user_id"]].groupby("task_container_id" ).agg(["nunique"])
 group2.columns = ["avg_questions"]
group3 = group1 / group2<groupby>
all_ingredients = []
for item in train_data['ingredients']:
for ingr in item:
all_ingredients.append(ingr)
 counter = Counter()
for ingredient in all_ingredients:
counter[ingredient] += 1
print("Among {} unique ingredients in our training sample,"
"the most commonly used 20 are: ".format(len(counter)))
 counter.most_common(20 )
group3["avg_question_seen"] = group3.avg_questions.cumsum()<groupby>
features = []
all_ingredients = []
for ingredient_list in train_data['ingredients']:
features.append(ingredient_list)
 all_ingredients += ingredient_list
test_features = []
for ingredient_list in test_data['ingredients']:
test_features.append(ingredient_list )
results_u_final = train[["user_id", "answered_correctly"]].groupby("user_id" ).agg(["mean"])
 results_u_final.columns = ["answered_correctly_user"]<groupby>
tfidf = TfidfVectorizer(
 vocabulary = list(set([str(i ).lower() for i in all_ingredients])) ,
max_df=0.99, norm='l2', ngram_range=(1, 4)
 ).fit([str(i)for i in features])
 X_tr = tfidf.transform([str(i)for i in features])
 to_predict = tfidf.transform([str(i)for i in test_features])
 feature_names = tfidf.get_feature_names()
results_u2_final = train[["user_id", "prior_question_had_explanation"]].groupby("user_id" ).agg(["mean"])
 results_u2_final.columns = ["explanation_mean_user"]<merge>
def top_feats_by_class(min_tfidf=0.1, top_n=10):

 dfs = []
labels = np.unique(target)
 for label in labels:
ids = np.where(target==label)
 D = X_tr[ids].toarray()
D[D < min_tfidf] = 0
tfidf_means = np.nanmean(D, axis=0)
 topn_ids = np.argsort(tfidf_means)[::-1][:top_n]
top_feats = [(feature_names[i], tfidf_means[i])for i in topn_ids]
df = pd.DataFrame(top_feats)
 df.columns = ['feature', 'tfidf']
df['cuisine'] = label
dfs.append(df)
 return dfs
train = pd.merge(train, questions_df, left_on="content_id", right_on="question_id", how="left" )<groupby>
target = train_data['cuisine']
result_tfidf = top_feats_by_class(min_tfidf=0.1, top_n=5 )
results_q_final = train[['question_id','answered_correctly']].groupby(['question_id'] ).agg(['mean'])
 results_q_final.columns = ['quest_pct']<groupby>
warnings.filterwarnings("ignore")
 encoder = LabelEncoder()
y_transformed = encoder.fit_transform(train_data.cuisine)
 X_train, X_test, y_train, y_test = train_test_split(X_tr, y_transformed, random_state=42 )
results_q2_final = train[['question_id','part']].groupby(['question_id'] ).agg(['count'])
 results_q2_final.columns = ['quest_count']<merge>
clf1_cv = LogisticRegression(C=10, verbose=True)
 clf1_cv.fit(X_train, y_train)
 y_pred = encoder.inverse_transform(clf1_cv.predict(X_train))
y_true = encoder.inverse_transform(y_train)
 print("Accuracy score on train data: {}".format(accuracy_score(y_true, y_pred)))
 print("Accuracy score on test data: {}".format(clf1_cv.score(X_test, y_test)) )
question2 = pd.merge(questions_df, results_q2_final, on="question_id", how="left" )<merge>
_SVC = SVC(C=50, kernel='rbf', gamma=1.4, coef0=1, cache_size=3000, probability=True, verbose=True)
 OvRSVC = OneVsRestClassifier(_SVC, n_jobs=-1)
 OvRSVC.fit(X_train, y_train)
 y_pred = encoder.inverse_transform(OvRSVC.predict(X_train))
y_true = encoder.inverse_transform(y_train)
 print("Accuracy score on train data: {}".format(accuracy_score(y_true, y_pred)))
 print("Accuracy score on test data: {}".format(OvRSVC.score(X_test, y_test)) )
question2 = pd.merge(question2, results_q_final, on="question_id", how="left" )<feature_engineering>
vclf=VotingClassifier(estimators=[('clf1',clf1_cv),('clf2',OvRSVC)],voting='soft',weights=[1,2])
 vclf.fit(X_train , y_train)
 vclf.score(X_test, y_test )
question2.quest_pct = round(question2.quest_pct, 5 )<drop_column>
predicted_result = vclf.predict(to_predict)
 predicted_result_encoded = encoder.inverse_transform(predicted_result)
 result_to_submit = pd.DataFrame({'cuisine' : predicted_result_encoded , 'id' : test_data.id })
 result_to_submit = result_to_submit[[ 'id' , 'cuisine']]
result_to_submit.to_csv('submit.csv', index = False )