Kaggle competition: What's Cooking? (Kernels Only)
fold_information = pd.read_csv(".. /input/fold-information/10_fold_information.csv" )<data_type_conversions>
train_df = pd.read_json(".. /input/train.json" ).set_index("id")
 train_df.head()
splits=[]
for i in fold_information.columns:
a = np.array(list(set(train_df.index)-set(list(fold_information[i].dropna()))))
a.sort()
splits.append(( np.array(fold_information[i].dropna().astype(int)) ,a))<set_options>
le = LabelEncoder()
y = le.fit_transform(train_df["cuisine"].values)
 X = train_df["ingredients"].values
x_train, x_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.33 )
del fold_information
del a
gc.collect()<drop_column>
tfidf_vectorizer = TfidfVectorizer(analyzer = "word", preprocessor = lambda x: x, tokenizer = lambda x: x, token_pattern = None)
 tfidf_train = tfidf_vectorizer.fit_transform(x_train)
 tfidf_valid = tfidf_vectorizer.transform(x_valid)
 tfidf_train
train_features = train_df.drop(['target','ID_code'], axis = 1)
 test_features = test_df.drop(['ID_code'],axis = 1)
 train_target = train_df['target']<prepare_x_and_y>
LogisticRegression().get_params().keys()
fake_data = test_features.iloc[index_f_data]
real_data = test_features.iloc[index_r_data]<concatenate>
logreg = LogisticRegression(penalty = "L2", dual = False, tol = 1e-5, C = 1.0, fit_intercept = True, intercept_scaling = 1,
class_weight = None, random_state = None, solver = "saga", max_iter = 250, multi_class = "ovr",
verbose = 0, warm_start = False, n_jobs = 1)
 logreg.fit(tfidf_train, y_train)
 train_pred = logreg.predict(tfidf_train)
 valid_pred = logreg.predict(tfidf_valid )
train_all_real = pd.concat([train_features,real_data], axis = 0 )<count_duplicates>
print("Train Accuracy: "+str(accuracy_score(y_train, train_pred)))
 print("Valid Accuracy: "+str(accuracy_score(y_valid, valid_pred)))
 print("Train F1 Score: "+str(f1_score(y_train, train_pred, average = "weighted")))
 print("Valid F1 Score: "+str(f1_score(y_valid, valid_pred, average = "weighted")))
 print("Train Precision: "+str(precision_score(y_train, train_pred, average = "weighted")))
 print("Valid Precision: "+str(precision_score(y_valid, valid_pred, average = "weighted")))
 print("Train Recall: "+str(recall_score(y_train, train_pred, average = "weighted")))
 print("Valid Recall: "+str(recall_score(y_valid, valid_pred, average = "weighted")) )
for f in train_all_real.columns[0:200]:
train_all_real[f+'duplicate'] = train_all_real.duplicated(f,False ).astype(int )<categorify>
Cs = np.geomspace(1e-8, 1e8, 30)
 penalties = ["L1", "L2"]
accuracy_train = {}
accuracy_valid = {}
accuracy_diff = {}
f1_train = {}
f1_valid = {}
f1_diff = {}
precision_train = {}
precision_valid = {}
precision_diff = {}
recall_train = {}
recall_valid = {}
recall_diff = {}
for i, penalty in enumerate(penalties):
accuracy_train[penalty] = np.zeros(len(Cs))
f1_train[penalty] = np.zeros(len(Cs))
precision_train[penalty] = np.zeros(len(Cs))
recall_train[penalty] = np.zeros(len(Cs))
accuracy_valid[penalty] = np.zeros(len(Cs))
f1_valid[penalty] = np.zeros(len(Cs))
precision_valid[penalty] = np.zeros(len(Cs))
recall_valid[penalty] = np.zeros(len(Cs))
for j, C in enumerate(Cs):
start_time = time.time()
print("("+str(( i*30)+(j+1)) +")Starting penalty="+penalty+", C="+str(C))
logreg = LogisticRegression(penalty = penalty, dual = False, tol = 1e-5, C = C, fit_intercept = True, intercept_scaling = 1,
class_weight = None, random_state = None, solver = "saga", max_iter = 250, multi_class = "ovr",
verbose = 0, warm_start = False, n_jobs = 1)
 logreg.fit(tfidf_train, y_train)
 train_pred = logreg.predict(tfidf_train)
 valid_pred = logreg.predict(tfidf_valid)
 accuracy_train[penalty][j] = accuracy_score(y_train, train_pred)
 accuracy_valid[penalty][j] = accuracy_score(y_valid, valid_pred)
 f1_train[penalty][j] = f1_score(y_train, train_pred, average = "weighted")
 f1_valid[penalty][j] = f1_score(y_valid, valid_pred, average = "weighted")
 precision_train[penalty][j] = precision_score(y_train, train_pred, average = "weighted")
 precision_valid[penalty][j] = precision_score(y_valid, valid_pred, average = "weighted")
 recall_train[penalty][j] = recall_score(y_train, train_pred, average = "weighted")
 recall_valid[penalty][j] = recall_score(y_valid, valid_pred, average = "weighted")
 end_time = time.time()
