Kaggle competition: What's Cooking? (Kernels Only)
img_path = '/kaggle/input/melanoma-merged-external-data-512x512-jpeg'
img_path<load_from_csv>
%matplotlib inline
train_df = pd.read_csv(img_path + '/folds_13062020.csv')
 train_df.head()<load_from_csv>
rawdf_te = pd.read_json(path_or_buf='.. /input/test.json')
 rawdf_te.head(n=3)
 rawdf_tr = pd.read_json(path_or_buf='.. /input/train.json')
 rawdf_tr.head(n=3 )
test_df = pd.read_csv(path + '/test.csv')
 test_df.head()<load_from_csv>
print(rawdf_tr['ingredients'].loc[41935])
 print(rawdf_tr['ingredients'].loc[27566])
 print(rawdf_tr['ingredients'].loc[32596])
 print(rawdf_tr['ingredients'].loc[8476] )
sample_df = pd.read_csv(path + '/sample_submission.csv')
 sample_df.head()<feature_engineering>
ingredients_tr = rawdf_tr['ingredients']
ingredients_te = rawdf_te['ingredients']
tfms = get_transforms(flip_vert=True, max_rotate=15, max_zoom=1.2, max_lighting=0.3, max_warp=0, p_affine=0, p_lighting=0.8 )<compute_train_metric>
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
class FocalLoss(nn.Module):
def __init__(self, gamma=2., reduction='mean'):
super().__init__()
self.gamma = gamma
self.reduction = reduction
def forward(self, inputs, targets):
CE_loss = nn.CrossEntropyLoss(reduction='none' )(inputs, targets)
 pt = torch.exp(-CE_loss)
 F_loss =(( 1 - pt)**self.gamma)* CE_loss
if self.reduction == 'sum':
return F_loss.sum()
elif self.reduction == 'mean':
return F_loss.mean()<count_unique_values>
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
num_classes = len(np.unique(train_df['target']))
num_classes<feature_engineering>
ingredients_tr = lemma(ingredients_tr)
 ingredients_te = lemma(ingredients_te )
test_df['image_name'] = '512x512-test/512x512-test/' + test_df['image_name'] + '.jpg'<create_dataframe>
rawdf_tr['ingredients_lemma'] = ingredients_tr
rawdf_tr['ingredients_lemma_string'] = [' '.join(_ ).strip() for _ in rawdf_tr['ingredients_lemma']]
rawdf_te['ingredients_lemma'] = ingredients_te
rawdf_te['ingredients_lemma_string'] = [' '.join(_ ).strip() for _ in rawdf_te['ingredients_lemma']]
test_data = ImageList.from_df(test_df, img_path)
 test_data<prepare_output>
traindf = rawdf_tr[['cuisine', 'ingredients_lemma_string']].reset_index(drop=True)
 testdf = rawdf_te[['ingredients_lemma_string']]
labels_df = train_df[['image_id', 'target']].copy()
labels_df.head()<categorify>
X_train = 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)
 y_train = traindf['cuisine']
le = LE()
y_train_ec = le.fit_transform(y_train)
 X_pred = testdf['ingredients_lemma_string']
vectorizerts = TfidfVectorizer(stop_words='english')
 X_pred = vectorizertr.transform(X_pred)
def k_fold(df, num_fld, seed = seed):
for fold in range(num_fld):
df.loc[df.fold == fold, f'is_valid_{fold}'] = True
df.loc[df.fold != fold, f'is_valid_{fold}'] = False<concatenate>
clf_ovrc_svm = SVC(C=3.25, cache_size=500, class_weight=None, coef0=0.0,\
decision_function_shape='ovr', degree=3, gamma=1, kernel='rbf',\
max_iter=-1, probability=False, random_state=0, shrinking=True,\
tol=0.001, verbose=False)
 clf_ovrc_svm = clf_ovrc_svm.fit(X_train, y_train)
 y_pred_ovrc_svm = clf_ovrc_svm.predict(X_pred)
 testdf['cuisine'] = y_pred_ovrc_svm
d = pd.DataFrame(data=testdf['cuisine'], index=testdf.index ).sort_index().reset_index().to_csv('submission_ovr_svm.csv', index=False )