Kaggle competition: Categorical Feature Encoding Challenge II
reg_ridge = Ridge(solver='sag', alpha=5)
 params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 0,
'num_leaves': 31
}
reg_lgbm = lgb.LGBMRegressor(**params )<create_dataframe>
train = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv')
 test = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')
 test.insert(1, 'target', 0 )
start = time.time()
train_preds, test_preds, sub_preds = stack_predictions(X_train, y_train, X_test,
sparse_matrix_sub, 5,
reg_ridge, reg_lgbm)
 print_time(start )<compute_test_metric>
features = [_f for _f in train if _f not in ['id', 'target']]
def factor_encoding(train, test):
assert sorted(train.columns)== sorted(test.columns)
 full = pd.concat([train, test], axis=0, sort=False)
 for f in full:
full[f], _ = pd.factorize(full[f])
 full[f] += 1
return full.iloc[:train.shape[0]], full.iloc[train.shape[0]:]
train_f, test_f = factor_encoding(train[features], test[features] )
X_train_stacked = sparse.csr_matrix(sparse.hstack([X_train, sparse.csr_matrix(train_preds)]))
X_test_stacked = sparse.csr_matrix(sparse.hstack([X_test, sparse.csr_matrix(test_preds)]))
sub_stacked = sparse.csr_matrix(sparse.hstack([sparse_matrix_sub, sparse.csr_matrix(sub_preds)]))<train_model>
class LibFFMEncoder(object):
def __init__(self):
self.encoder = 1
self.encoding = {}
def encode_for_libffm(self, row):
txt = f"{row[0]}"
for i, r in enumerate(row[1:]):
try:
txt += f' {i+1}:{self.encoding[(i, r)]}:1'
except KeyError:
self.encoding[(i, r)] = self.encoder
self.encoder += 1
txt += f' {i+1}:{self.encoding[(i, r)]}:1'
return txt
fold_ids = [
[trn_, val_] for(trn_, val_)in KFold(5,True,1 ).split(train)
 ]
for fold_,(trn_, val_)in enumerate(fold_ids):
encoder = LibFFMEncoder()
libffm_format_trn = pd.concat([train['target'].iloc[trn_], train_f.iloc[trn_]], axis=1 ).apply(
 lambda row: encoder.encode_for_libffm(row), raw=True, axis=1 
)
 libffm_format_val = pd.concat([train['target'].iloc[val_], train_f.iloc[val_]], axis=1 ).apply(
 lambda row: encoder.encode_for_libffm(row), raw=True, axis=1 
)
 print(train['target'].iloc[trn_].shape, train['target'].iloc[val_].shape, libffm_format_val.shape)
 libffm_format_trn.to_csv(f'libffm_trn_fold_{fold_+1}.txt', index=False, header=False)
 libffm_format_val.to_csv(f'libffm_val_fold_{fold_+1}.txt', index=False, header=False)
 encoder = LibFFMEncoder()
libffm_format_trn = pd.concat([train['target'], train_f], axis=1 ).apply(
 lambda row: encoder.encode_for_libffm(row), raw=True, axis=1 
)
 libffm_format_tst = pd.concat([test['target'], test_f], axis=1 ).apply(
 lambda row: encoder.encode_for_libffm(row), raw=True, axis=1 
)
 libffm_format_trn.to_csv(f'libffm_trn.txt', index=False, header=False)
 libffm_format_tst.to_csv(f'libffm_tst.txt', index=False, header=False )
start = time.time()
reg_ridge = Ridge(solver='sag', alpha=5)
 reg_ridge.fit(X_train_stacked, y_train)
 y_pred = reg_ridge.predict(X_test_stacked)
 print(rmsle(10 ** y_test - 1, 10 ** y_pred - 1))
