Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<concatenate>
%matplotlib inline
plt.style.use('ggplot')
 warnings.filterwarnings('ignore' )
train_features = pd.concat(( train_features, train_features_cluster,train_cluster_pca,train_features_stats), axis=1)
 test_features = pd.concat(( test_features, test_features_cluster,test_cluster_pca,test_features_stats), axis=1 )<merge>
train = pd.read_csv('.. /input/train.csv', index_col='Id')
 test = pd.read_csv('.. /input/test.csv', index_col='Id' )
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]<drop_column>
test.loc[test['rez_esc'] == 99.0 , 'rez_esc'] = 5
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )<drop_column>
relabel_cols = ['edjefe', 'edjefa', 'dependency']
train[relabel_cols] = train[relabel_cols].replace({'yes':1, 'no':1} ).astype(float)
 test[relabel_cols] = test[relabel_cols].replace({'yes':1, 'no':1} ).astype(float )
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()<filter>
train.loc[train.tipovivi1 == 1, 'v2a1'] = 0
test.loc[test.tipovivi1 == 1, 'v2a1'] = 0
target=target[target_cols]<categorify>
agg_dict = {'escolari':np.sum, 'rez_esc':np.sum, 'age':np.sum, 'estadocivil1':np.sum}
train_by_hh = train.groupby('idhogar' ).agg(agg_dict)
 test_by_hh = test.groupby('idhogar' ).agg(agg_dict)
 train = train.join(train_by_hh, on='idhogar', rsuffix='_hh')
 test = test.join(test_by_hh, on='idhogar', rsuffix='_hh' )
train = pd.get_dummies(train, columns=['cp_time','cp_dose'])
 test_ = pd.get_dummies(test, columns=['cp_time','cp_dose'] )<define_variables>
train['rent_per_cap'] = train['v2a1'] / train['tamhog']
test['rent_per_cap'] = test['v2a1'] / test['tamhog']
train['tab_per_cap'] = train['v18q'] / train['tamhog']
test['tab_per_cap'] = test['v18q'] / test['tamhog']
train['mf_rat'] = train['r4h3'] / train['r4m3']
test['mf_rat'] = test['r4h3'] / test['r4m3']
train['walls_roof_bad'] = train['epared1'] + train['eviv1']
test['walls_roof_bad'] = test['epared1'] + test['eviv1']
train['child_perc'] =(train['r4h1'] + train['r4m1'])/ train['r4t3']
test['child_perc'] =(test['r4h1'] + test['r4m1'])/ test['r4t3']
train['young_perc'] = train['r4t1'] / train['hogar_nin']
test['young_perc'] = test['r4t1'] / test['hogar_nin']
train['child_per_adult'] = train['hogar_nin'] / train['hogar_adul']
test['child_per_adult'] = test['hogar_nin'] / test['hogar_adul']
train['older_perc'] = train['hogar_mayor'] / train['tamviv']
test['older_perc'] = test['hogar_mayor'] / test['tamviv']
train['tamdiff'] = train['tamhog'] - train['tamviv']
test['tamdiff'] = test['tamhog'] - test['tamviv']
train['comp_tv'] = train['computer'] + train['television']
test['comp_tv'] = test['computer'] + test['television']
feature_cols = [c for c in train.columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['sig_id']]<drop_column>
train.fillna(-1, inplace=True)
 test.fillna(-1, inplace=True)
 train.replace([np.inf, -np.inf], -1, inplace=True)
 test.replace([np.inf, -np.inf], -1, inplace=True )
train = train[feature_cols]
test = test_[feature_cols]<prepare_x_and_y>
keep_cols = [col for col in train.columns if col[:3] != 'SQB']
keep_cols = [col for col in keep_cols if col not in ['idhogar', 'agesq', 'Target']]
X = train.loc[:, keep_cols].values
y = train.Target.values
X_test = test.loc[:, keep_cols].values
X_test = test.values<compute_test_metric>
gbm = GradientBoostingClassifier(n_estimators=100 ).fit(X,y )
class LogitsLogLoss(Metric):
def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):
logits = 1 /(1 + np.exp(-y_pred))
aux =(1 - y_true)* np.log(1 - logits + 5e-5)+ y_true * np.log(logits + 5e-5)
 return np.mean(-aux )<choose_model_class>
skf = StratifiedKFold(n_splits=3)
 skf.split(X, y )
MAX_EPOCH = 200
tabnet_params = dict(
 n_d = 32,
n_a = 32,
n_steps = 1,
gamma = 1.3,
lambda_sparse = 0,
optimizer_fn = optim.Adam,
optimizer_params = dict(lr = 2e-2, weight_decay = 1e-5),
mask_type = "entmax",
scheduler_params = dict(mode = "min", patience = 5, min_lr = 1e-5, factor = 0.9),
scheduler_fn = ReduceLROnPlateau,
seed = seed,
verbose = 10
)<find_best_model_class>
logmod = LogisticRegression(class_weight='balanced')
 param_grid = { 'C': [0.5, 1] }
log_grid = GridSearchCV(logmod, cv=skf, param_grid=param_grid, scoring='f1_macro')
 log_grid.fit(X, y);
test_cv_preds = []
NB_SPLITS = 10
mskf = MultilabelStratifiedKFold(n_splits = NB_SPLITS, random_state = 0, shuffle = True)
 SEED = [20,21,22]
for s in SEED:
tabnet_params['seed'] = s
for fold_nb,(train_idx, val_idx)in enumerate(mskf.split(train, target)) :
model = TabNetRegressor()
model.load_model(f".. /input/moa-tabnet-train-inference/TabNet_seed_{s}_fold_{fold_nb+1}.zip")
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)))
 test_preds_all = np.stack(test_cv_preds )<save_to_csv>
print(log_grid.best_params_)
 print(log_grid.best_score_ )
all_feat = [col for col in df.columns if col not in ["sig_id"]]
test = pd.read_csv(".. /input/lish-moa/test_features.csv")
 sig_id = test[test["cp_type"] != "ctl_vehicle"].sig_id.reset_index(drop = True)
 tmp = pd.DataFrame(test_preds_all.mean(axis = 0), columns = all_feat)
 tmp["sig_id"] = sig_id
submission = pd.merge(test[["sig_id"]], tmp, on = "sig_id", how = "left")
 submission.fillna(0, inplace = True)
 submission.to_csv("submission.csv", index = None)
 submission.head()<load_from_csv>
params = {
'learning_rate': [0.01, 0.1],
'max_depth': [3, 4, 5, 6],
'n_estimators': [500, 1000],
'colsample_bytree':[0.5, 1],
}
check=pd.read_csv('.. /input/moa-tabnet-train-inference/submission.csv' )<load_from_csv>
lgb_mod = lgb.LGBMClassifier(class_weight='balanced')
 lgbm_grid = GridSearchCV(estimator=lgb_mod, cv=skf, param_grid=params, scoring='f1_macro', verbose=1, n_jobs=2)
 lgbm_grid.fit(X, y);
subm=pd.read_csv("./submission.csv" )<set_options>
print('Best lightgbm parameters: {}'.format(lgbm_grid.best_params_))
print('Best tuning score: {}'.format(lgbm_grid.best_score_))
warnings.filterwarnings('ignore' )<load_from_csv>
preds_log = log_grid.predict(X_test)
 preds_lgbm = lgbm_grid.predict(X_test )
<install_modules><EOS> 
sub_log = pd.DataFrame({'Id':test.index, 'Target':preds_log})
 sub_lgbm = pd.DataFrame({'Id':test.index, 'Target':preds_lgbm})
 sub_lgbm.to_csv('sub_lgbm1.csv', index=False )