Kaggle competition: What's Cooking? (Kernels Only)
data.show_batch(rows = 3, figsize =(10,8))<choose_model_class>
train_df = pd.read_json('.. /input/train.json')
 test_df = pd.read_json('.. /input/test.json')
 train_df['seperated_ingredients'] = train_df['ingredients'].apply(','.join)
 test_df['seperated_ingredients'] = test_df['ingredients'].apply(','.join )
learn = cnn_learner(data, models.resnet101, metrics = accuracy, model_dir='/tmp/model/' )<find_best_params>
ingreList = []
for index, row in train_df.iterrows() :
ingre = row['ingredients']
for i in ingre:
if i not in ingreList:
ingreList.append(i)
 def binary(ingre_list):
binaryList = []
for item in ingreList:
if item in ingre_list:
binaryList.append(1)
 else:
binaryList.append(0)
 return binaryList
train_df['bin ingredients']=train_df['ingredients'].apply(lambda x: binary(x))
learn.lr_find()<define_search_space>
def Similarity(Id1, Id2):
a = train_df.iloc[Id1]
b = train_df.iloc[Id2]
A = a['bin ingredients']
B = b['bin ingredients']
distance=spatial.distance.cosine(A,B)
 return distance, Id2
lr = 3e-02<train_model>
food=[]
for i in train_df.index:
food.append(Similarity(1,i))
common_ingredients=sorted(food,key=lambda x: x[0])[1:10]
indexes=[]
for i in range(len(common_ingredients)) :
indexes.append(common_ingredients[i][1])
 train_df.iloc[indexes]
learn.fit_one_cycle(3, slice(lr))<save_model>
import nltk
from collections import Counter
learn.save('resnet-101-1' )<choose_model_class>
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' )
learn = cnn_learner(data, models.densenet161, metrics = accuracy, model_dir='/tmp/model/' )<find_best_params>
vect = TfidfVectorizer(binary=True ).fit(train_df['seperated_ingredients'].values)
 X_train_vectorized = vect.transform(train_df['seperated_ingredients'].values)
 X_train_vectorized = X_train_vectorized.astype('float')
 Result_transformed = vect.transform(test_df['seperated_ingredients'].values)
 Result_transformed = Result_transformed.astype('float' )
learn.lr_find()<define_search_space>
encoder = LabelEncoder()
y_transformed = encoder.fit_transform(train_df.cuisine )
lr = 3e-02<train_model>
X_train, X_test, y_train, y_test = train_test_split(X_train_vectorized, y_transformed , random_state = 0)
 clf1 = LogisticRegression(C=10,dual=False)
 clf1.fit(X_train , y_train)
 clf1.score(X_test, y_test )
learn.fit_one_cycle(3, slice(lr))<save_model>
vclf=VotingClassifier(estimators=[('clf1',LogisticRegression(C=10,dual=False)) ,('clf2',SVC(C=100,gamma=1,kernel='rbf',probability=True)) ],voting='soft',weights=[1,2])
 vclf.fit(X_train , y_train)
 vclf.score(X_test, y_test )
learn.save('densenet-161-1' )<choose_model_class>
y_predicted = vclf.predict(Result_transformed)
 y_predicted_final = encoder.inverse_transform(y_predicted)
 predictions = pd.DataFrame({'cuisine' : y_predicted_final , 'id' : test_df.id })
 predictions = predictions[[ 'id' , 'cuisine']]
predictions.to_csv('submit.csv', index = False )