Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<prepare_x_and_y>
warnings.filterwarnings('ignore')
y = X["answered_correctly"]
X = X.drop(["answered_correctly"], axis=1)
 y_val = validation["answered_correctly"]
X_val = validation.drop(["answered_correctly"], axis=1 )<drop_column>
df_train = pd.read_json(".. /input/train.json")
 df_test = pd.read_json(".. /input/test.json" )
X = X[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']]
X_val = X_val[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']]<data_type_conversions>
non_alphabetical_or_whitespace = re.compile(r"[^a-zA-Z\s]")
 multi_whitespace = re.compile(r"\s+")
 lemmatizer = WordNetLemmatizer()
def clean_ingredients(ingredients: List[str])-> List[str]:
result = []
for ingredient in ingredients:
temp = ingredient.lower()
temp = non_alphabetical_or_whitespace.sub("", temp)
 temp = multi_whitespace.sub(" ", temp)
 temp = ' '.join([lemmatizer.lemmatize(word)for word in multi_whitespace.split(temp)])
 result.append(temp)
 return ",".join(result)
 df_train["ingredients_cleaned"] = df_train["ingredients"].apply(lambda ingredients: clean_ingredients(ingredients))
df_test["ingredients_cleaned"] = df_test["ingredients"].apply(lambda ingredients: clean_ingredients(ingredients))
X['answered_correctly_user'].fillna(0.65, inplace=True)
 X['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 X['quest_pct'].fillna(content_mean, inplace=True)
 X['part'].fillna(4, inplace = True)
 X['avg_question_seen'].fillna(1, inplace = True)
 X['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 X['prior_question_had_explanation_enc'].fillna(0, inplace = True)
 X['part_1'].fillna(0, inplace = True)
 X['part_2'].fillna(0, inplace = True)
 X['part_3'].fillna(0, inplace = True)
 X['part_4'].fillna(0, inplace = True)
 X['part_5'].fillna(0, inplace = True)
 X['part_6'].fillna(0, inplace = True)
 X['part_7'].fillna(0, inplace = True)
 X['type_of_concept'].fillna(0, inplace = True)
 X['type_of_intention'].fillna(0, inplace = True)
 X['type_of_solving_question'].fillna(0, inplace = True)
 X['type_of_starter'].fillna(0, inplace = True)
 X['part_1_boolean'].fillna(0, inplace = True)
 X['part_2_boolean'].fillna(0, inplace = True)
 X['part_3_boolean'].fillna(0, inplace = True)
 X['part_4_boolean'].fillna(0, inplace = True)
 X['part_5_boolean'].fillna(0, inplace = True)
 X['part_6_boolean'].fillna(0, inplace = True)
 X['part_7_boolean'].fillna(0, inplace = True)
 X['type_of_concept_boolean'].fillna(0, inplace = True)
 X['type_of_intention_boolean'].fillna(0, inplace = True)
 X['type_of_solving_question_boolean'].fillna(0, inplace = True)
 X['type_of_starter_boolean'].fillna(0, inplace = True )<data_type_conversions>
label_encoder = LabelEncoder()
vectorizer = TfidfVectorizer(binary=True)
 X = vectorizer.fit_transform(df_train["ingredients_cleaned"])
 y = label_encoder.fit_transform(df_train["cuisine"] )
X_val['answered_correctly_user'].fillna(0.65, inplace=True)
 X_val['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 X_val['quest_pct'].fillna(content_mean, inplace=True)
 X_val['part'].fillna(4, inplace = True)
 X_val['avg_question_seen'].fillna(1, inplace = True)
 X_val['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 X_val['prior_question_had_explanation_enc'].fillna(0, inplace = True)
 X_val['part_1'].fillna(0, inplace = True)
 X_val['part_2'].fillna(0, inplace = True)
 X_val['part_3'].fillna(0, inplace = True)
 X_val['part_4'].fillna(0, inplace = True)
 X_val['part_5'].fillna(0, inplace = True)
 X_val['part_6'].fillna(0, inplace = True)
 X_val['part_7'].fillna(0, inplace = True)
 X_val['type_of_concept'].fillna(0, inplace = True)
 X_val['type_of_intention'].fillna(0, inplace = True)
 X_val['type_of_solving_question'].fillna(0, inplace = True)
 X_val['type_of_starter'].fillna(0, inplace = True)
 X_val['part_1_boolean'].fillna(0, inplace = True)
 X_val['part_2_boolean'].fillna(0, inplace = True)
 X_val['part_3_boolean'].fillna(0, inplace = True)
 X_val['part_4_boolean'].fillna(0, inplace = True)
 X_val['part_5_boolean'].fillna(0, inplace = True)
 X_val['part_6_boolean'].fillna(0, inplace = True)
 X_val['part_7_boolean'].fillna(0, inplace = True)
 X_val['type_of_concept_boolean'].fillna(0, inplace = True)
 X_val['type_of_intention_boolean'].fillna(0, inplace = True)
 X_val['type_of_solving_question_boolean'].fillna(0, inplace = True)
 X_val['type_of_starter_boolean'].fillna(0, inplace = True )<train_on_grid>
y = keras.utils.to_categorical(y )
params = {
'num_leaves': 31,
'n_estimators': 200,
'max_depth': 8,
'min_child_samples': 356,
'learning_rate': 0.2982483634778906,
'min_data_in_leaf': 82,
'bagging_fraction': 0.6545628633239445,
'feature_fraction': 0.9164482379289846,
'random_state': 666
}
full_model = LGBMClassifier(**params)
 full_model.fit(X, y)
 preds = full_model.predict_proba(X_val)[:,1]
print("LGB roc auc", roc_auc_score(y_val, preds))
full_xgb = XGBClassifier(random_state=666)
 full_xgb.fit(X, y)
 preds = full_xgb.predict_proba(X_val)[:,1]
print("XGB roc auc", roc_auc_score(y_val, preds))
full_lr = LogisticRegression(random_state=666)
 full_lr.fit(X, y)
 preds = full_lr.predict_proba(X_val)[:,1]
print("LR roc auc", roc_auc_score(y_val, preds))<import_modules>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2 )
import optuna
from optuna.samplers import TPESampler<normalization>
def create_model1(input_dim: int):
model = Sequential()
model.add(Dense(1024, input_dim=input_dim))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(64))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(20, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
 model.summary()
return model
def create_logistic_model(input_dim: int):
model = Sequential()
model.add(Dense(20, input_dim=input_dim, activation='softmax'))
batch_size = 128
nb_epoch = 20
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
rfe = RFE(estimator=DecisionTreeClassifier(random_state=666), n_features_to_select=14)
 rfe.fit(X, y)
 X = rfe.transform(X)
 X_val = rfe.transform(X_val )<init_hyperparams>
features_dimension = X_train.shape[1]
model1 = create_logistic_model(features_dimension)
 model1.fit(X, y, epochs=200, batch_size=32)
 model2 = create_model1(features_dimension)
 model2.fit(X, y, epochs=50, batch_size=64 )
params = {'num_leaves': 29,
'n_estimators': 300,
'max_depth': 9,
'min_child_samples': 1089,
'learning_rate': 0.306030368670154,
'min_data_in_leaf': 65,
'bagging_fraction': 0.49498535405259425,
'feature_fraction': 0.9235503880887722,
'random_state': 666}<find_best_model_class>
for model in [model1, model2]:
print(model.evaluate(X_test, y_test, batch_size=128))
model = LGBMClassifier(**params)
 model.fit(X, y)
 preds = model.predict_proba(X_val)[:,1]
roc_auc_score(y_val, preds )<create_dataframe>
X_validation = df_test["ingredients_cleaned"].apply(lambda ingredients: vectorizer.transform([ingredients] ).todense() )
X = pd.DataFrame(X)
 X_val = pd.DataFrame(X_val)
 y = pd.DataFrame(y)
 y_val = pd.DataFrame(y_val )<choose_model_class>
classification_results = []
for ingredients in X_validation:
classification_results.append(
 label_encoder.inverse_transform(
 np.argmax(
(model1.predict(ingredients)+ model2.predict(ingredients)) /2 
)
)
)
 df_test["cuisine"] = classification_results
models = []
preds = []
for n,(tr, te)in enumerate(KFold(n_splits=5, random_state=666, shuffle=True ).split(y)) :
print(f'Fold {n}')
 model = LGBMClassifier(**params)
 model.fit(X.values[tr], y.values[tr])
 pred = model.predict_proba(X_val)[:, 1]
preds.append(pred)
 print('Fold roc auc:', roc_auc_score(y.values[te], model.predict_proba(X.values[te])[:, 1]))
models.append(model )<compute_test_metric>
df_test[["id", "cuisine"]].to_csv("results_multi_with_feature_engineering.csv", index=False )