Kaggle competition: What's Cooking? (Kernels Only)
sample_submission = pd.read_csv(MAIN_DIR + 'sample_submission.csv')
 df_prediction = predict(model, sample_submission)
 df_prediction.to_csv('submission_1.csv', index=False )<load_from_csv>
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 gc.collect() ;
train_df = pd.read_csv('.. /input/train.csv' )<load_pretrained>
procent = re.compile('[0-9]+ ?%')
 non_alphas = re.compile('[^a-z]+')
 def tok_origin(tokens):
return tokens;
def tok_clean(tokens):
cleaned = []
for token in tokens:
if len(token)<= 2:
continue
token = token.lower()
token = procent.sub('', token)
 token = non_alphas.sub(' ', token)
 cleaned.append(token.strip())
 return cleaned;
train_data = []
train_tgt = []
for idx, row in train_df.iterrows() :
img = imread('.. /input/train/train/' + row['id'])
 train_data.append(img)
 train_tgt.append(row['has_cactus'])
 train_data = np.array(train_data)
 train_tgt = np.array(train_tgt )<split>
encoder = LabelEncoder()
y_train = encoder.fit_transform(train['cuisine'] )
train_X, val_X, train_y, val_y = sklearn.model_selection.train_test_split(train_data, train_tgt, test_size=0.40 )<categorify>
%%script false
parameters = {
'tfidf__tokenizer': [tok_origin, tok_clean],
'tfidf__binary': [False],
'tfidf__use_idf': [True],
'tfidf__sublinear_tf': [True],
'classifier': [
OneVsRestClassifier(MultinomialNB()),
OneVsRestClassifier(LogisticRegression(solver='saga', multi_class='multinomial'))
],
}
pipe = Pipeline(
 memory=None,
steps=[ 
('tfidf', TfidfVectorizer(preprocessor=None, lowercase=False)) , 
('classifier', OneVsRestClassifier(MultinomialNB())) ,
])
 grid = GridSearchCV(pipe, parameters, verbose=2, cv=2, iid=False, scoring='f1_micro')
 grid.fit(train['ingredients'], y_train)
 print(grid.best_params_)
 print(grid.best_score_)
m0 = train_X[:,:,:,0].mean()
m1 = train_X[:,:,:,1].mean()
m2 = train_X[:,:,:,2].mean()
def augmented(data: list, tgt: list):
aug_data = []
aug_tgt = []
for i in range(data.shape[0]):
img = data[i, :, :, :]
img = img/255.
 aug_data.append(img)
 aug_data.append(rotate(img, 90.))
 aug_data.append(rotate(img, 180.))
 aug_data.append(rotate(img, 270.))
 aug_data.append(np.flip(img))
aug_data.append(np.flip(img, axis=0))
aug_data.append(np.flip(img, axis=1))
aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 return np.array(aug_data), np.array(aug_tgt)
 train_X, train_y = augmented(train_X, train_y)
 val_X, val_y = augmented(val_X, val_y )<feature_engineering>
%%script false
parameters = {
'estimator__multi_class' : ['ovr', 'multinomial'],
'estimator__C' : [1, 5, 10, 50],
'estimator__fit_intercept' : [False, True],
'estimator__class_weight' : [None, 'balanced'],
}
model = OneVsRestClassifier(LogisticRegression(solver='saga', max_iter=500, warm_start=True, random_state=42))
grid = GridSearchCV(model, parameters, verbose=2, cv=2, iid=False, scoring='f1_micro', n_jobs=3)
 grid.fit(X_train_vec, y_train)
 print(grid.best_params_)
 print(grid.best_score_)
train_X.mean(axis=0 ).mean(axis=0 ).mean(axis=0 )<feature_engineering>
parameters = {
'tfidf__tokenizer': [tok_clean],
'tfidf__binary': [False],
'tfidf__use_idf': [True],
'tfidf__sublinear_tf': [True],
'classifier__estimator__multi_class' : ['multinomial'],
'classifier__estimator__C' : [2.6],
'classifier__estimator__fit_intercept' : [True],
'classifier__estimator__class_weight' : [None],
'classifier__estimator__warm_start': [True]
}
pipe = Pipeline(
 memory=None,
steps=[ 
('tfidf', TfidfVectorizer(preprocessor=None, lowercase=False)) , 
('classifier', OneVsRestClassifier(LogisticRegression(solver='saga', max_iter=500, random_state=42))),
])
 grid = GridSearchCV(pipe, parameters, verbose=2, cv=2, iid=False, scoring='f1_micro', n_jobs=3)
 grid.fit(train['ingredients'], y_train)
 print(grid.best_params_)
 print(grid.best_score_)
val_X.mean(axis=0 ).mean(axis=0 ).mean(axis=0 )<choose_model_class>
predicted_classes = encoder.inverse_transform(grid.predict(test['ingredients']))
submissions = pd.DataFrame({"id": test['id'], "cuisine": predicted_classes})
 submissions.to_csv("submission.csv", index=False, header=True )