Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<choose_model_class>
%matplotlib inline
init_notebook_mode(connected=True)
 warnings.filterwarnings("ignore")
 notebookstart= time.time()
def get_model(cfg):
model_input = tf.keras.Input(shape=(cfg['net_size'][0], cfg['net_size'][0], 3), name='imgIn')
 model_input_data = tf.keras.Input(shape=(10,), name='dataIn')
 dummy = tf.keras.layers.Lambda(lambda x:x )(model_input)
 dense = tf.keras.layers.Dense(1024, activation='relu' )(model_input_data)
 dense = tf.keras.layers.BatchNormalization()(dense)
 dense = tf.keras.layers.Dropout(0.5 )(dense)
 dense = tf.keras.layers.Dense(256, activation='relu' )(dense)
 dense = tf.keras.layers.BatchNormalization()(dense)
 dense = tf.keras.layers.Dropout(0.5 )(dense)
 outputs = []
for i in range(cfg['net_count']):
constructor = getattr(efn, f'EfficientNetB{i}')
 x = constructor(include_top=False, weights='imagenet',
input_shape=(cfg['net_size'][i], cfg['net_size'][i], 3),
pooling='avg' )(dummy)
 x = tf.keras.layers.Concatenate()([x, dense])
 x = tf.keras.layers.Dense(256, activation='relu' )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.5 )(x)
 x = tf.keras.layers.Dense(1, activation='sigmoid' )(x)
 outputs.append(x)
 model = tf.keras.Model([model_input, model_input_data], outputs, name='aNetwork')
 model.summary()
return model<compute_train_metric>
train_df = pd.read_json('.. /input/train.json')
 test_df = pd.read_json('.. /input/test.json')
 train=train_df
train.head(15 )
def binary_focal_loss(gamma=2., alpha=.25):

 def binary_focal_loss_fixed(y_true, y_pred):

 pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
epsilon = K.epsilon()
pt_1 = K.clip(pt_1, epsilon, 1.- epsilon)
 pt_0 = K.clip(pt_0, epsilon, 1.- epsilon)
 return -K.sum(alpha * K.pow(1.- pt_1, gamma)* K.log(pt_1)) \
-K.sum(( 1 - alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0))
return binary_focal_loss_fixed<choose_model_class>
train=train_df
total = train.isnull().sum().sort_values(ascending = False)
 percent =(train.isnull().sum() /train.isnull().count() *100 ).sort_values(ascending = False)
 missing_train_data = pd.concat([total, percent], axis=1, keys=['Total missing', 'Percent missing'])
 print("
print(missing_train_data.head() )
def compile_new_model(cfg, old_model=None):
with strategy.scope() :
if(old_model is None):
model = get_model(cfg)
 else:
model = old_model
losses = [tfa.losses.SigmoidFocalCrossEntropy(gamma = 2.0, alpha = 0.80)for i in range(cfg['net_count'])]
lr = tf.keras.optimizers.schedules.ExponentialDecay(
 initial_learning_rate=0.1,
decay_steps=10000,
decay_rate=0.9)
 model.compile(
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.1),
loss = losses,
metrics = [tf.keras.metrics.AUC(name='auc')])
 return model<load_from_csv>
train_df['seperated_ingredients'] = train_df['ingredients'].apply(','.join)
 test_df['seperated_ingredients'] = test_df['ingredients'].apply(','.join)
 train_df['for ngrams']=train_df['seperated_ingredients'].str.replace(',',' ')
 def generate_ngrams(text, n):
words = text.split(' ')
 iterations = len(words)- n + 1
for i in range(iterations):
yield words[i:i + n]
def net_diagram(*cuisines):
ngrams = {}
for title in train_df[train_df.cuisine==cuisines[0]]['for ngrams']:
for ngram in generate_ngrams(title, 2):
ngram = ','.join(ngram)
 if ngram in ngrams:
ngrams[ngram] += 1
else:
ngrams[ngram] = 1
ngrams_mws_df = pd.DataFrame.from_dict(ngrams, orient='index')
 ngrams_mws_df.columns = ['count']
ngrams_mws_df['cusine'] = cuisines[0]
ngrams_mws_df.reset_index(level=0, inplace=True)
 ngrams = {}
for title in train_df[train_df.cuisine==cuisines[1]]['for ngrams']:
for ngram in generate_ngrams(title, 2):
ngram = ','.join(ngram)
 if ngram in ngrams:
ngrams[ngram] += 1
else:
ngrams[ngram] = 1
ngrams_mws_df1 = pd.DataFrame.from_dict(ngrams, orient='index')
 ngrams_mws_df1.columns = ['count']
ngrams_mws_df1['cusine'] = cuisines[1]
ngrams_mws_df1.reset_index(level=0, inplace=True)
 cuisine1=ngrams_mws_df.sort_values('count',ascending=False)[:25]
