Kaggle competition: What's Cooking? (Kernels Only)
params = {
'objective':'binary:logistic',
'colsample_bytree': 0.75,
'learning_rate': 0.001,
'max_depth': 13,
'n_estimators': 1500,
'subsample': 0.3}
bst = xgb.train(clf.best_params_, train_mat, clf.best_params_['n_estimators'])
 roc_auc_score(y_train, bst.predict(train_mat))<compute_test_metric>
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
roc_auc_score(y_test, bst.predict(test_mat))<predict_on_test>
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 train.head()
CFG['batch_size'] = 256
cnt_test = count_data_items(files_test)
 steps = cnt_test /(CFG['batch_size'] * REPLICAS)* CFG['tta_steps']
ds_testAug = get_dataset(files_test, CFG, augment=True, repeat=True,
labeled=False, return_image_names=False)
 probs = model.predict(ds_testAug, verbose=1, steps=steps)
 probs = np.stack(probs)
 probs = probs[:,:cnt_test * CFG['tta_steps']]
probs = np.stack(np.split(probs, CFG['tta_steps'], axis=1), axis=1)
 probs = np.mean(probs, axis=1 )<create_dataframe>
print("Total number of cuisines are: {}".format(len(train['cuisine'].unique())))
 print(train['cuisine'].unique() )
ds = get_dataset(files_test, CFG, augment=False, repeat=False,
labeled=False, return_image_names=True)
 image_names = np.array([img_name.numpy().decode("utf-8")
 for img, img_name in iter(ds.unbatch())] )<save_to_csv>
cuisine_df = pd.DataFrame({
'Count': train['cuisine'].value_counts() })
 cuisine_df
for i in range(1):
submission = pd.DataFrame(dict(
 image_name = image_names,
target = probs[:,0]))
submission = submission.sort_values('image_name')
 submission.to_csv(f'submission_model_{i}.csv', index=False )<save_to_csv>
allingredients = []
for item in train['ingredients']:
for ingr in item:
allingredients.append(ingr )
submission = pd.DataFrame(dict(
 image_name = image_names,
target = np.mean(probs[:,:,0], axis=0)))
 submission = submission.sort_values('image_name')
 submission.to_csv('submission_models_blended.csv', index=False )<feature_engineering>
count_ingr = Counter(allingredients)
 count_ingr
df_test['sex'] = pd.Categorical(df_test.sex)
 df_test['sex_cat'] = df_test.sex.cat.codes
df_test['anatom_site_general_challenge'] = pd.Categorical(df_test.anatom_site_general_challenge)
 df_test['anatom_site_general_challenge_cat'] = df_test.anatom_site_general_challenge.cat.codes<categorify>
train['num_ingredients'] = train['ingredients'].apply(len)
 train = train[train['num_ingredients'] > 1]
train.drop('num_ingredients', axis=1, inplace=True )
imputer = MissForest()
df_test[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']] = imputer.fit_transform(df_test[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']] )<drop_column>
def case_normalization(ingredients):
ingredients = ' '.join(ingredients ).lower()
ingredients = ingredients.replace('-','')
 return ingredients
X_test = df_test[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']]<predict_on_test>
train['ingredients'] = train['ingredients'].apply(case_normalization)
 test['ingredients'] = test['ingredients'].apply(case_normalization)
 train.head()
df_test['xgb_pred'] = bst.predict(xgb.DMatrix(X_test))
df_test = df_test.set_index('image_name' )<concatenate>
def clean_data(ingredients):
lemmatizer = WordNetLemmatizer()
ingredients = re.sub(r'[^\w\s\d]', '', ingredients)
 ingredients = re.sub(r'\d+', '', ingredients)
 tokens = word_tokenize(ingredients)
 words = []
for token in tokens:
word = lemmatizer.lemmatize(token)
 if len(word)> 0:
words.append(word)
 return ' '.join(words)
pred_df = pd.DataFrame(probs[:,:,0] ).transpose()
pred_df['image_name'] = image_names
pred_df = pred_df.set_index('image_name')
 pred_df = pd.concat([pred_df, df_test['xgb_pred']], axis=1 )<filter>
train['features'] = train['ingredients'].apply(clean_data)
 test['features'] = test['ingredients'].apply(clean_data)
 train.head()
df_test[df_test.index=='ISIC_1581247']<feature_engineering>
pipeline = Pipeline([ 
("tfidf", TfidfVectorizer()), 
("functiontransform", FunctionTransformer(lambda x: x.astype('float32'), validate=False))
])
 x_train = pipeline.fit_transform(train['features'].values)
 x_train.sort_indices()
x_test = pipeline.transform(test['features'].values )
pred_df['final_prediction'] = np.average(pred_df[list(range(7)) ].values, axis=1)
 pred_df['final_prediction'] = pred_df['final_prediction']*0.95+pred_df['xgb_pred']*0.05<save_to_csv>
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(train['cuisine'].values)
 dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))
pred_df = pred_df.sort_index()
pred_df['target'] = pred_df['final_prediction']
pred_df['image_name'] = pred_df.index
pred_df[['image_name', 'target']].to_csv('submission_models_blended_with_dense.csv', index=False )<install_modules>
estimator = SVC(
 C = 50,
kernel = 'rbf',
gamma = 1.5,
random_state=42 
)
 classifier = OneVsRestClassifier(estimator, n_jobs=-1 )
!pip install -q efficientnet<import_modules>
classifier.fit(x_train, y_train )
<load_from_csv><EOS> 
y_pred = label_encoder.inverse_transform(classifier.predict(x_test))
test['cuisine'] = y_pred
test[['id', 'cuisine']].to_csv('submission.csv', index=False)
 test[['id', 'cuisine']].head()