Kaggle competition: What's Cooking? (Kernels Only)
history = model.fit_generator(
 train_generator,
epochs = 20,
steps_per_epoch = 63,
validation_data = valid_generator,
validation_steps = 14 
)
<predict_on_test>
%matplotlib inline
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
 )<prepare_output>
rawdf_te = pd.read_json(path_or_buf='.. /input/test.json')
 rawdf_tr = pd.read_json(path_or_buf='.. /input/train.json' )
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
preds = preds.flatten()
data = {'id': image_ids, 'has_cactus':preds}
submission = pd.DataFrame(data)
 print(submission.head() )<save_to_csv>
ingredients_tr = rawdf_tr['ingredients']
ingredients_te = rawdf_te['ingredients']
def sub_match(pattern, sub_pattern, ingredients):
for i in ingredients.index.values:
for j in range(len(ingredients[i])) :
ingredients[i][j] = re.sub(pattern, sub_pattern, ingredients[i][j].strip())
 ingredients[i][j] = ingredients[i][j].strip()
re.purge()
return ingredients
def regex_sub_match(series):
p0 = re.compile(r'\s*(oz|ounc|ounce|pound|lb|inch|inches|kg|to)\s*[^a-z]')
 series = sub_match(p0, ' ', series)
 p1 = re.compile(r'\d+')
 series = sub_match(p1, ' ', series)
 p2 = re.compile('[^\w]')
 series = sub_match(p2, ' ', series)
 return series
submission.to_csv("submission.csv", index=False )<save_to_csv>
ingredients_tr = regex_sub_match(ingredients_tr )
submission.to_csv("submission.csv", index=False )<import_modules>
lemmatizer = WordNetLemmatizer()
def lemma(series):
for i in series.index.values:
for j in range(len(series[i])) :
series[i][j] = series[i][j].strip()
token = TK(series[i][j])
 for k in range(len(token)) :
token[k] = lemmatizer.lemmatize(token[k])
 token = ' '.join(token)
 series[i][j] = token
return series
import os
import numpy
from fastai.vision import *
import pandas as pd<define_variables>
ingredients_tr = lemma(ingredients_tr )
train_dir = path/'train'/'train'
test_dir = path/'test'/'test'
train_labels = path/'train.csv'
output = path/'sample_submission.csv'<define_variables>
rawdf_tr['ingredients_lemma'] = ingredients_tr
rawdf_tr['ingredients_lemma_string'] = [' '.join(_ ).strip() for _ in rawdf_tr['ingredients_lemma']]
np.random.seed(26 )<load_from_csv>
def tt_split(cuisine):
cuisine_population = rawdf_tr.loc[(rawdf_tr['cuisine'] == cuisine)].index.values
train, test = train_test_split(cuisine_population, test_size=0.15, random_state=0)
 train = train.tolist()
test = test.tolist()
return train, test
cuisine_list = rawdf_tr['cuisine'].unique().tolist()
ix_train = []
ix_test = []
for _ in cuisine_list:
temp_train, temp_test = tt_split(_)
 ix_train += temp_train
ix_test += temp_test
traindf = rawdf_tr[['cuisine', 'ingredients_lemma_string']].loc[ix_train].reset_index(drop=True)
 print(traindf.shape)
 testdf = rawdf_tr[['cuisine', 'ingredients_lemma_string']].loc[ix_test].reset_index(drop=True)
 print(testdf.shape)
 X_train_ls = traindf['ingredients_lemma_string']
vectorizertr = TfidfVectorizer(stop_words='english', analyzer="word", max_df=0.65, min_df=2, binary=True)
 X_train = vectorizertr.fit_transform(X_train_ls)
 y_train = traindf['cuisine']
le = LE()
y_train_ec = le.fit_transform(y_train)
 y_train_1h = pd.get_dummies(y_train_ec)
 X_pred_ls = testdf['ingredients_lemma_string']
vectorizerts = TfidfVectorizer(stop_words='english')
 X_pred = vectorizertr.transform(X_pred_ls)
 y_true = testdf['cuisine']
y_true_ec = le.fit_transform(y_true)
 y_true_1h = pd.get_dummies(y_true_ec )
data =(ImageList.from_csv(path=path, csv_name='train.csv', folder='train/train')
.split_by_rand_pct() 
.label_from_df(cols='has_cactus')
.add_test(ImageList.from_csv(path=path, csv_name='sample_submission.csv',
folder='test/test')) 
.transform(get_transforms() , size=224)
.databunch(bs=32, num_workers=0)
.normalize(imagenet_stats))<train_model>
model = Sequential()
model.add(Dense(2161, input_shape=(2161,), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1024, activation='softsign'))
model.add(Dropout(0.58))
model.add(Dense(256, activation='softsign'))
model.add(Dropout(0.67))
model.add(Dense(20, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 epochs = 20
early_stopping = EarlyStopping(monitor='val_loss', patience=1)
 model.fit(X_train,y_train_1h,
batch_size=30,
epochs=epochs,
verbose=2,
callbacks=[early_stopping],
validation_data=(X_pred, y_true_1h),
shuffle=True )
data.classes, data.classes, data.train_ds, data.test_ds<import_modules>
ingredients_te = lemma(ingredients_te )
from fastai.vision.learner import create_cnn, models<choose_model_class>
rawdf_te['ingredients_lemma'] = ingredients_te
rawdf_te['ingredients_lemma_string'] = [' '.join(_ ).strip() for _ in rawdf_te['ingredients_lemma']]
arch = cnn_learner(data, base_arch=models.resnet34, model_dir='/tmp/models',
metrics=accuracy )<find_best_params>
X_pred_ls = testdf['ingredients_lemma_string']
vectorizerts = TfidfVectorizer(stop_words='english')
 X_pred = vectorizertr.transform(X_pred_ls)
arch.lr_find()<train_model>
y_pred_nn = le.inverse_transform(model.predict_classes(X_pred))
<save_model><EOS> 
testdf['cuisine'] = y_pred_nn
d = pd.DataFrame(data=testdf['cuisine'], index=testdf.index ).sort_index().reset_index().to_csv('submission_nn.csv', index=False )