Kaggle competition: What's Cooking?
test_image_path = base_path / 'test' / 'test'<define_variables>
archive_train = zipfile.ZipFile("/kaggle/input/whats-cooking/train.json.zip", 'r')
 archive_submission = zipfile.ZipFile("/kaggle/input/whats-cooking/test.json.zip", 'r' )
def add_full_path(file_name):
full_path = str(test_image_path)+ '/' + file_name
return full_path<feature_engineering>
dataset_train = pd.read_json(archive_train.read('train.json'))
dataset_submission = pd.read_json(archive_submission.read('test.json'))
dataset_train_copy = dataset_train.copy()
dataset_submission_copy = dataset_submission.copy()
df_submission['id'] = df_submission['id'].apply(add_full_path )<normalization>
train_dict_count = {}
for entry_list in dataset_train_copy['ingredients']:
for entry in entry_list:
if entry in train_dict_count.keys() :
train_dict_count[entry] += 1
else:
train_dict_count[entry] = 1
def read_image(image_path):
image = Image.open(image_path)
 image = image.convert('RGB')
 image = image.resize(image_size, Image.ANTIALIAS)
 return image<categorify>
submission_dict_count = {}
for entry_list in dataset_submission_copy['ingredients']:
for entry in entry_list:
if entry in submission_dict_count.keys() :
submission_dict_count[entry] += 1
else:
submission_dict_count[entry] = 1
test_transform = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])
 ] )<predict_on_test>
for key in train_dict_count:
if key not in submission_dict_count.keys() :
submission_dict_count[key] = 0
def get_predictions(image_path):
image = read_image(image_path)
 image = test_transform(image)
 image = torch.unsqueeze(image, 0)
 pred = model(image)
 p_cactus = torch.nn.functional.softmax(pred, dim=1)[0][1].item()
return p_cactus 
<feature_engineering>
for cols in train_dict_count.keys() :
dataset_train_copy[cols] = np.zeros(( dataset_train_copy.shape[0], 1))
df_submission['has_cactus'] = df_submission['id'].apply(get_predictions )<drop_column>
for cols in submission_dict_count.keys() :
dataset_submission_copy[cols] = np.zeros(( dataset_submission_copy.shape[0], 1))
def remove_full_path(image_path):
paths = image_path.split("/")
 path = paths[4]
return path<feature_engineering>
for column_name in dataset_train_copy.columns:
if column_name not in ['id', 'cuisine', 'ingredients']:
for index, each_entry_list in enumerate(dataset_train_copy['ingredients']):
if column_name in each_entry_list:
dataset_train_copy.loc[index, column_name] = 1
df_submission['id'] = df_submission['id'].apply(remove_full_path )<save_to_csv>
for column_name in dataset_submission_copy.columns:
if column_name not in ['id', 'ingredients']:
for index, each_entry_list in enumerate(dataset_submission_copy['ingredients']):
if column_name in each_entry_list:
dataset_submission_copy.loc[index, column_name] = 1
df_submission.to_csv('submission_resnet50_unfreeze_1.csv', index=None )<load_pretrained>
dataset_submission_copy = dataset_submission_copy[dataset_train_copy.drop('cuisine', axis=1 ).columns]
base_dir = Path(".. /input")
 save_dir = Path('/kaggle/working')
 model_dir= Path('/tmp/models')
 base = base_dir
base.ls()<define_variables>
dataset_train_copy.drop(['id', 'ingredients', 'cuisine'], axis=1, inplace=True)
 dataset_submission_copy.drop(['id', 'ingredients'], axis=1, inplace=True )
train_folder, train_path, sample_sub_path, test_path= base.ls()<set_options>
X = dataset_train_copy.values
y = dataset_train.iloc[:,1].values
X_submission = dataset_submission_copy.values
train_folder.ls()<load_from_csv>
le = LabelEncoder()
y = le.fit_transform(y)
 print(y )
train_df = pd.read_csv(train_path); train_df.head()<categorify>
classifier = LogisticRegression(random_state = 0)
 classifier.fit(X, y )
np.random.seed(42)
 data_il = ImageList.from_df(train_df,train_folder)
 data_il = data_il.split_by_rand_pct()
data_il = data_il.label_from_df()
data_il = data_il.transform(get_transforms() ,size=32 )<categorify>
y_submission = classifier.predict(X_submission)
 y_submission = le.inverse_transform(y_submission)
 Id = dataset_submission.iloc[:,0]
submission_dataset = pd.DataFrame({'id':Id, 'cuisine':y_submission})
 submission_dataset.set_index('id', inplace=True)
 submission_dataset.to_csv('Submission.csv' )