Kaggle competition: What's Cooking? (Kernels Only)
for f in train_all_fake.columns[0:200]:
train_all_fake[f+'duplicate_value'] = train_all_fake[f]*train_all_fake[f+'duplicate']<split>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import catboost
import tensorflow as tf
train_features_fake = train_all_fake.iloc[:len(train_target)]
fake_data = train_all_fake.iloc[len(train_target):len(train_all_fake)]<set_options>
device_lib.list_local_devices()
del train_all_fake
gc.collect()<feature_engineering>
tf.test.is_gpu_available(
 cuda_only=False,
min_cuda_compute_capability=None
)
for f in train_features_real.columns[0:200]:
train_features_real[f+'distance_of_mean'] = train_features_real[f]-train_features_real[f].mean()
real_data[f+'distance_of_mean'] = real_data[f]-real_data[f].mean()
train_features_real[f+'distance_of_mean'] = train_features_real[f+'distance_of_mean']*train_features_real[f+'duplicate']
real_data[f+'distance_of_mean'] = real_data[f+'distance_of_mean']*real_data[f+'duplicate'] 
<set_options>
df=pd.read_json('.. /input/whats-cooking-kernels-only/train.json', orient='records', dtype={"id":int, "cuisine":str,"ingredients":list})
 df_test=pd.read_json('.. /input/whats-cooking-kernels-only/test.json', orient='records', dtype={"id":int, "ingredients":list} )
del train_features
del test_features
gc.collect()<define_variables>
df['cuisine'].unique() , len(df['cuisine'].unique() )
n_splits = 10<init_hyperparams>
lists = [df['ingredients'].values[i] for i in range(len(df)) ]
unique_ingredients = list(set(list(np.concatenate(lists))))
d = dict(zip(np.arange(len(unique_ingredients)) , unique_ingredients))
inv_d = dict(zip(unique_ingredients, np.arange(len(unique_ingredients))))
param = {
'bagging_freq': 5,
'bagging_fraction': 0.33,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.0085,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 12,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<prepare_x_and_y>
sorted(inv_d.items() )
oof = np.zeros(len(train_features_real))
predictions = np.zeros(len(test_df))
for i,(train_idx, valid_idx)in enumerate(splits):
print(f'Fold {i + 1}')
 x_train = np.array(train_features_fake)
 y_train = np.array(train_target)
 trn_data = lgb.Dataset(x_train[train_idx.astype(int)], label=y_train[train_idx.astype(int)])
 val_data = lgb.Dataset(x_train[valid_idx.astype(int)], label=y_train[valid_idx.astype(int)])
 num_round = 100000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 1000)
 oof[valid_idx] = clf.predict(x_train[valid_idx], num_iteration=clf.best_iteration)
 predictions[fake_data.index] += clf.predict(fake_data, num_iteration=clf.best_iteration)/ n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(train_target, oof)) )<prepare_x_and_y>
dict(sorted(inv_d.items() , key=lambda item: item[1], reverse=True))
oof = np.zeros(len(train_features_real))
feature_importance_df = pd.DataFrame()
features = [c for c in train_features_real.columns if c not in ['ID_code', 'target']]
for i,(train_idx, valid_idx)in enumerate(splits):
print(f'Fold {i + 1}')
 x_train = np.array(train_features_real)
 y_train = np.array(train_target)
 trn_data = lgb.Dataset(x_train[train_idx.astype(int)], label=y_train[train_idx.astype(int)])
 val_data = lgb.Dataset(x_train[valid_idx.astype(int)], label=y_train[valid_idx.astype(int)])
 num_round = 100000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 1000)
 oof[valid_idx] = clf.predict(x_train[valid_idx], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = i + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions[real_data.index] += clf.predict(real_data, num_iteration=clf.best_iteration)/ n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(train_target, oof)) )<compute_test_metric>
import string
from nltk.corpus import stopwords
import re
print('LightBGM auc = {:<8.5f}'.format(roc_auc_score(train_target, oof)))
<define_variables>
chars = re.escape(string.punctuation)
 chars
id_code_test = test_df['ID_code']
id_code_train = train_df['ID_code']<create_dataframe>
clean_ingredients_list = [re.sub(r'['+chars+']', '',
re.sub('[0-9]+','', c.replace("oz","")) ).strip(' ' ).lower()
for c in total_ingredients_list]
my_submission_lbgm = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions})
 my_submission_train = pd.DataFrame({"ID_code" : id_code_train, "target" : oof})
<save_to_csv>
len(np.unique(total_ingredients_list))
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)
<load_from_csv>
len(np.unique(clean_ingredients_list))
train_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/train.csv')
 test_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv' )<load_from_csv>
