text_plain_output_5.png text_plain_output_4.png text_plain_output_6.png text_plain_output_3.png text_plain_output_2.png text_plain_output_1.png
from lightgbm import LGBMClassifier

from sklearn import metrics

from sklearn import model_selection

from sklearn import preprocessing

import numpy as np

import os

import pandas as pd

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)

class CFG:
    HOME_DIR = '/kaggle/input/icr-identify-age-related-conditions'
    SPLITS = 5
    SEED = 2023
    boosting_type = 'gbdt'
    ITERATION = 1000
    BOOSTING_TYPE = 'dart'
    lgb_params = {'objective': 'binary', 'metric': None, 'boosting': BOOSTING_TYPE, 'learning_rate': 0.005, 'num_leaves': 5, 'feature_fraction': 0.5, 'bagging_fraction': 0.8, 'lambda_l1': 2, 'lambda_l2': 4, 'n_jobs': -1, 'seed': SEED}
    xgb_params = {'objective': 'binary:logistic', 'eval_metric': 'logloss', 'learning_rate': 0.005, 'max_depth': 4, 'colsample_bytree': 0.5, 'subsample': 0.8, 'eta': 0.03, 'gamma': 1.5, 'random_state': SEED}
    cat_params = {'learning_rate': 0.005, 'iterations': ITERATION, 'depth': 4, 'colsample_bylevel': 0.5, 'subsample': 0.8, 'l2_leaf_reg': 3, 'random_seed': SEED, 'auto_class_weights': 'Balanced'}
cfg = CFG()

train_data = pd.read_csv(os.path.join(cfg.HOME_DIR, 'train.csv'))
test_data = pd.read_csv(os.path.join(cfg.HOME_DIR, 'test.csv'))
greeks_data = pd.read_csv(os.path.join(cfg.HOME_DIR, 'greeks.csv'))
sample_data = pd.read_csv(os.path.join(cfg.HOME_DIR, 'sample_submission.csv'))

def rename_column(df):
    df = df.rename(columns={'BD ': 'BD', 'CD ': 'CD', 'CW ': 'CW', 'FD ': 'FD'})
    return df
def scale_features(train_df, test_df):
    scaler = preprocessing.StandardScaler()
    train_df[FEATURES] = scaler.fit_transform(train_df[FEATURES])
    test_df[FEATURES] = scaler.transform(test_df[FEATURES])
    return (train_df, test_df)

ej_mapper = {'A': 1, 'B': 0}
train_data['EJ'] = train_data.EJ.map(ej_mapper)
test_data['EJ'] = test_data.EJ.map(ej_mapper)
train_data = rename_column(train_data)
test_data = rename_column(test_data)

IDENTIFIER = 'Id'
FEATURES = ['AB', 'AF', 'AH', 'AM', 'AR', 'AX', 'AY', 'AZ', 'BC', 'BD', 'BN', 'BP', 'BQ', 'BR', 'BZ', 'CB', 'CC', 'CD', 'CF', 'CH', 'CL', 'CR', 'CS', 'CU', 'CW', 'DA', 'DE', 'DF', 'DH', 'DI', 'DL', 'DN', 'DU', 'DV', 'DY', 'EB', 'EE', 'EG', 'EH', 'EJ', 'EL', 'EP', 'EU', 'FC', 'FD', 'FE', 'FI', 'FL', 'FR', 'FS', 'GB', 'GE', 'GF', 'GH', 'GI', 'GL']
TARGET = 'Class'

skf = model_selection.StratifiedKFold(n_splits=cfg.SPLITS, shuffle=True, random_state=cfg.SEED)
train_data = train_data.sample(frac=1)
train_data['kfold'] = -99
train_data = train_data.reset_index(drop=True)
for fold, (tidx, vidx) in enumerate(skf.split(train_data[FEATURES], train_data[TARGET])):
    train_data.loc[vidx, 'kfold'] = fold

def train_classifier(train_data, test_data, model_, fold):
    df_train = train_data.query('kfold != @fold').reset_index(drop=True)
    df_valid = train_data.query('kfold == @fold').reset_index(drop=True)
    t_id = df_train[IDENTIFIER]
    X_train = df_train[FEATURES]
    y_train = df_train[TARGET]
    v_id = df_valid[IDENTIFIER]
    X_valid = df_valid[FEATURES]
    y_valid = df_valid[TARGET]
    v_test = test_data[IDENTIFIER]
    X_test = test_data[FEATURES]
    model_.fit(X_train, y_train)
    v_pred = model_.predict_proba(X_valid)[:, 1]
    test_pred = model_.predict_proba(X_test)[:, 1]
    score = metrics.log_loss(y_valid, v_pred)
    return (v_id, y_valid, v_pred, np.ones_like(v_pred, dtype='int8') * fold, test_pred)

model_0 = LGBMClassifier(**cfg.lgb_params)
oof_id = []
oof_tar = []
oof_pred = []
oof_fold = []
oof_sub = []
for fold in range(0, 5):
    v_id, y_valid, v_pred, f, test_pred = train_classifier(train_data, test_data, model_0, fold)
    oof_id.append(v_id)
    oof_tar.append(y_valid)
    oof_pred.append(v_pred)
    oof_fold.append(f)
    oof_sub.append(test_pred)
oof = np.concatenate(oof_pred)
true = np.concatenate(oof_tar)
names = np.concatenate(oof_id).reshape(-1)
folds = np.concatenate(oof_fold)
log_loss = metrics.log_loss(true, oof)
print('Overall OOF Log Loss with TTA = %.3f' % log_loss)
df_oof = pd.DataFrame(dict(id=names, Class=true, pred=oof, fold=folds))
df_oof.to_csv('oof_lgb.csv', index=False)
print(df_oof.head())
test_ids = test_data[IDENTIFIER].values.reshape(-1)
test_score = np.mean(np.column_stack(oof_sub), axis=1)
sub_oof = pd.DataFrame(dict(id=test_ids, Class=test_score))
sub_oof.to_csv('sub_lgb.csv', index=False)
print(sub_oof.head())