cuisine2=ngrams_mws_df1.sort_values('count',ascending=False)[:25]
df_final=pd.concat([cuisine1,cuisine2])
 g = nx.from_pandas_dataframe(df_final,source='cusine',target='index')
 cmap = plt.cm.RdYlGn
colors = [n for n in range(len(g.nodes())) ]
k = 0.35
pos=nx.spring_layout(g, k=k)
 nx.draw_networkx(g,pos, node_size=df_final['count'].values*8, cmap = cmap, node_color=colors, edge_color='grey', font_size=15, width=3)
 plt.title("Top 25 Bigrams for %s and %s" %(cuisines[0],cuisines[1]), fontsize=30)
 plt.gcf().set_size_inches(30,30)
 plt.show()
plt.savefig('network.png')
 net_diagram('french','cajun_creole' )
train = pd.read_csv('.. /input/siim-isic-melanoma-classification/train.csv')
 class_weights = class_weight.compute_class_weight('balanced',
np.unique(train.target),
train.target)
 class_weights = dict(enumerate(class_weights))<define_variables>
df = pd.read_json('.. /input/train.json' ).set_index('id')
 test_df = pd.read_json('.. /input/test.json' ).set_index('id')
 traindex = df.index
testdex = test_df.index
y = df.cuisine.copy()
df = pd.concat([df.drop("cuisine", axis=1), test_df], axis=0)
 df_index = df.index
del test_df; gc.collect() ;
vect = CountVectorizer()
dummies = vect.fit_transform(df.ingredients.str.join(' '))
df = pd.DataFrame(dummies.todense() ,columns=vect.get_feature_names())
 df.index= df_index
X = df.loc[traindex,:]
test_df = df.loc[testdex,:]
del df; gc.collect() ;
file_path=".. /input/output/effnet_comb_weights.best.hdf5" 
<create_dataframe>
def read_dataset(path):
return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json')
 def generate_text(data):
text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
return text_data
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]
tfidf = TfidfVectorizer(binary=True)
 def tfidf_features(txt, flag):
if flag == "train":
x = tfidf.fit_transform(txt)
 else:
x = tfidf.transform(txt)
 x = x.astype('float16')
 return x
X2 = tfidf_features(train_text, flag="train")
 X_test3 = tfidf_features(test_text, flag="test")
 lb = LabelEncoder()
y2 = lb.fit_transform(target )
ds_train = get_dataset(files_train, CFG, augment=True, shuffle=True, repeat=True)
 steps_train = count_data_items(files_train)/(CFG['batch_size'] * REPLICAS )<choose_model_class>
def compareAccuracy(a, b):
print('
Compare Multiple Classifiers:
')
 print('K-Fold Cross-Validation Accuracy:
')
 names = []
models = []
resultsAccuracy = []
models.append(( 'LR', LogisticRegression()))
models.append(( 'LSVM', LinearSVC()))
models.append(( 'RF', RandomForestClassifier()))
for name, model in models:
model.fit(a, b)
 kfold = model_selection.KFold(n_splits=10, random_state=7)
 accuracy_results = model_selection.cross_val_score(model, a,b, cv=kfold, scoring='accuracy')
 resultsAccuracy.append(accuracy_results)
 names.append(name)
 accuracyMessage = "%s: %f(%f)" %(name, accuracy_results.mean() , accuracy_results.std())
 print(accuracyMessage)
 fig = plt.figure()
fig.suptitle('Algorithm Comparison: Accuracy')
 ax = fig.add_subplot(111)
 plt.boxplot(resultsAccuracy)
 ax.set_xticklabels(names)
 ax.set_ylabel('Cross-Validation: Accuracy Score')
 plt.show()
def defineModels() :
print('
LR = LogisticRegression')
 print('LSVM = LinearSVM')
 print('RF = RandomForestClassifier' )
print("Buidling model...")
 model = compile_new_model(CFG )<train_model>
compareAccuracy(X2,y2)
 defineModels()
print("Disabling effnets for training...")
 for i in range(CFG['net_count']):
for layer in model.layers:
if(layer.name==f'efficientnet-b{i}'):
layer.trainable = False
break
print("Rebuilding model")
 model = compile_new_model(CFG, model)
 history = model.fit(ds_train,
verbose = 1,
steps_per_epoch = steps_train,
epochs = 5,
class_weight=class_weights )<train_model>
model = LinearSVC()
model.fit(X, y)
 submission = model.predict(test_df)
 submission_df = pd.Series(submission, index=testdex ).rename('cuisine')
 submission_df.to_csv("recipe_submission.csv", index=True, header=True)
 model.fit(X2, y2)
 y_test3 = model.predict(X_test3)
 y_pred = lb.inverse_transform(y_test3)
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('recipe_submission2.csv', index=False )