stop_words = set(stopwords.words('english'))
stop_words
magic_index = pd.read_csv('.. /input/magicdddd/78734.csv',header = None )<define_variables>
tq = tqdm_notebook(total=len(clean_ingredients_list))
for i, ingredients in enumerate(clean_ingredients_list):
cleaned_ingredients = [c for c in ingredients.split(' ')if c not in stop_words]
cleaned_ingredients =(' '.join(cleaned_ingredients)).strip(' ')
 clean_ingredients_list[i] = cleaned_ingredients
tq.update(1)
 tq.close()
clean_unique_ingredients_list = [c for c in list(np.unique(clean_ingredients_list)) if len(c)>0]
magic_index_list = list(magic_index[0] )<define_variables>
len(clean_unique_ingredients_list )
magic_valid = list(set(train_df.index)-set(magic_index_list))<drop_column>
ingredients_encoded_test = np.zeros(( df_test.shape[0], len(clean_unique_ingredients_list)+2), dtype=np.uint8)
 tq = tqdm_notebook(total=df_test.shape[0])
 for i in range(df_test.shape[0]):
ingredients_encoded_test[i,0] = df_test['id'].values[i]
ingredients_encoded_test[i,1] = 0
clean_recipe = [re.sub(r'['+chars+']', '',
re.sub('[0-9]+','', c.replace("oz","")) ).strip(' ' ).lower()
for c in df_test['ingredients'][i]]
for k, ingredients in enumerate(clean_recipe):
cleaned_ingredients = [c for c in ingredients.split(' ')if c not in stop_words]
cleaned_ingredients =(' '.join(cleaned_ingredients)).strip(' ')
 clean_recipe[k] = cleaned_ingredients
clean_recipe = [c for c in clean_recipe if len(c)>0]
for ingredient in clean_recipe:
if ingredient not in clean_unique_ingredients_list:
continue
ingredients_encoded_test[i,clean_unique_ingredients_list.index(ingredient)+2]= 1
tq.update(1)
 tq.close()
train_features = train_df.drop(['target','ID_code'], axis = 1)
 test_features = test_df.drop(['ID_code'],axis = 1)
 train_target = train_df['target']<define_variables>
data_test = pd.DataFrame(data=ingredients_encoded_test[:,1:])
 data_test
large_duplicate_features=['var_5','var_10','var_11','var_17','var_18','var_19','var_20','var_21','var_26','var_30','var_40','var_41','var_44','var_45','var_47','var_48','var_49','var_51','var_54','var_55','var_61','var_67','var_70','var_73','var_74','var_75','var_76','var_80','var_82','var_83','var_84','var_86','var_87','var_90','var_96','var_97','var_100','var_102','var_107','var_117','var_118','var_120','var_123','var_134','var_135','var_136','var_137','var_139','var_141','var_142','var_147','var_149','var_155','var_157','var_158','var_160','var_167','var_171','var_172','var_173','var_174','var_176','var_178','var_182','var_184','var_187','var_199']<define_variables>
data = pd.DataFrame(data=ingredients_encoded[:,1:])
 data
less_duplicate_features =['var_4']<data_type_conversions>
data_gb = data.groupby([0] ).sum()
train_df_copy = train_df.copy()
test_df_copy = test_df.copy()
for col in train_df.columns[2:202]:
duplicate = list(set(list(train_features[col].value_counts().index)) & set(list(test_features[col].value_counts().index)))
 train_df_copy[col+'train_test_have'] = train_df_copy[col].isin(duplicate ).astype(int)
 test_df_copy[col+'train_test_have'] = test_df_copy[col].isin(duplicate ).astype(int )<feature_engineering>
data_gb.sum(0)[data_gb.sum(0)<2]
for f in train_df.columns[2:202]:
train_df_copy[f+'duplicate_value'] = round(train_df_copy[f],4)*train_df_copy[f+'train_test_have']
test_df_copy[f+'duplicate_value'] = round(test_df_copy[f],4)*test_df_copy[f+'train_test_have']<drop_column>
len(data_test.sum(0)[data_test.sum(0)<2] )
train_features = train_df_copy.drop(['target','ID_code'], axis = 1)
 test_features = test_df_copy.drop(['ID_code'],axis = 1)
 train_target = train_df_copy['target']<concatenate>
