Kaggle competition: Categorical Feature Encoding Challenge
ss = l + colgroups<save_to_csv>
!pip install -U lightautoml
def _get_leak(df, cols,extra_feats, lag=0):
f1 = cols[:(( lag+2)* -1)]
f2 = cols[(lag+2):]
for ef in extra_feats:
f1 += ef[:(( lag+2)* -1)]
f2 += ef[(lag+2):]
d1 = df[f1].apply(tuple, axis=1 ).to_frame().rename(columns={0: 'key'})
 d1.to_csv('extra_d1.csv')
 d2 = df[f2].apply(tuple, axis=1 ).to_frame().rename(columns={0: 'key'})
 d2['pred'] = df[cols[lag]]
d3 = d2[~d2.duplicated(['key'], keep=False)]
d4 = d1[~d1.duplicated(['key'], keep=False)]
d5 = d4.merge(d3, how='inner', on='key')
 d6 = d1.merge(d5, how='left', on='key')
 d6.to_csv('extra_d6.csv')
 return d1.merge(d5, how='left', on='key' ).pred.fillna(0 )<feature_engineering>
from lightautoml.automl.presets.tabular_presets import TabularAutoML, TabularUtilizedAutoML
from lightautoml.dataset.roles import DatetimeRole, CategoryRole
from lightautoml.tasks import Task
from sklearn.metrics import classification_report, roc_auc_score
def compiled_leak_result() :
max_nlags = len(cols)-2
train_leak = train[["ID", "target"] + cols]
train_leak["compiled_leak"] = 0
train_leak["nonzero_mean"] = train[transact_cols].apply(
 lambda x: np.expm1(np.log1p(x[x!=0] ).mean()), axis=1 
)
 scores = []
leaky_value_counts = []
leaky_value_corrects = []
leaky_cols = []
for i in range(max_nlags):
c = "leaked_target_"+str(i)
 print('Processing lag', i)
 train_leak[c] = _get_leak(train, cols,ss, i)
 leaky_cols.append(c)
 train_leak = train.join(
 train_leak.set_index("ID")[leaky_cols+["compiled_leak", "nonzero_mean"]],
on="ID", how="left" 
)[["ID", "target"] + cols + leaky_cols+["compiled_leak", "nonzero_mean"]]
zeroleak = train_leak["compiled_leak"]==0
train_leak.loc[zeroleak, "compiled_leak"] = train_leak.loc[zeroleak, c]
leaky_value_counts.append(sum(train_leak["compiled_leak"] > 0))
_correct_counts = sum(train_leak["compiled_leak"]==train_leak["target"])
 leaky_value_corrects.append(_correct_counts*1.0/leaky_value_counts[-1])
 print("Leak values found in train", leaky_value_counts[-1])
 print(
 "% of correct leaks values in train ",
leaky_value_corrects[-1] 
)
 tmp = train_leak.copy()
tmp.loc[zeroleak, "compiled_leak"] = tmp.loc[zeroleak, "nonzero_mean"]
print('Na count',tmp.compiled_leak.isna().sum())
 scores.append(np.sqrt(mean_squared_error(y, np.log1p(tmp["compiled_leak"] ).fillna(14.49))))
print(
 'Score(filled with nonzero mean)',
scores[-1] 
)
 result = dict(
 score=scores,
leaky_count=leaky_value_counts,
leaky_correct=leaky_value_corrects, 
)
 return train_leak, result<create_dataframe>
train_df = pd.read_csv('.. /input/cat-in-the-dat/train.csv')
 test_df = pd.read_csv('.. /input/cat-in-the-dat/test.csv')
 submission_df = pd.read_csv('.. /input/cat-in-the-dat/sample_submission.csv' )
result = pd.DataFrame.from_dict(result, orient='columns')
 result.T<save_to_csv>