print("("+str(( i*30)+(j+1)) +")Finished penalty="+penalty+", C="+str(C)+" in "+str(end_time - start_time)+"s")
 accuracy_diff[penalty] = np.absolute(accuracy_train[penalty] - accuracy_valid[penalty])
 f1_diff[penalty] = np.absolute(f1_train[penalty] - f1_valid[penalty])
 precision_diff[penalty] = np.absolute(precision_train[penalty] - precision_valid[penalty])
 recall_diff[penalty] = np.absolute(recall_train[penalty] - recall_valid[penalty] )
for f in train_all_real.columns[0:200]:
train_all_real[f+'duplicate_count'] = train_all_real.groupby([f])[f].transform('count' )<data_type_conversions>
xx = np.argmax(accuracy_valid["L1"])
 yx = np.argmax(accuracy_valid["L2"])
 print("Max L1 validation accuracy is at C="+str(Cs[xx]))
print("Train Accuracy : "+str(accuracy_train["L1"][xx]))
print("Valid Accuracy : "+str(accuracy_valid["L1"][xx]))
print("Difference Accuracy : "+str(accuracy_diff["L1"][xx]))
print("Max L2 validation accuracy is at C="+str(Cs[yx]))
print("Train Accuracy : "+str(accuracy_train["L2"][yx]))
print("Valid Accuracy : "+str(accuracy_valid["L2"][yx]))
print("Difference Accuracy : "+str(accuracy_diff["L2"][yx]))
for f in train_all_real.columns[0:200]:
train_all_real[f + '_duplicate_value_1'] = train_all_real[f]*(train_all_real[f + 'duplicate_count']>1 ).astype(int)
 train_all_real[f + '_duplicate_value_2'] = train_all_real[f]*(train_all_real[f + 'duplicate_count']>2 ).astype(int)
 train_all_real[f + '_duplicate_value_3'] = train_all_real[f]*(train_all_real[f + 'duplicate_count']>3 ).astype(int )<feature_engineering>
logreg = LogisticRegression(dual = False, tol = 1e-5, fit_intercept = True, intercept_scaling = 1, class_weight = None,
random_state = None, solver = "saga", max_iter = 1000, multi_class = "ovr",
verbose = 0, warm_start = False, n_jobs = 1)
 params = {
"penalty": ["L1", "L2"],
"C": stats.lognorm(0.75, scale = Cs[xx])
 }
clf = RandomizedSearchCV(logreg, params, scoring = "accuracy", cv = 5)
 clf.fit(tfidf_train, y_train)
 print(clf.best_params_ )
<drop_column>
print(clf.best_params_ )
train_all_real = train_all_real.drop(train_all_real.columns[400:600],axis = 1 )<split>
C_best = clf.best_params_["C"]
penalty_best = clf.best_params_["penalty"]
tfidf_vectorizer = TfidfVectorizer(analyzer = "word", preprocessor = lambda x: x, tokenizer = lambda x: x, token_pattern = None)
 tfidf = tfidf_vectorizer.fit_transform(train_df["ingredients"])
 le = LabelEncoder()
y = le.fit_transform(train_df["cuisine"])
 logreg = LogisticRegression(dual = False, tol = 1e-5, fit_intercept = True, intercept_scaling = 1, class_weight = None,
random_state = None, solver = "saga", max_iter = 100000, multi_class = "ovr",
verbose = 0, warm_start = False, n_jobs = 1, C = C_best, penalty = penalty_best)
 logreg.fit(tfidf, y)
 pred = logreg.predict(tfidf)
 print("Accuracy: "+str(accuracy_score(y, pred)) )
<set_options><EOS> 
test_df = pd.read_json(".. /input/test.json" ).set_index("id")
 test_tfidf = tfidf_vectorizer.transform(test_df["ingredients"])
 pred_test = logreg.predict(test_tfidf)
 test_df["cuisine"] = le.inverse_transform(pred_test)
 test_df.drop(columns = ["ingredients"], inplace = True)
 test_df.to_csv("tuned_logistic_regression_submission.csv" )