Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<save_to_csv>
def initi(m):
if isinstance(m,nn.Linear):
nn.init.xavier_uniform(m.weight.data)
 with open('.. /input/train.json')as f:
train = json.load(f)
 with open('.. /input/test.json')as f:
test = json.load(f)
 print('imported' )
my_submission_lbgm.to_csv('submission_lbgm.csv', index = False, header = True)
 my_submission_train.to_csv('submission_lbgm_train.csv', index = False, header = True)
<set_options>
shuffle(train)
 xx=[d['ingredients'] for d in train]
yy=[d['cuisine'] for d in train]
sns.set(font_scale=1)
 warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings('ignore' )<load_from_csv>
word_list=set()
for x in xx:
word_list|=set(tuple(x))
word_list=list(word_list)
 cat_list=list(set(yy))
random_state = 2434
np.random.seed(random_state)
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<create_dataframe>
newx=[]
for num,rec in enumerate(xx):
newx.append(np.zeros(len(word_list)))
 for ing in rec:
if ing in word_list:
ing_location=word_list.index(ing)
 newx[num][ing_location]=1
newx=np.array(newx)
 newy2=[]
for num,rec in enumerate(yy):
newy2.append(cat_list.index(rec))
newy2=np.array(newy2)
 print('First 3 recepies of one hot vectors')
 print(newx[0:5])
 print('First 5 cuisines in numerical')
 print(newy2[0:5] )
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 oof = train[['ID_code', 'target']]
oof['predict'] = 0
predictions = test[['ID_code']]
val_aucs = []
feature_importance = pd.DataFrame()<prepare_x_and_y>
def stats(prevloss):
train_loss_history.append(loss.data[0])
 valpred=model(txval)
 valloss=loss_fn(valpred,tyval)
 val_loss_history.append(valloss.data[0])
 actualy=tyval.cpu().data.numpy()
predy=valpred.cpu().data.numpy()
val_accuracy=(np.argmax(predy,axis=1)==actualy ).sum() /len(predy)
 val_accuracy_history.append(val_accuracy)
 if t%10==0:
print('Iteration
print('Current valdiation loss')
 print(round(valloss.data[0],2))
print('Previous validation loss')
 print(round(prevloss,2))
print('Validation accuracy: {} %'.format(round(100*val_accuracy,3)))

prevloss=valloss.data[0]
return(prevloss)
features = [col for col in train.columns if col not in ['target', 'ID_code']]
X_test = test[features]<count_unique_values>
dtype = torch.cuda.FloatTensor
SPLIT = 0.8
tv=int(np.ceil(len(newx)*SPLIT))
xtrain=newx[0:tv]
ytrain=newy2[0:tv]
xval=newx[tv:]
yval=newy2[tv:]

 txval=Variable(torch.from_numpy(xval.astype(float)).float() ).type(dtype)
 tyval=Variable(torch.from_numpy(yval.astype(float)).long() ).cuda()
tx=Variable(torch.from_numpy(xtrain.astype(float)).float() ).type(dtype)
 ty=Variable(torch.from_numpy(ytrain.astype(float)).long() ).cuda()
print(tx.shape)
all_df = pd.concat([train.iloc[:, 2:], test.iloc[:, 1:]])
 unique_maps = dict()
for col in tqdm(list(all_df)) :
map_ = all_df[col].value_counts()
unique_maps[col] = map_<statistical_test>
batch_size=50
model= nn.Sequential(
 nn.Linear(6714,20),
nn.LeakyReLU() , 
)
 model.cuda()
model.zero_grad()
model.apply(initi)
 loss_fn=nn.CrossEntropyLoss()
print('built new model')
delta_dics = {}
delta2_dics = {}
for col in ['var_108']:
kde = st.gaussian_kde(all_df[col])
 uvar = np.unique(np.hstack([all_df[col].unique() , all_df[col].unique() +.0001, all_df[col].unique() -.0001]))
uvar.sort()
gs = []
for i in tqdm(range(-(-uvar.shape[0]//20))):
gs.extend(list(kde(uvar[20*i:20*(i+1)])))
 dic_ = {uvar[i]: gs[i]-gs[i-1] for i in range(1, uvar.shape[0])}
delta_dics[col] = dic_<feature_engineering>
lr=1e-4
optimizer = torch.optim.Adam(model.parameters() , lr=lr,betas=(0.9,0.99))
optimizer.zero_grad()
print('made new optimizer')
def FE(X):
unique_df = pd.DataFrame()
for col in unique_maps.keys() :
unique_df["unique_" + col] = X[col].map(unique_maps[col])
 X["var_uniques"] =(unique_df <= 2 ).mean(axis=1)
 for col in unique_maps.keys() :
X[col + "_uniques"] = unique_df["unique_" + col]
epoch_datetime = pd.datetime(1900, 1, 1)
 s =(X['var_68']*10000 - 7000 + epoch_datetime.toordinal() ).astype(int ).map(datetime.fromordinal)
 X["dayofyear"] = s.dt.dayofyear
for key in ['var_108']:
X[key + "_delta"] = X[key].map(delta_dics[key])
 return X<categorify>
epochs=170
itt=0
train_loss_history=[]
val_loss_history=[]
val_accuracy_history=[]
prevloss=90
steps_back=0
def augment(x,y,t=2):
xs,xn = [],[]
for i in tqdm(range(t)) :
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//6):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
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<split>
for t in range(epochs):
itt=0
for curr in range(batch_size,len(tx),batch_size):
x=tx[itt:curr]
y=ty[itt:curr]
pred=model(x)
 loss=loss_fn(pred,y)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
itt+=batch_size
prevloss=stats(prevloss)
 print('Trained!')
features = [col for col in train.columns if col not in ['target', 'ID_code']]
X_test = test[features]
X_test = FE(X_test)
 for fold,(trn_idx, val_idx)in enumerate(skf.split(train, train['target'])) :
X_train, y_train = train.iloc[trn_idx][features], train.iloc[trn_idx]['target']
X_valid, y_valid = train.iloc[val_idx][features], train.iloc[val_idx]['target']
N = 5
p_valid,yp = 0,0
X_valid = FE(X_valid)
 for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values, t=12)
 X_t = pd.DataFrame(X_t)
 X_t = X_t.add_prefix('var_')
 X_t = FE(X_t)
 dev_pool = Pool(X_t,
y_t)
 val_pool = Pool(X_valid,
y_valid)
 evals_result = {}
model = CatBoostClassifier(eval_metric="AUC",
custom_loss='Logloss',
depth=3,
subsample=.2,
l2_leaf_reg=1,
verbose=1000,
early_stopping_rounds=500,
bootstrap_type="Bernoulli",
learning_rate=0.02,
task_type="GPU",
random_seed=2434 + fold*5 + i,
od_type="Iter",
iterations=60000, 
)
 model.fit(dev_pool, eval_set=val_pool)
 p_valid += model.predict_proba(X_valid)[:, 1]
yp += model.predict_proba(X_test)[:, 1]
del X_t, dev_pool
collect()
oof['predict'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp/N<save_to_csv>
xx=[d['ingredients'] for d in test]
testx=[]
for num,rec in enumerate(xx):
testx.append(np.zeros(len(word_list)))
 for ing in rec:
if ing in word_list:
testx[num][word_list.index(ing)]=1
testx=np.array(testx)
 testx=torch.from_numpy(testx.astype(float)).float()
testx=Variable(testx ).type(dtype)
 pred=model(testx)
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 predictions.to_csv('cat_all_predictions.csv', index=None)
 sub = pd.DataFrame({"ID_code":test["ID_code"].values})
 sub["target"] = predictions['target']
sub.to_csv("cat_submission.csv", index=False)
 oof.to_csv('cat_oof.csv', index=False )<create_dataframe>
results1=pred.data.cpu().numpy()
resdf = pd.DataFrame([model.feature_importances_,
model.feature_names_] ).T.sort_values(0, ascending=False )<define_variables>
answers=[]
for num,val in enumerate(test):
answers.append(str(val['id'])+','+cat_list[int(np.argmax(results1[num])) ])
 answers.insert(0,'id,cuisine')
 print('predicted!')
 print(answers[17])
original_feat = [f"var_{i}" for i in range(200)]
search_cols = resdf[resdf[1].isin(original_feat)][1].tolist()<define_variables>
np.savetxt("one_layer_1000_epoch.csv", answers, delimiter=",", fmt='%s' )