N_THREADS = 4
N_FOLDS = 5
RANDOM_STATE = 42
TEST_SIZE = 0.2
TIMEOUT = 1800
TARGET_NAME = 'target'
np.random.seed(RANDOM_STATE )
result.to_csv('train_leaky_stat.csv', index=False )<compute_test_metric>
def preprocess(df):
df['time'] =(np.datetime64('2018-01-01')+ df['day'].astype(np.dtype('timedelta64[D]')) + df['month'].astype(np.dtype('timedelta64[M]')) ).astype(str)
 return df.drop(columns=['id', 'day', 'month'])
 train = preprocess(train_df)
 test = preprocess(test_df )
best_score = np.min(result['score'])
 best_lag = np.argmin(result['score'])
 print('best_score', best_score, '
best_lag', best_lag )<feature_engineering>
task = Task('binary',)
 roles = {'target': TARGET_NAME,
DatetimeRole(base_date=True, seasonality=('m', 'd', 'wd', 'hour'), base_feats=True): 'time',
CategoryRole(ordinal=False): ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4', 'nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9',],
CategoryRole(ordinal=True): ['ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5',],
}
def rewrite_compiled_leak(leak_df, lag):
leak_df["compiled_leak"] = 0
for i in range(lag):
c = "leaked_target_"+str(i)
 zeroleak = leak_df["compiled_leak"]==0
leak_df.loc[zeroleak, "compiled_leak"] = leak_df.loc[zeroleak, c]
return leak_df<categorify>
automl = TabularUtilizedAutoML(task = task,
verbose=2,
timeout = TIMEOUT,
general_params = {'nested_cv': False, 'use_algos': [['linear_l2', 'lgb', 'lgb_tuned', 'cb', 'cb_tuned']]},
reader_params = {'cv': N_FOLDS, 'random_state': RANDOM_STATE},
tuning_params = {'max_tuning_iter': 20},
lgb_params = {'default_params': {'num_threads': N_THREADS, }},
cb_params = {'default_params': {'thread_count': N_THREADS, }} 
)
 oof_pred = automl.fit_predict(train, roles = roles)
leaky_cols = [c for c in train_leak.columns if 'leaked_target_' in c]
train_leak = rewrite_compiled_leak(train_leak, best_lag)
 train_leak[['ID']+leaky_cols+['compiled_leak']].head()<feature_engineering>
print(roc_auc_score(train[TARGET_NAME].values.ravel() , oof_pred.data.ravel()))
def compiled_leak_result_test(max_nlags):
test_leak = test[["ID", "target"] + cols]
test_leak["compiled_leak"] = 0
test_leak["nonzero_mean"] = test[transact_cols].apply(
 lambda x: np.expm1(np.log1p(x[x!=0] ).mean()), axis=1 
)
 scores = []
leaky_value_counts = []
leaky_cols = []
for i in range(max_nlags):
c = "leaked_target_"+str(i)
 print('Processing lag', i)
 test_leak[c] = _get_leak(test, cols,ss, i)
 leaky_cols.append(c)
 test_leak = test.join(
 test_leak.set_index("ID")[leaky_cols+["compiled_leak", "nonzero_mean"]],
on="ID", how="left" 
)[["ID", "target"] + cols + leaky_cols+["compiled_leak", "nonzero_mean"]]
zeroleak = test_leak["compiled_leak"]==0
test_leak.loc[zeroleak, "compiled_leak"] = test_leak.loc[zeroleak, c]
leaky_value_counts.append(sum(test_leak["compiled_leak"] > 0))
print("Leak values found in test", leaky_value_counts[-1])
 result = dict(
 leaky_count=leaky_value_counts, 
)
 return test_leak, result<categorify>
thres =.5
print(classification_report(train[TARGET_NAME].values.ravel() ,(oof_pred.data.ravel() > thres ).astype(int), digits=6))
test_leak, test_result = compiled_leak_result_test(max_nlags=38)
<create_dataframe>
test_pred = automl.predict(test )
test_result = pd.DataFrame.from_dict(test_result, orient='columns')
 test_result.T<save_to_csv>
submission_df['target'] = test_pred.data.ravel()
submission_df.head()
<choose_model_class><EOS> 
submission_df.to_csv('submission.csv', index=False )