print_time(start )<train_model>
!./ffm-train -p libffm_val_fold_1.txt -r 0.05 -l 0.00001 -k 50 -t 7 libffm_trn_fold_1.txt libffm_fold_1_model
!./ffm-predict libffm_val_fold_1.txt libffm_fold_1_model val_preds_fold_1.txt 
(
 log_loss(train['target'].iloc[fold_ids[0][1]], pd.read_csv('val_preds_fold_1.txt', header=None ).values[:,0]),
roc_auc_score(train['target'].iloc[fold_ids[0][1]], pd.read_csv('val_preds_fold_1.txt', header=None ).values[:,0])
 )
start = time.time()
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 0,
'num_leaves': 31,
'n_estimators': 1000,
'learning_rate': 0.5,
'max_depth': 10,
}
reg_lgbm = lgb.LGBMRegressor(**params, n_jobs=4)
 reg_lgbm.fit(X_train_stacked, y_train)
 r_pred = reg_lgbm.predict(X_test_stacked)
 print(rmsle(10 ** y_test - 1,10 ** r_pred - 1))
print_time(start )<save_to_csv>
!./ffm-train -p libffm_val_fold_2.txt -r 0.05 -l 0.00001 -k 50 -t 7 libffm_trn_fold_2.txt libffm_fold_2_model
!./ffm-predict libffm_val_fold_2.txt libffm_fold_2_model val_preds_fold_2.txt 
(
 log_loss(train['target'].iloc[fold_ids[1][1]], pd.read_csv('val_preds_fold_2.txt', header=None ).values[:,0]),
roc_auc_score(train['target'].iloc[fold_ids[1][1]], pd.read_csv('val_preds_fold_2.txt', header=None ).values[:,0])
 )
pred_sub = reg_lgbm.predict(sub_stacked)
 lightgbm_submission = submission.copy()
lightgbm_submission['price'] = pd.DataFrame(10 ** pred_sub - 1)
 lightgbm_submission.to_csv('stacked_submission_1.csv', index=False )<import_modules>
!./ffm-train -p libffm_val_fold_3.txt -r 0.05 -l 0.00001 -k 50 -t 7 libffm_trn_fold_3.txt libffm_fold_3_model
!./ffm-predict libffm_val_fold_3.txt libffm_fold_3_model val_preds_fold_3.txt 
(
 log_loss(train['target'].iloc[fold_ids[2][1]], pd.read_csv('val_preds_fold_3.txt', header=None ).values[:,0]),
roc_auc_score(train['target'].iloc[fold_ids[2][1]], pd.read_csv('val_preds_fold_3.txt', header=None ).values[:,0])
 )
<import_modules>
!./ffm-train -p libffm_val_fold_4.txt -r 0.05 -l 0.00001 -k 50 -t 7 libffm_trn_fold_4.txt libffm_fold_4_model
!./ffm-predict libffm_val_fold_4.txt libffm_fold_4_model val_preds_fold_4.txt 
(
 log_loss(train['target'].iloc[fold_ids[3][1]], pd.read_csv('val_preds_fold_4.txt', header=None ).values[:,0]),
roc_auc_score(train['target'].iloc[fold_ids[3][1]], pd.read_csv('val_preds_fold_4.txt', header=None ).values[:,0])
 )
print('Finished importing.... ')
 modeling_start_time = time.time()
print('******Modeling start time******:' , 
(time.strftime("%Hhrs:%Mm:%Ss", time.gmtime(( modeling_start_time)))))
 print('Loading data.... ')
 print('Setting opition pd.set_option display.max_colwidth to avoid truncation of columns.... ')
 pd.set_option('display.max_colwidth', -1)
 origtrain = pd.read_table('.. /input/train.tsv',
converters = {'brand_name': np.str ,
'category_name': np.str,
'item_condition_id': np.uint8,
'item_description': np.str,
'name': np.str,
'price': np.float64,
'shipping': np.uint8,
'train_id' : np.uint64
})
 origtest = pd.read_table('.. /input/test.tsv',
converters = {'brand_name': np.str ,
'category_name': np.str,
'item_condition_id': np.uint8,
'item_description': np.str,
'name': np.str,
'price': np.float64,
'shipping': np.uint8,
'train_id' : np.uint64
})
 print(" The number of rows in train", origtrain.shape)
 print(" The number of rows in test", origtest.shape)
 print(" Number of rows with price zero : " ,origtrain.price[origtrain.price == 0.0].count())
 print(" Number of rows : ", origtrain.shape[0])
 origtrain = origtrain[origtrain.price > 0.0]
