Kaggle competition: What's Cooking?
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?<load_pretrained>
sns.set_style("whitegrid")
 with open('.. /input/train.json', 'r')as f:
txt = f.read()
df = pd.DataFrame(json.loads(txt))
df.head()
model1 = BertForMultiClass.from_pretrained("/kaggle/input/bert-fold1/", num_labels=30)
 model2 = BertForMultiClass.from_pretrained("/kaggle/input/bert-fold2/", num_labels=30)
 model3 = BertForMultiClass.from_pretrained("/kaggle/input/bert-fold3/", num_labels=30)
 model4 = BertForMultiClass.from_pretrained("/kaggle/input/bert-fold4/", num_labels=30)
 model5 = BertForMultiClass.from_pretrained("/kaggle/input/bert-fold5/", num_labels=30)
 result = []
predictor1 = Predictor(model=model1, n_gpu='0')
 result1 = predictor1.predict(data=test_dataloader)
 predictor2 = Predictor(model=model2, n_gpu='0')
 result2 = predictor2.predict(data=test_dataloader)
 predictor3 = Predictor(model=model3, n_gpu='0')
 result3 = predictor3.predict(data=test_dataloader)
 predictor4 = Predictor(model=model4, n_gpu='0')
 result4 = predictor4.predict(data=test_dataloader)
 predictor5 = Predictor(model=model5, n_gpu='0')
 result5 = predictor5.predict(data=test_dataloader)
 result.append([result1, result2, result3, result4, result5] )<feature_engineering>
df['joined'] = df.ingredients.map(lambda x: ' '.join(x))
df_nb = df[['cuisine','joined']]
df_nb.head()
test_predictions = np.average(result[0], axis=0 )<define_variables>
count_vect = CountVectorizer()
tfidf_transformer = TfidfTransformer()
X = count_vect.fit_transform(df_nb.joined)
 X = tfidf_transformer.fit_transform(X)
 X.shape
n=test_df['url'].apply(lambda x:('english.stackexchange.com' in x)).tolist()
spelling=[]
for x in n:
if x:
spelling.append(0.5)
 else:
spelling.append(0.)<feature_engineering>
clf = MultinomialNB()
scores = cross_val_score(clf, X, df_nb.cuisine, cv=5)
 print('accuracy CV:',scores )
test_preds = test_predictions
y_train = train_df[target_cols].values
for column_ind in range(30):
curr_column = y_train[:, column_ind]
values = np.unique(curr_column)
 map_quantiles = []
for val in values:
occurrence = np.mean(curr_column == val)
 cummulative = sum(el['occurrence'] for el in map_quantiles)
 map_quantiles.append({'value': val, 'occurrence': occurrence, 'cummulative': cummulative})
 for quant in map_quantiles:
pred_col = test_preds[:, column_ind]
q1, q2 = np.quantile(pred_col, quant['cummulative']), np.quantile(pred_col, min(quant['cummulative'] + quant['occurrence'], 1))
pred_col[(pred_col >= q1)&(pred_col <= q2)] = quant['value']
test_preds[:, column_ind] = pred_col<load_from_csv>
def simple_NN(input_shape, nodes_per=[60], hidden=0, out=2, act_out='softmax', act_hid='relu', drop=True, d_rate=0.1):

 model = Sequential()
model.add(Dense(nodes_per[0],activation=act_hid,input_shape=input_shape))
if drop:
model.add(Dropout(d_rate))
try:
if hidden != 0:
for i,j in zip(range(hidden), nodes_per[1:]):
model.add(Dense(j,activation=act_hid))
if drop:
model.add(Dropout(d_rate))
model.add(Dense(out,activation=act_out))
return(model)
 except:
print('Error in generating hidden layers')
 ch_dict = dict([(y,x)for x,y in enumerate(set(df_nb.cuisine)) ])
 y = np.array([ch_dict[x] for x in df_nb.cuisine])
 y = to_categorical(y)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
 print('creating and training model...')
 model = simple_NN(input_shape=(X.shape[1],), nodes_per=[100, 100], hidden=1, out=y.shape[1], drop=True)
 model.compile(loss='categorical_crossentropy', optimizer=Adam() , metrics=['acc'])
 early_stopping_monitor = EarlyStopping(patience=3)
 history = model.fit(X_train,y_train, epochs=1000, validation_data=(X_test, y_test), callbacks=[early_stopping_monitor], verbose=True)
 print('model trained')
submission_df = pd.read_csv(ROOT+'sample_submission.csv')
 submission_df[target_cols] = test_preds
submission_df['question_type_spelling']=spelling
submission_df<save_to_csv>
with open('.. /input/test.json', 'r')as f:
txt = f.read()
df_test = pd.DataFrame(json.loads(txt))
df_test['joined'] = df_test.ingredients.map(lambda x: ' '.join(x))
df_test = df_test.drop(['ingredients'], axis=1)
 df_test.head()
sub_file_name = 'submission.csv'
submission_df.to_csv(sub_file_name, index=False)
<set_options>
dec_dict = dict([(x,y)for y,x in ch_dict.items() ])
 X_test = np.array(df_test.joined)
 X_test = count_vect.transform(X_test)
 X_test = tfidf_transformer.transform(X_test)
 preds = model.predict(X_test)
 y_test = [dec_dict[np.argmax(x)] for x in preds]
df_test['cuisine'] = y_test
df_test.head()
<load_from_csv><EOS> 
df_test = df_test.drop('joined', axis=1)
 df_test.to_csv('result.csv', index=False)
 print('written to csv.' )