Kaggle competition: DL in NLP Spring 2019. Classification
train = pd.read_json("/kaggle/input/whats-cooking-kernels-only/train.json")
 test = pd.read_json("/kaggle/input/whats-cooking-kernels-only/test.json" )<define_variables>
fastai.__version__
raw_ingredients = [ing for ingredients in train['ingredients'] for ing in ingredients]<feature_engineering>
from fastai.text import *
from fastai.callbacks import *
train['num_ing'] = train['ingredients'].str.len()<filter>
data_train = pd.read_csv(".. /input/train.csv")
 data_valid = pd.read_csv(".. /input/valid.csv")
 data_test = pd.read_csv(".. /input/test.csv" )
longrecip = train[train['num_ing'] > 30]
print(len(longrecip))<count_values>
data_train.fillna("xxempty", inplace=True)
 data_valid.fillna("xxempty", inplace=True)
 data_test.fillna("xxempty", inplace=True )
longrecip['cuisine'].value_counts()<filter>
data_train["full"] = data_train["text"].apply(lambda x: x + " xxtitle ")+ data_train["title"]
data_valid["full"] = data_valid["text"].apply(lambda x: x + " xxtitle ")+ data_valid["title"]
data_test["full"] = data_test["text"].apply(lambda x: x + " xxtitle ")+ data_test["title"]
print(longrecip[longrecip['num_ing'] == 65]['ingredients'].values)
 print('Cuisine :-',longrecip[longrecip['num_ing'] == 65]['cuisine'].values )<filter>
data_train["is_valid"] = False
data_valid["is_valid"] = True
shortrecip = train[train['num_ing']<=2]
print(len(shortrecip))<count_values>
data_lm =(TextList.from_df(pd.concat([data_train, data_valid]), cols=["full"])
.split_from_df("is_valid")
.label_for_lm() 
.databunch() )
shortrecip['cuisine'].value_counts()<filter>
lm = language_model_learner(data_lm, AWD_LSTM, drop_mult=0.3, pretrained=True )
train[train['num_ing'] <= 1]<define_variables>
lm.lr_find()
[ingredient for ingredient in raw_ingredients if len(ingredient)<= 2]<string_transform>
lm.fit_one_cycle(1, 4e-3, callbacks=[SaveModelCallback(lm, name="best_lm")], moms=(0.8,0.7))
' '.join(sorted([char for char in set(' '.join(raw_ingredients)) if re.findall('[^A-Za-z]', char)]))<define_variables>
lm.unfreeze()
list(set([ingredient for ingredient in raw_ingredients if re.findall('[A-Z]+', ingredient)])) [:5]<define_variables>
lm.fit_one_cycle(5, 4e-3, callbacks=[SaveModelCallback(lm, name="best_lm")], moms=(0.8,0.7))
list(set([ingredient for ingredient in raw_ingredients if '’' in ingredient]))<define_variables>
lm.load("best_lm" )
list(set([ingredient for ingredient in raw_ingredients if re.findall('-', ingredient)])) [:5]<define_variables>
lm.save_encoder("enc" )
temp_ing = list(set([ingredient for ingredient in raw_ingredients if re.findall('[0-9]', ingredient)]))
temp_ing[:6]<create_dataframe>
data_clf =(TextList.from_df(pd.concat([data_train, data_valid]), vocab=data_lm.vocab, cols=["full"] ).
 split_from_df("is_valid" ).
 label_from_df("label" ).
 add_test(data_test["full"] ).
 databunch()
)
units = ['inch', 'oz', 'lb', 'ounc', '%']
@interact(unit=units)
 def f(unit):
ingredients_df = pd.DataFrame([ingredient for ingredient in raw_ingredients if unit in ingredient], columns=['ingredient'])
 return ingredients_df.groupby(['ingredient'] ).size().reset_index(name='count' ).sort_values(['count'], ascending=False )<feature_engineering>
