Kaggle competition: What's Cooking? (Kernels Only)
submission = pd.DataFrame({"ID_code": test.ID_code.values})
 submission["target"] = predictions
submission.to_csv("submission.csv", index=False )<load_from_csv>
import json
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import *
from sklearn.model_selection import GridSearchCV
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
train_data=pd.read_json('.. /input/train.json')
 test_data=pd.read_json('.. /input/test.json' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<randomize_order>
def preprocess(data):
Mydata=[]
for i in range(len(data['ingredients'])) :
a=" ".join(data['ingredients'][i])
 Mydata.append(a)
 return Mydata
train=preprocess(train_data)
 test=preprocess(test_data )
def shuffle_col_vals(x1):
rand_x = np.array([np.random.choice(x1.shape[0], size=x1.shape[0], replace=False)for i in range(x1.shape[1])] ).T
grid = np.indices(x1.shape)
 rand_y = grid[1]
return x1[(rand_x, rand_y)]
def augment_fast1(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
x1 = shuffle_col_vals(x1)
 xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
x1 = shuffle_col_vals(x1)
 xn.append(x1)
 xs = np.vstack(xs); xn = np.vstack(xn)
 ys = np.ones(xs.shape[0]);yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn]); y = np.concatenate([y,ys,yn])
 return x,y
def disarrange(a, axis=-1):

 b = a.swapaxes(axis, -1)
 shp = b.shape[:-1]
for ndx in np.ndindex(shp):
np.random.shuffle(b[ndx])
 return
def augment_fast2(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
disarrange(x1,axis=0)
 xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
disarrange(x1,axis=0)
 xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y<prepare_x_and_y>
vect=TfidfVectorizer()
train_TDF=vect.fit_transform(train)
 test_TDF=vect.transform(test )
%%time
x = train[train.columns[2:]].values
y = train['target'].values
x_ts = test[test.columns[1:]].values<train_model>
le=LabelEncoder()
enc_target=le.fit_transform(target)
 enc_target.shape
%%time
x1,y1 = augment_fast2(x,y,t=10)
 print(x.shape,y.shape,x1.shape,y1.shape )<import_modules>
import lightgbm as lgb
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold, KFold, train_test_split<split>
Y=pd.get_dummies(train_data.cuisine)
 input_data=Y.values
x_train,x_test,y_train,y_test = train_test_split(x1,y1, test_size = 1/9, random_state=1999111000 )<init_hyperparams>
train_TDF.toarray()
param = {
'bagging_freq': 5,
'bagging_fraction': 0.331,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.0405,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<create_dataframe>
scaler=MinMaxScaler()
scaler.fit(train_TDF.toarray())
 featuresNN=scaler.transform(train_TDF.toarray() )
trn_data = lgb.Dataset(x_train, label=y_train)
 val_data = lgb.Dataset(x_test, label=y_test )<train_model>
trainX,testX,trainY,testY=train_test_split(featuresNN,input_data,test_size=0.3 )
clf = lgb.train(param, trn_data, num_boost_round=30000, valid_sets = [trn_data, val_data], verbose_eval=2000, early_stopping_rounds = 3500 )<predict_on_test>
numfeat=trainX.shape[1]
numfeat
predictions = clf.predict(x_ts, num_iteration=clf.best_iteration )<train_model>
import keras
from keras.layers import *
from keras.models import Sequential
<save_to_csv>
model=keras.models.Sequential()
model.add(Dense(300,input_dim=numfeat,activation='relu'))
model.add(Dense(500,activation='relu'))
model.add(Dense(400,activation='relu'))
model.add(Dense(20,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer = 'adam',)
 model.fit(featuresNN,input_data,epochs=50,shuffle=True,batch_size=100 )
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub["target"] = predictions
sub.to_csv("submission.csv", index=False)
 sub.head()<define_variables>
a=model.predict_classes(test_TDF )
notebookstart = time.time()<set_options>
predicted=le.inverse_transform(a)
 predicted
warnings.filterwarnings('ignore')
 warnings.simplefilter(action='ignore', category=DeprecationWarning)
 warnings.simplefilter(action='ignore', category=UserWarning )<import_modules>
sub=pd.DataFrame({'id':test_data.id,'cuisine':predicted})
 sub.to_csv('sample_submission.csv',index=False )
import platform
import sys
import importlib
import multiprocessing
import random<set_options>