Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<train_model>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
test = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')
 train = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv' )
print("beginning time is {}".format(datetime.datetime.now()))
display("Running Optimization for {} Trials.".format(MAX_EVALS))
best = fmin(fn = objective, space = space, algo = tpe.suggest, trials = trials,max_evals = MAX_EVALS)
 print("end time is {}".format(datetime.datetime.now()))<find_best_params>
%%time
def random_permutation(x):
perm = np.random.permutation(len(x))
x = x.iloc[perm].reset_index(drop=True)
 return x
train = random_permutation(train)
 test = random_permutation(test)
 train_ids = train.id
test_ids = test.id
train.drop('id', 1, inplace=True)
 test.drop('id', 1, inplace=True)
 train_targets = train.target
train.drop('target', 1, inplace=True )
hyperparameters = results.hyperparameters[0]<init_hyperparams>
%%time
bin_recode = {0: 0, 1: 1, 'F':0, 'T':1, 'N':0, 'Y':1}
for i in range(5):
train[f'bin_{i}'] = train[f'bin_{i}'].map(bin_recode)
 test[f'bin_{i}'] = test[f'bin_{i}'].map(bin_recode)
 levels = { 'Novice':0, 'Contributor':1,
'Expert':2, 'Master':3, 'Grandmaster':4 }
train['ord_1'] = train['ord_1'].map(levels)
 test['ord_1'] = test['ord_1'].map(levels)
 temps = { 'Freezing':0, 'Cold':1, 'Warm':2, 'Hot':3,
'Boiling Hot':4, 'Lava Hot':5 }
train['ord_2'] = train['ord_2'].map(temps)
 test['ord_2'] = test['ord_2'].map(temps)
 lowercase_letters = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,
'g':7,'h':8,'i':9,'j':10,'k':11,
'l':12,'m':13,'n':14,'o':15,'p':16,
'q':17,'r':18,'s':19,'t':20,'u':21,
'v':22,'w':23,'x':24,'y':25,'z':26}
train['ord_3'] = train['ord_3'].map(lowercase_letters)
 test['ord_3'] = test['ord_3'].map(lowercase_letters)
 uppercase_letters = {'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,
'G':7,'H':8,'I':9,'J':10,'K':11,
'L':12,'M':13,'N':14,'O':15,'P':16,
'Q':17,'R':18,'S':19,'T':20,'U':21,
'V':22,'W':23,'X':24,'Y':25,'Z':26}
train['ord_4'] = train['ord_4'].map(uppercase_letters)
 test['ord_4'] = test['ord_4'].map(uppercase_letters )
hyperparameters = {
'boosting_type': 'gbdt',
'colsample_bytree': 0.7812943473676428,
'is_unbalance': True,
'learning_rate': 0.012732207618246335,
'max_bin': 200,
'max_depth': 14,
'min_child_samples': 70,
'min_child_weight': 0.0010242091278688855,
'num_leaves': 10,
'objective': 'binary',
'subsample': 0.8026192939361728,
'subsample_for_bin': 72000,
'subsample_freq': 7,
'n_estimators': 6589}<compute_train_metric>
%%time
noms_0_4 = ['nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4']
train = pd.get_dummies(train,
columns = noms_0_4,
prefix = noms_0_4,
drop_first=True,
sparse=True,
dtype=np.int8)
 test = pd.get_dummies(test,
columns = noms_0_4,
prefix = noms_0_4,
drop_first=True,
sparse=True,
dtype=np.int8 )
val_scores, predictions, gbm_fi= model_gbm(train,train_label,test,test_ids,hyperparameters=hyperparameters )<save_to_csv>
%%time
for i in [5,6,7,8,9]:
cbe = CatBoostEncoder()
train[f'nom_{i}'] = cbe.fit_transform(train[f'nom_{i}'], train_targets)
 test[f'nom_{i}'] = cbe.transform(test[f'nom_{i}'])
 cbe = CatBoostEncoder()
train['ord_5'] = cbe.fit_transform(train['ord_5'], train_targets)
 test['ord_5'] = cbe.transform(test['ord_5'] )
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = predictions
submission.to_csv("submission.csv",index=False )<install_modules>
%%time
cb = CatBoostClassifier(eval_metric='AUC',
learning_rate=0.1,
depth=3,
l2_leaf_reg=5)
 cb.fit(train, train_targets, verbose=False )
<install_modules><EOS> 
preds = cb.predict_proba(test)[:, 1]
preds_df = pd.DataFrame(list(zip(test_ids, preds)) ,
columns = ['id', 'target'])
 preds_df.sort_values(by=['id'], inplace = True)
 preds_df.to_csv("./submission.csv", index=False )