Kaggle competition: What's Cooking? (Kernels Only)
print("Disabling effnets for training...")
 for i in range(CFG['net_count']):
for layer in model.layers:
if(layer.name==f'efficientnet-b{i}'):
layer.trainable = True
break
model = compile_new_model(CFG, model)
 history_complete = model.fit(ds_train,
verbose = 1,
steps_per_epoch = steps_train,
epochs = CFG['epochs'],
callbacks = [get_lr_callback(CFG)],
class_weight=class_weights )<train_model>
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 class_names = list(train.cuisine.unique())
 class_names
<count_missing_values>
train['num_ings'] = train['ingredients'].apply(lambda x : len(x))
test['num_ings'] = test['ingredients'].apply(lambda x : len(x))
train.head()
df_train.sex.isna().sum()<count_missing_values>
train = train[train['num_ings'] > 2]
len(train )
df_train.anatom_site_general_challenge.isna().sum()<count_missing_values>
train['ingredients'] = train['ingredients'].apply(lambda x: list(map(lambda y: y.lower() , x)))
 test['ingredients'] = test['ingredients'].apply(lambda x: list(map(lambda y: y.lower() , x)))
 train.head()
df_train.age_approx.isna().sum()<feature_engineering>
def get_replacements() :
return {'wasabe': 'wasabi', '-': '', 'sauc': 'sauce',
'baby spinach': 'babyspinach', 'coconut cream': 'coconutcream',
'coriander seeds': 'corianderseeds', 'corn tortillas': 'corntortillas',
'cream cheese': 'creamcheese', 'fish sauce': 'fishsauce',
'purple onion': 'purpleonion','refried beans': 'refriedbeans',
'rice cakes': 'ricecakes', 'rice syrup': 'ricesyrup',
'sour cream': 'sourcream', 'toasted sesame seeds': 'toastedsesameseeds',
'toasted sesame oil': 'toastedsesameoil', 'yellow onion': 'yellowonion'}
df_train['sex'] = pd.Categorical(df_train.sex)
 df_train['sex_cat'] = df_train.sex.cat.codes
df_train['anatom_site_general_challenge'] = pd.Categorical(df_train.anatom_site_general_challenge)
 df_train['anatom_site_general_challenge_cat'] = df_train.anatom_site_general_challenge.cat.codes<categorify>
lemmatizer = WordNetLemmatizer()
replacements = get_replacements()
stop_pattern = re.compile('[\d’%]' )
imputer = MissForest()
df_train[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']] = imputer.fit_transform(df_train[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']] )<count_missing_values>
def tranform_to_single_string(ingredients, lemmatizer, replacements, stop_pattern):
ingredients_text = ' '.join(iter(ingredients))
for key, value in replacements.items() :
ingredients_text = ingredients_text.replace(key, value)
 words = []
for word in ingredients_text.split() :
if not stop_pattern.match(word)and len(word)> 2:
word = lemmatizer.lemmatize(word)
 words.append(word)
 return ' '.join(words )
print("Sex nan:", df_train.sex_cat.isna().sum())
 print("anatom_site_general_challenge nan:", df_train.anatom_site_general_challenge_cat.isna().sum())
 print("age_approx nan:", df_train.age_approx.isna().sum() )<prepare_x_and_y>
transform = lambda ingredients: tranform_to_single_string(ingredients, lemmatizer, replacements, stop_pattern)
 train['x'] = train['ingredients'].apply(transform)
 test['x'] = test['ingredients'].apply(transform)
 train.head()
train, test = train_test_split(df_train, test_size=0.2)
 print(len(train))
print(len(test))
X_train, y_train = train[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']], train.target
X_test, y_test = test[['sex_cat', 'age_approx', 'anatom_site_general_challenge_cat']], test.target<categorify>
vectorizer = make_pipeline(
 TfidfVectorizer(sublinear_tf=True),
FunctionTransformer(lambda x: x.astype('float'), validate=False)
 )
class_weights = list(class_weight.compute_class_weight('balanced',
np.unique(y_train),
y_train))
weights_train = np.ones(y_train.shape[0], dtype = 'float')
 for i, val in enumerate(y_train):
weights_train[i] = class_weights[val-1]
weights_test = np.ones(y_test.shape[0], dtype = 'float')
 for i, val in enumerate(y_test):
weights_test[i] = class_weights[val-1]<train_model>
x_train = vectorizer.fit_transform(train['x'].values)
 x_train.sort_indices()
x_test = vectorizer.transform(test['x'].values )
train_mat = xgb.DMatrix(X_train, y_train, weight=weights_train)
 test_mat = xgb.DMatrix(X_test, y_test, weight=weights_test )<train_model>
def get_estimator() :
return SVC(C=300,
kernel='rbf',
gamma=1.5,
shrinking=True,
tol=0.001,
cache_size=1000,
class_weight=None,
max_iter=-1,
decision_function_shape='ovr',
random_state=42 )
class XGBoostClassifier(xgb.XGBClassifier):
def __init__(self, num_boost_round=10, **params):
self.clf = None
self.num_boost_round = num_boost_round
if 'num_boost_round' in params:
self.num_boost_round = params.pop('n_estimators')
 self.params = params
self.params.update({'objective': 'binary:logistic'})
 def fit(self, X, y, num_boost_round=None, weights=None):
num_boost_round = num_boost_round or self.num_boost_round
dtrain = xgb.DMatrix(X, y, weights)
 self.clf = xgb.train(params=self.params, dtrain=dtrain, num_boost_round=num_boost_round)
 def predict(self, X):
return self.clf.predict(xgb.DMatrix(X))
def predict_proba(self, X):
dtest = xgb.DMatrix(X)
 classone_probs = self.clf.predict(dtest)
 classzero_probs = 1.0 - classone_probs
return np.vstack(( classzero_probs, classone_probs)).transpose()
def get_params(self, deep=True):
return self.params
def set_params(self, **params):
if 'n_estimators' in params:
self.num_boost_round = params.pop('n_estimators')
 self.params.update(params)
 return self<train_on_grid>
estimator = get_estimator()
y_train = train['cuisine'].values
classifier = OneVsRestClassifier(estimator, n_jobs=-1)
 classifier.fit(x_train, y_train )
params = {
'max_depth':[3, 5, 10],
'learning_rate': [0.5, 0.1, 0.001],
'n_estimators': [300, 500, 1500],
'subsample': [0.5, 0.7, 1.0],
'colsample_bytree': [0.01, 0.75, 1]
}
xgbc = XGBoostClassifier(objective='binary:logistic')
 clf = GridSearchCV(xgbc, params, scoring='roc_auc', cv=3)
 clf.fit(X_train, y_train, weights=weights_train )<train_on_grid>
test['cuisine'] = classifier.predict(x_test)
 test[['id', 'cuisine']].to_csv('submission.csv', index=False )