nrow_train = origtrain.shape[0]
print(" Number of rows aferdrop : ", nrow_train)
 test_id = origtest.test_id.values
combdata = pd.concat([origtrain , origtest],0)
 combdata = combdata.reindex()
stop_words = set(stopwords.words('english'))
print('Finished loading data.... ')
 print
del origtrain
del origtest
gc.collect() 
<data_type_conversions>
!./ffm-train -p libffm_val_fold_5.txt -r 0.05 -l 0.00001 -k 50 -t 7 libffm_trn_fold_5.txt libffm_fold_5_model
!./ffm-predict libffm_val_fold_5.txt libffm_fold_5_model val_preds_fold_5.txt 
(
 log_loss(train['target'].iloc[fold_ids[4][1]], pd.read_csv('val_preds_fold_5.txt', header=None ).values[:,0]),
roc_auc_score(train['target'].iloc[fold_ids[4][1]], pd.read_csv('val_preds_fold_5.txt', header=None ).values[:,0])
 )
start_time = time.time()
print('Starting pre-processing of text columns at start time is:' , 
(time.strftime("%Hhrs:%Mm:%Ss", time.gmtime(( start_time)))))
 print(' pre-processing brand name...')
 combdata.brand_name = combdata.brand_name.apply(removeSpecial)
 combdata.loc[combdata['brand_name'].values == '', 'brand_name'] = '0000aaaa'
combdata['newBrand'] = combdata['brand_name'].str.strip()
print(' pre-processing price...')
 combdata.price = combdata.price.fillna(0)
 combdata.price = combdata.price.astype('float16')
 print(' pre-processing shipping...')
 combdata.shipping = combdata.shipping.astype('uint8')
 print(' pre-processing item condition...')
 combdata.item_condition_id = combdata.item_condition_id.astype('uint8')
 print(' pre-processing category name...')
 combdata.loc[combdata['category_name'].values == '', 'category_name'] = '0000aaaa'
combdata['newCategory'] = combdata.category_name.apply(removeSpecial)
 print(' pre-processing test_id...')
 combdata.test_id = combdata.test_id.fillna(9999999)
 combdata.test_id = combdata.test_id.astype('uint32')
 print(' pre-processing train id...')
 combdata.train_id = combdata.train_id.fillna(9999999)
 combdata.train_id = combdata.train_id.astype('uint32')
 print(' filling the null with 0000aaaa...')
 combdata.loc[combdata['name'].values == '', 'name'] = '0000aaaa'
combdata.loc[combdata['item_description'].values == '', 'item_description'] = '0000aaaa'
print(' creating new columns newName and description...')
 combdata['newName'] = combdata.name.values
combdata['description'] = combdata.item_description.values
print(' removing special character from newName...')
 combdata['newName'] = combdata.newName.apply(removeSpecial)
 print(' removing special character from description...')
 combdata['description'] = combdata.description.apply(removeSpecial)
 end_time = end_time = time.time()
print('Completed pre-processing of text columns and time taken:', 
(time.strftime("%Hhrs:%Mm:%Ss",(time.gmtime(( end_time - start_time))))))
gc.collect() 
<categorify>
oof_preds = np.zeros(train.shape[0])
 for fold_,(_, val_)in enumerate(fold_ids):
oof_preds[val_] = pd.read_csv(f'val_preds_fold_{fold_+1}.txt', header=None ).values[:, 0]
oof_score = roc_auc_score(train['target'], oof_preds)
 print(oof_score )
<choose_model_class><EOS> 
submission = test[['id']].copy()
submission['target'] = pd.read_csv('tst_preds.txt', header=None ).values[:,0]
submission.to_csv('libffm_prediction.csv', index=False )