text_html_output_1.png
from functools import partial

from optuna.samplers import TPESampler

from sklearn.decomposition import PCA

from sklearn.metrics import accuracy_score,roc_auc_score

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.preprocessing import StandardScaler

from xgboost import XGBClassifier

import optuna

import os

import pandas as pd

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import optuna
from optuna.samplers import TPESampler
import catboost
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, roc_auc_score
from functools import partial
optuna.logging.set_verbosity(optuna.logging.WARNING)
import os

Xtrain = pd.read_csv('../input/sept-2021-filled/train_new.csv')
test = pd.read_csv('../input/sept-2021-filled/test_new.csv')

(Xtrain.shape, test.shape)

y = Xtrain.claim
Xtrain = Xtrain.drop(['id', 'claim'], axis=1)

test_id = test.id
test = test.drop('id', axis=1)

ss = StandardScaler()
ss.fit(Xtrain)
Xtrain = ss.transform(Xtrain)
test = ss.transform(test)

pca = PCA(0.95)
pca.fit(Xtrain)
Xtrain = pca.transform(Xtrain)
test = pca.transform(test)

(Xtrain.shape, test.shape)

def getXgbHyperparameters(trial):
    xgb_param = {'tree_method': 'gpu_hist', 'eval_metric': 'auc', 'n_estimators': trial.suggest_int('n_estimators', 700, 2000, 100), 'booster': 'gbtree', 'reg_lambda': trial.suggest_int('reg_lambda', 1, 100), 'reg_alpha': trial.suggest_int('reg_alpha', 1, 100), 'subsample': trial.suggest_float('subsample', 0.2, 1.0), 'colsample_bytree': trial.suggest_float('colsample_bytree', 0.2, 1.0), 'max_depth': trial.suggest_int('max_depth', 3, 15), 'min_child_weight': trial.suggest_int('min_child_weight', 2, 18), 'learning_rate': trial.suggest_loguniform('learning_rate', 1e-05, 0.01), 'gamma': trial.suggest_float('gamma', 0, 20)}
    return xgb_param

def optimize(trial, X, y):
    params = getXgbHyperparameters(trial)
    xgb = XGBClassifier(**params, use_label_encoder=False)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=44)
    xgb.fit(X_train, y_train)
    pred = xgb.predict(X_test)
    return -1 * roc_auc_score(pred, y_test)

opt_func = partial(optimize, X=X_train, y=y_train)
func = lambda trial: optimize(trial, Xtrain, y)

def logging_callback(study, frozen_trial):
    previous_best_value = study.user_attrs.get('previous_best_value', None)
    if previous_best_value != study.best_value:
        study.set_user_attr('previous_best_value', study.best_value)

study = optuna.create_study(sampler=TPESampler(seed=69), direction='minimize', study_name='xgb')
study.optimize(func, timeout=1 * 60 * 60, callbacks=[logging_callback])