Kaggle competition: What's Cooking?
test = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv')
 test.head()<count_missing_values>
archive_train=zipfile.ZipFile('/kaggle/input/whats-cooking/train.json.zip','r')
 train_data=pd.read_json(archive_train.read('train.json'))
train_data.head()
missing = False
for i in test.columns:
if len(pd.notnull(test[i])==True)!=len(test):
print(i, ": ",len(pd.notnull(test[i])))
 missing = True
if not missing:
print("No missing Data in test." )<feature_engineering>
archive_test=zipfile.ZipFile('/kaggle/input/whats-cooking/test.json.zip','r')
 test_data=pd.read_json(archive_test.read('test.json'))
test_data.head()
str(np.round(len(data[data['target']==1])/(len(data[data['target']==0])+ len(data[data['target']==0])) *100,2)) +' %, ratio of positive class'<feature_engineering>
train_data['cuisine'].value_counts()
stat_table["var"] = stat_table.apply(lambda x: np.abs(x["Class 0"] - x["Class 1"])>0.05 , 1)
 stat_table = stat_table[stat_table['var']==True] 
<prepare_x_and_y>
train_data.isna().sum()
data = all_data.iloc[:len(data),:].drop('ID_code', 1)
 test = all_data.iloc[len(data):,:].drop('target', 1 )<feature_engineering>
test_data.isna().sum()
data['average_row'] = data[init_cols].apply(lambda x: np.round(np.mean(x),5), 1)
 test['average_row'] = test[init_cols].apply(lambda x: np.round(np.mean(x),5), 1 )<feature_engineering>
train_ingredients_count={}
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
if j in train_ingredients_count.keys() :
train_ingredients_count[j]+=1
else:
train_ingredients_count[j]=1
data['std_row'] = data[init_cols].apply(lambda x: np.round(np.std(x),5), 1)
 test['std_row'] = test[init_cols].apply(lambda x: np.round(np.std(x),5), 1 )<count_values>
test_ingredients_count={}
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
if j in test_ingredients_count.keys() :
test_ingredients_count[j]+=1
else:
test_ingredients_count[j]=1
all_data = pd.concat([data, test])
 for i in init_cols:
counts = all_data[i].value_counts()
all_data[i+"_count"] = all_data[i].map(counts )<feature_engineering>
ingredients_missing_train=[]
for i in test_ingredients_count.keys() :
if i not in train_ingredients_count.keys() :
ingredients_missing_train.append(i)
 print(len(ingredients_missing_train))
powers = [2, 3]
for p in powers:
for i in init_cols:
all_data[i+"_"+str(p)] = np.power(all_data[i], p )<prepare_x_and_y>
for i in ingredients_missing_train:
train_ingredients_count[i]=0
print(len(train_ingredients_count))
data = all_data.iloc[:len(data),:].drop('ID_code', 1)
 test = all_data.iloc[len(data):,:].drop('target', 1 )<set_options>
ingredients_missing=[]
for i in train_ingredients_count.keys() :
if i not in test_ingredients_count.keys() :
ingredients_missing.append(i)
 print(len(ingredients_missing))
del all_data
gc.collect()<prepare_x_and_y>
for i in ingredients_missing:
test_ingredients_count[i]=0
print(len(test_ingredients_count))
X = subsampled_data[cols].values
y = subsampled_data['target'].values
scaler = MinMaxScaler()
scaler.fit(X)
 X = scaler.transform(X)
 X = TSNE(2, n_jobs=-1 ).fit_transform(X)
 df = pd.DataFrame()
df['target'] = y
df[['x', 'y']] = X
sns.scatterplot('x', 'y', 'target', data=df)
<prepare_output>
for i in train_ingredients_count.keys() :
train_data[i]=np.zeros(len(train_data))
for i in test_ingredients_count.keys() :
test_data[i]=np.zeros(len(test_data))
submission = test.copy()
submission['target'] = 1
submission = submission [['ID_code', 'target']] 
<import_modules>
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
train_data[j].iloc[i]=1
from sklearn.preprocessing import StandardScaler
from collections import Counter
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score, roc_auc_score
from imblearn.over_sampling import RandomOverSampler
from sklearn.model_selection import GridSearchCV<train_on_grid>
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
test_data[j].iloc[i]=1
X = data.drop('target', 1 ).values
y = data['target'].values
print('Original Dataset', Counter(y))
scaler = StandardScaler()
scaler.fit(X)
 X = scaler.transform(X)
 nm = RandomOverSampler()
X, y = nm.fit_resample(X, y)
 print('Resampled dataset shape %s' % Counter(y))
grid={"C":np.logspace(-3,3,4)}
logreg=LogisticRegression()
logreg_cv=GridSearchCV(logreg,grid,cv=10)
 logreg_cv.fit(X,y)
 print("tuned hpyerparameters :(best parameters)",logreg_cv.best_params_)
 print("accuracy :",logreg_cv.best_score_)
 scores_f1 = []
scores_roc = []
C = logreg_cv.best_params_['C']
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
 for train_ix, test_ix in kfold.split(X, y):
train_X, test_X = X[train_ix], X[test_ix]
train_y, test_y = y[train_ix], y[test_ix]
clf = LogisticRegression(C=C,)
 clf.fit(train_X, train_y)
 y_hat = clf.predict(train_X)
 scores_f1.append(f1_score(train_y, y_hat))