clf = text_classifier_learner(data_clf, AWD_LSTM, drop_mult=0.3 )
keywords = ['american', 'greek', 'filipino', 'indian', 'jamaican', 'spanish', 'italian', 'mexican', 'chinese', 'thai',
'vietnamese', 'cajun', 'creole', 'french', 'japanese', 'irish', 'korean', 'moroccan', 'russian',
]
d ={}
for k in keywords:
temp = [ingredient for ingredient in raw_ingredients if k in ingredient]
d[k] = temp<count_unique_values>
del lm
torch.cuda.empty_cache()
top = Counter([item for sublist in train['ingredients'] for item in sublist])
 print(top.most_common(20))<create_dataframe>
clf.load_encoder("enc" )
labels = ['greek', 'southern_us', 'filipino', 'indian', 'jamaican',
'spanish', 'italian', 'mexican', 'chinese', 'british', 'thai',
'vietnamese', 'cajun_creole', 'brazilian', 'french', 'japanese',
'irish', 'korean', 'moroccan', 'russian']
templist=[]
for cus in labels:
lisofing=[]
for lis in train[train['cuisine'] == cus]['ingredients']:
for ing in lis:
lisofing.append(ing)
 templist.append([cus,len(list(set(lisofing)))])
 Unique_ing = pd.DataFrame(templist,columns=['cuisine','unique_ing'] ).sort_values(by='unique_ing',ascending=False )<drop_column>
clf.lr_find()
train = train[train['num_ing'] > 1]<filter>
clf.fit(3, 2e-3, callbacks=[SaveModelCallback(clf, name="best_clf")] )
train = train[train['num_ing']<60]<categorify>
clf.load("best_clf" )
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text = ' '.join(ingredients)
 ingredients_text = ingredients_text.lower()
ingredients_text = ingredients_text.replace('-', ' ')
 words = []
for word in ingredients_text.split() :
word = re.sub("[0-9]"," ",word)
 word = re.sub(( r'\b(oz|ounc|ounce|pound|lb|inch|inches|kg|to)\b'), ' ', word)
 if len(word)<= 2: continue
word = unidecode.unidecode(word)
 word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return ' '.join(words )<define_variables>
clf.unfreeze()
for ingredient, expected in [ 
('Eggs', 'egg'), 
('all-purpose flour', 'all purpose flour'), 
('purée', 'puree'), 
('1% low-fat milk', 'low fat milk'), 
('half & half', 'half half'), 
('safetida(powder)', 'safetida(powder)')
 ]:
actual = preprocess([ingredient])
 assert actual == expected, f'"{expected}" is excpected but got "{actual}"'<feature_engineering>
clf.fit(1, 3e-4, callbacks=[SaveModelCallback(clf, name="best_clf_ft1")] )
train['x'] = train['ingredients'].progress_apply(preprocess)
 test['x'] = test['ingredients'].progress_apply(preprocess)
 train.head()<feature_engineering>
clf.fit(1, 3e-4, callbacks=[SaveModelCallback(clf, name="best_clf_ft2")] )
def apply_word2vec(sentences):
vectorizer = Word2Vec(
 sentences,
size=500,
window=20,
min_count=3,
sg=1,
iter=20 
)
 def to_vector(sentence):
words = [word for word in sentence if word in vectorizer.wv.vocab]
if words:
return np.mean(vectorizer[words], axis=0)
 else:
return np.zeros(500)
 return np.array([to_vector(sentence)for sentence in sentences])
 def apply_fasttext(sentences):
vectorizer = FastText(
 size=500,
window=20,
min_count=3,
sg=1,
iter=20 
)
 vectorizer.build_vocab(sentences)
 vectorizer.train(sentences, total_examples=vectorizer.corpus_count, epochs=vectorizer.iter)
 def to_vector(sentence):
words = [word for word in sentence if word in vectorizer.wv.vocab]
if words:
return np.mean(vectorizer.wv[words], axis=0)
 else:
