Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<save_to_csv>
import pandas as pd
import numpy as np
import json
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
import re
from sklearn.linear_model import LogisticRegression
subm = pd.DataFrame({'ID_code':df_test['ID_code'], 'target':pd.Series(subm[1])})
 subm.to_csv('submissionS.csv', index=False )<import_modules>
trainData = pd.read_json('.. /input/train.json')
 testData = pd.read_json('.. /input/test.json')
 trainData.head()
import lightgbm as lgb
import pandas as pd
import numpy as np
from sklearn.metrics import roc_auc_score
from datetime import datetime
from pathlib import Path
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import RepeatedStratifiedKFold<load_from_csv>
lemma=WordNetLemmatizer()
def clean_review(review_col):
review_corpus=[]
for i in range(0,len(review_col)) :
review=str(review_col[i])
 review=re.sub('[^a-zA-Z]',' ',review)
 review=[lemma.lemmatize(w)for w in word_tokenize(str(review ).lower())]
review=' '.join(review)
 review_corpus.append(review)
 return review_corpus
path=Path(".. /input/")
 train=pd.read_csv(path/"train.csv" ).drop("ID_code",axis=1)
 test=pd.read_csv(path/"test.csv" ).drop("ID_code",axis=1 )<categorify>
trainData['clean_ingredients']=clean_review(trainData.ingredients.values)
 testData['clean_ingredients']=clean_review(testData.ingredients.values)
 trainData.head()
def augment(train,num_n=1,num_p=2):
newtrain=[train]
n=train[train.target==0]
for i in range(num_n):
newtrain.append(n.apply(lambda x:x.values.take(np.random.permutation(len(n)))))
 for i in range(num_p):
p=train[train.target>0]
newtrain.append(p.apply(lambda x:x.values.take(np.random.permutation(len(p)))))
 return pd.concat(newtrain)
<init_hyperparams>
tfidf=TfidfVectorizer()
xtrain=tfidf.fit_transform(trainData.clean_ingredients ).toarray()
xtest=tfidf.transform(testData.clean_ingredients ).toarray()
param = {'num_leaves': 16,
'min_data_in_leaf': 42,
'tree_learner': 'serial',
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.01,
'boosting': 'gbdt',
'bagging_freq': 2,
'bagging_fraction': 0.4,
'feature_fraction': 0.05,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'min_gain_to_split': 7,
'num_threads': -1,
'min_sum_hessian_in_leaf': 10.0,
'boost_from_average':'false'} 
<train_model>
lb = LabelEncoder()
yTrain = lb.fit_transform(trainData['cuisine'] )
result=np.zeros(test.shape[0])
 oof = np.zeros(len(train))
rskf = RepeatedStratifiedKFold(n_splits=5, n_repeats=5,random_state=10)
 for counter,(train_index, valid_index)in enumerate(rskf.split(train, train.target),1):
print(counter)
 t=train.iloc[train_index]
t=augment(t)
 trn_data = lgb.Dataset(t.drop("target",axis=1), label=t.target)
 v=train.iloc[valid_index]
val_data = lgb.Dataset(v.drop("target",axis=1), label=v.target)
 model = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
 result +=model.predict(test, num_iteration=model.best_iteration)
 oof[valid_index]+=model.predict(train.iloc[valid_index], num_iteration=model.best_iteration)
<compute_test_metric>
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(xtrain , yTrain)
 vclf.score(xtrain , yTrain )
score=roc_auc_score(train.target, oof/counter)
 print("CV score: {:<8.8f}".format(score))<save_to_csv>
pred = vclf.predict(xtest)
 ypred = lb.inverse_transform(pred )
<load_from_csv><EOS> 
sub = pd.DataFrame({'id': testData['id'], 'cuisine': ypred})
 sub.to_csv('svm_output.csv', index=False )