y_hat = clf.predict_proba(train_X)[:,1]
scores_roc.append(roc_auc_score(train_y, y_hat))
print('F1 Score: %f, +/-%f'%(np.mean(scores_f1), np.std(scores_f1)))
 print('ROC AUC Score: %f, +/-%f'%(np.mean(scores_roc), np.std(scores_roc)))
 clf = LogisticRegression(C=C)
 clf.fit(X, y)
 coefs = np.abs(clf.coef_[0])
 indices = np.argsort(coefs)[::-1]
plt.figure()
plt.title("Feature importances(Logistic Regression)")
 plt.bar(range(15), coefs[indices[:15]],
color="r", align="center")
 plt.xticks(range(15), data.columns[indices[:15]], rotation=45, ha='right')
 plt.subplots_adjust(bottom=0.3 )<find_best_model_class>
test_data=test_data[train_data.drop('cuisine',axis=1 ).columns]
X = data.drop('target', 1 ).values
y = data['target'].values
print('Original Dataset', Counter(y))
nm = RandomOverSampler()
X, y = nm.fit_resample(X, y)
 scores_f1 = []
scores_roc = []
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
 for train_ix, test_ix in kfold.split(X, y):
train_X, test_X = X[train_ix], X[test_ix]
train_y, test_y = y[train_ix], y[test_ix]
clf = GaussianNB()
clf.fit(train_X, train_y)
 y_hat = clf.predict(train_X)
 scores_f1.append(f1_score(train_y, y_hat))
y_hat = clf.predict_proba(train_X)[:,1]
scores_roc.append(roc_auc_score(train_y, y_hat))
print('F1 Score: %f, +/-%f'%(np.mean(scores_f1), np.std(scores_f1)))
 print('ROC AUC Score: %f, +/-%f'%(np.mean(scores_roc), np.std(scores_roc)))
 clf = GaussianNB()
clf.fit(X, y)
 y_hat = clf.predict_proba(X)[:,1]
print('ROC AUC Score: %f'%(roc_auc_score(y, y_hat)))
<init_hyperparams>
X=train_data.drop(['id','ingredients','cuisine'],axis=1)
 y=train_data['cuisine']
gbdt_param = {
'objective': 'binary',
'boosting': 'gbdt',
'learning_rate': 0.01,
'num_leaves': 15,
'tree_learner': 'serial',
'num_threads': 8,
'seed': 42,
'max_depth': -1,
'min_data_in_leaf': 50,
'min_sum_hessian_in_leaf': 10,
'bagging_fraction': 0.6,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 1.,
'bagging_seed': 42,
'verbosity ': 1,
'boost_from_average': False,
'metric': 'auc',
}
X = data.drop('target', 1 ).values
y = data['target'].values
print('Original Dataset', Counter(y))
nm = RandomOverSampler()
X, y = nm.fit_resample(X, y)
 scores_f1 = []
scores_roc = []
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
 for train_ix, test_ix in kfold.split(X, y):
train_X, test_X = X[train_ix], X[test_ix]
train_y, test_y = y[train_ix], y[test_ix]
evals_result = {}
trn_data = lgb.Dataset(train_X, label=train_y)
 val_data = lgb.Dataset(test_X, label=test_y)
 lgb_clf = lgb.train(gbdt_param, trn_data, 10000,
valid_sets=[trn_data, val_data], early_stopping_rounds=5000,
verbose_eval=1000, evals_result=evals_result)
<init_hyperparams>
X_train,X_val,y_train,y_val=train_test_split(X,y )
gbdt_param = {
'objective': 'binary',
'boosting': 'gbdt',
'learning_rate': 0.01,
'num_leaves': 15,
'tree_learner': 'serial',
'num_threads': 8,
'seed': 42,
'max_depth': -1,
'min_data_in_leaf': 50,
'min_sum_hessian_in_leaf': 10,
'bagging_fraction': 0.6,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 1.,
'bagging_seed': 42,
'verbosity ': 1,
'boost_from_average': False,
'metric': 'auc',
}
X = data.drop('target', 1 ).values
y = data['target'].values
nm = RandomOverSampler()
X, y = nm.fit_resample(X, y)
 trn_data = lgb.Dataset(X, label=y)
 lgb_clf = lgb.train(gbdt_param, trn_data, 100000)
<save_to_csv>
lr=LogisticRegression()
lr.fit(X_train,y_train)
 lr.score(X_val,y_val )
submission = test.copy()
submission['target'] = lgb_clf.predict(submission.drop('ID_code', 1 ).values)
 submission = submission [['ID_code', 'target']]
submission.to_csv('submission.csv', index=False )<import_modules>
test_data['cuisine']=lr.predict(test_data.drop(['id','ingredients'],axis=1))
from transformers import BertTokenizer, BertModel
from transformers import XLNetModel, XLNetTokenizer
from sklearn import preprocessing
from sklearn.model_selection import GroupKFold
import re
import pandas as pd<load_from_csv>
Submission=test_data[['id','cuisine']]
Submission.set_index('id',inplace=True )
sample_submission = pd.read_csv(".. /input/google-quest-challenge/sample_submission.csv")
 test = pd.read_csv(".. /input/google-quest-challenge/test.csv")
 train = pd.read_csv(".. /input/google-quest-challenge/train.csv" )<load_from_csv>
Submission.to_csv('Submission.csv' )