return np.zeros(500)
 return np.array([to_vector(sentence)for sentence in sentences])
 def train_model(x, y, n_splits=3):
model = LogisticRegression(C=10, solver='sag', multi_class='multinomial', max_iter=300, n_jobs=-1)
 i = 0
accuracies = []
kfold = KFold(n_splits)
 for train_index, test_index in kfold.split(x):
classifier = LogisticRegression(C=10, solver='sag', multi_class='multinomial', max_iter=300, n_jobs=-1)
 classifier.fit(x[train_index], y[train_index])
 predictions = classifier.predict(x[test_index])
 accuracies.append(accuracy_score(predictions, y[test_index]))
i += 1
average_accuracy = sum(accuracies)/ len(accuracies)
 return average_accuracy
def run_experiment(preprocessor):
train = json.load(open('/kaggle/input/whats-cooking-kernels-only/train.json'))
target = [doc['cuisine'] for doc in train]
lb = LabelEncoder()
y = lb.fit_transform(target)
 x = preprocessor.fit_transform(train)
 return train_model(x, y)
<feature_engineering>
pred_val = clf.get_preds(DatasetType.Valid, ordered=True )
results = []
for(name, preprocessor)in [ 
('TfidfVectorizer() ', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer() , 
)) , 
('TfidfVectorizer(binary=True)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(binary=True), 
)) , 
('TfidfVectorizer(min_df=3)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(min_df=3), 
)) , 
('TfidfVectorizer(min_df=5)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(min_df=5), 
)) , 
('TfidfVectorizer(max_df=0.95)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(max_df=0.95), 
)) , 
('TfidfVectorizer(max_df=0.9)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(max_df=0.9), 
)) , 
('TfidfVectorizer(sublinear_tf=True)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(sublinear_tf=True), 
)) , 
('TfidfVectorizer(strip_accents=unicode)', make_pipeline(
 FunctionTransformer(lambda x: [" ".join(doc['ingredients'] ).lower() for doc in x], validate=False),
TfidfVectorizer(strip_accents='unicode'), 
)) ,
]:
start = time.time()
accuracy = run_experiment(preprocessor)
 execution_time = time.time() - start
results.append({
'name': name,
'accuracy': accuracy,
'execution time': f'{round(execution_time, 2)}s'
})
 pd.DataFrame(results, columns=['name', 'accuracy', 'execution time'] ).sort_values(by='accuracy', ascending=False )<choose_model_class>
pred_val_l = pred_val[0].argmax(1 )
vectorizer = TfidfVectorizer(sublinear_tf=True )<feature_engineering>
from sklearn.metrics import classification_report
X_train = vectorizer.fit_transform(train['x'].values)
 X_train.sort_indices()
X_test = vectorizer.transform(test['x'].values )<categorify>
print(classification_report(pred_val[1], pred_val_l))
label_encoder = LabelEncoder()
Y_train = label_encoder.fit_transform(train['cuisine'].values )<choose_model_class>
pred_test, label_test = clf.get_preds(DatasetType.Test, ordered=True )
classifier = SVC(C=100,
	 			 kernel='rbf',
	 			 degree=3,
	 			 gamma=1,
	 			 coef0=1,
	 			 shrinking=True,
	 			 tol=0.001,
	 		 probability=False,
	 		 cache_size=200,
	 		 class_weight=None,
	 		 verbose=False,
	 		 max_iter=-1,
		 decision_function_shape=None,
		 random_state=None )<train_model>
pred_test_ = pred_test.argmax(1)
 pred_test_l = [data_clf.train_ds.y.classes[n] for n in pred_test_]
model = OneVsRestClassifier(classifier, n_jobs=4)
 model.fit(X_train, Y_train )<predict_on_test>
res.index.name = "id"
<save_to_csv><EOS> 
pd.DataFrame(res ).to_csv("submission.csv" )