data = data.loc[:,data.sum(0)>1]
train_all = pd.concat([train_features,test_features], axis = 0 )<data_type_conversions>
from sklearn.model_selection import train_test_split
for f in train_all.columns[0:200]:
train_all[f+'duplicate'] =(( train_all.duplicated(f,False ).astype(int)) -1)*(-1)
<data_type_conversions>
from sklearn.model_selection import train_test_split
<drop_column>
y = data[data.columns[0]]
X = data[data.columns[1:]]
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle = True, stratify = y, test_size=0.2 )
<split>
def prtime(start, stop):
temp = stop - start
hours = temp//3600
temp = temp - 3600*hours
minutes = temp//60
seconds = temp - 60*minutes
print('%d:%d:%d' %(hours,minutes,seconds))
train_features = train_all.iloc[:len(train_target)]
test_features = train_all.iloc[len(train_target):len(train_all)]<set_options>
start = timeit.default_timer()
rf_best = RandomForestClassifier(bootstrap=True,
class_weight='balanced',
criterion='gini',
max_depth=100,
max_features='auto',
max_leaf_nodes=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=1,
min_samples_split=2,
min_weight_fraction_leaf=0.0,
n_estimators=400,
n_jobs=8,
oob_score=False,
random_state=None, verbose=0, warm_start=False)
 stop = timeit.default_timer()
prtime(start, stop )
gc.collect()<define_variables>
from sklearn.metrics import accuracy_score
train_valid = list(set(train_features.index)-set(magic_valid))<set_options>
start = timeit.default_timer()
clf_best = MLPClassifier(solver='adam',
activation='relu',
early_stopping=True,
random_state=3,
max_iter=500,
verbose=True,
alpha=1e-08,
beta_1=0.05,
beta_2=0.4,
epsilon=1e-09)
 stop = timeit.default_timer()
prtime(start, stop )
gc.collect()<prepare_x_and_y>
from sklearn.metrics import accuracy_score
train_features_array = np.array(train_features)
 train_target_array = np.array(train_target)
 x_train = train_features_array[np.array(train_valid)]
y_train = train_target_array[np.array(train_valid)]
x_valid = train_features_array[np.array(magic_valid)]
y_valid = train_target_array[np.array(magic_valid)]<split>
start = timeit.default_timer()
clf_best = LogisticRegression(penalty='l2',
fit_intercept = True,
class_weight = 'balanced',
solver = 'newton-cg',
multi_class = 'multinomial' 
)
 clf_best.fit(X, y)
 stop = timeit.default_timer()
prtime(start, stop )
<set_options>
y_pred = clf_best.predict(X_test)
 accuracy_score(y_test,y_pred )
del train_features_array
del train_target_array
gc.collect()<drop_column>
X_sub = data_test[data_test.columns[1:]]
X_sub = X_sub.loc[:, X.columns]
del train_df_copy
del test_df_copy
del train_all
gc.collect()<train_model>
y_sub = clf_best.predict(X_sub)
 y_pred_str = [list(classes_dict.keys())[list(classes_dict.values() ).index(c)] for c in y_sub]
oof_cb = np.zeros(len(train_features))
predictions_cb = np.zeros(len(test_df))
cat_params = {
'learning_rate':0.01,
'max_depth':3,
'eval_metric': 'AUC',
'bootstrap_type': 'Bernoulli',
'objective': 'Logloss',
'od_type': 'Iter',
'l2_leaf_reg': 13,
'random_seed': 2500,
'allow_writing_files': False}
num_round = 50000
clf = CatBoostClassifier(num_round, task_type='GPU', early_stopping_rounds=3000,**cat_params)
 clf.fit(x_train, y_train, eval_set=(x_valid, y_valid), cat_features=[], use_best_model=True, verbose=500)
 predictions_cb = clf.predict_proba(test_features)[:,1]
valid_cb = clf.predict_proba(x_valid)[:,1]
oof_cb = clf.predict_proba(x_train)[:,1]
print("CV score: {:<8.5f}".format(roc_auc_score(y_valid, valid_cb)))
 gc.collect()<create_dataframe>
subm = pd.DataFrame({'id':df_test['id'], 'cuisine':y_pred_str} )
feature_importance_df = pd.DataFrame()
features = [c for c in train_features.columns if c not in ['ID_code', 'target']]
param = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'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': 11,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
trn_data = lgb.Dataset(x_train, label=y_train)
 val_data = lgb.Dataset(x_valid, label=y_valid)
 oof = np.zeros(len(x_train))
predictions = np.zeros(len(test_features))
num_round = 100000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 3000)
 oof = clf.predict(x_train, num_iteration=clf.best_iteration)
 valid = clf.predict(x_valid, num_iteration=clf.best_iteration)
 predictions = clf.predict(test_features, num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print("CV score: {:<8.5f}".format(roc_auc_score(y_valid, valid)))
 gc.collect()<load_from_csv>
len(np.unique(subm['cuisine']))
<define_variables><EOS> 
subm.to_csv('submission.csv', index=False )