application_vnd.jupyter.stderr_output_27.png application_vnd.jupyter.stderr_output_35.png application_vnd.jupyter.stderr_output_9.png text_plain_output_30.png application_vnd.jupyter.stderr_output_7.png application_vnd.jupyter.stderr_output_11.png text_plain_output_40.png text_plain_output_20.png application_vnd.jupyter.stderr_output_31.png text_plain_output_4.png application_vnd.jupyter.stderr_output_33.png application_vnd.jupyter.stderr_output_25.png text_plain_output_14.png text_plain_output_32.png text_plain_output_10.png text_plain_output_6.png text_plain_output_24.png application_vnd.jupyter.stderr_output_23.png text_plain_output_18.png text_plain_output_36.png application_vnd.jupyter.stderr_output_19.png application_vnd.jupyter.stderr_output_13.png application_vnd.jupyter.stderr_output_3.png text_plain_output_22.png application_vnd.jupyter.stderr_output_5.png text_plain_output_38.png text_plain_output_16.png application_vnd.jupyter.stderr_output_15.png text_plain_output_8.png application_vnd.jupyter.stderr_output_17.png text_plain_output_26.png text_plain_output_34.png application_vnd.jupyter.stderr_output_41.png text_plain_output_28.png text_plain_output_2.png application_vnd.jupyter.stderr_output_29.png application_vnd.jupyter.stderr_output_1.png text_plain_output_12.png application_vnd.jupyter.stderr_output_39.png application_vnd.jupyter.stderr_output_21.png application_vnd.jupyter.stderr_output_37.png
from optuna.integration import LightGBMPruningCallback

from sklearn.impute import SimpleImputer

from sklearn.metrics import log_loss, mean_squared_error

from sklearn.model_selection import KFold ,StratifiedKFold,cross_validate,train_test_split

import lightgbm as lgbm

import numpy as np

import optuna

import pandas as pd

train = pd.read_csv('../input/tabular-playground-series-sep-2021/train.csv')
test = pd.read_csv('../input/tabular-playground-series-sep-2021/test.csv')
sub = pd.read_csv('../input/tabular-playground-series-sep-2021/sample_solution.csv')

X = train.drop(['id', 'claim'], axis=1)
X_test = test.drop('id', axis=1)
y = train['claim']

imputer = SimpleImputer(missing_values=np.nan, strategy='median')
X = imputer.fit_transform(X)
X_test = imputer.transform(X_test)

from optuna.integration import LightGBMPruningCallback
def objective(trial, X, y):
    param_grid = {'device_type': trial.suggest_categorical('device_type', ['gpu']), 'n_estimators': trial.suggest_categorical('n_estimators', [10000]), 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3), 'num_leaves': trial.suggest_int('num_leaves', 20, 3000, step=20), 'max_depth': trial.suggest_int('max_depth', 3, 12), 'min_data_in_leaf': trial.suggest_int('min_data_in_leaf', 200, 10000, step=100), 'lambda_l1': trial.suggest_int('lambda_l1', 0, 100, step=5), 'lambda_l2': trial.suggest_int('lambda_l2', 0, 100, step=5), 'min_gain_to_split': trial.suggest_float('min_gain_to_split', 0, 15), 'bagging_fraction': trial.suggest_float('bagging_fraction', 0.2, 0.95, step=0.1), 'bagging_freq': trial.suggest_categorical('bagging_freq', [1]), 'feature_fraction': trial.suggest_float('feature_fraction', 0.2, 0.95, step=0.1)}
    cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=1121218)
    cv_scores = np.empty(5)
    for idx, (train_idx, test_idx) in enumerate(cv.split(X, y)):
        X_train, X_test = (X[train_idx], X[test_idx])
        y_train, y_test = (y[train_idx], y[test_idx])
        model = lgbm.LGBMClassifier(objective='binary', **param_grid)
        model.fit(X_train, y_train, eval_set=[(X_test, y_test)], eval_metric='binary_logloss', early_stopping_rounds=100, callbacks=[LightGBMPruningCallback(trial, 'binary_logloss')], verbose=2)
        preds = model.predict_proba(X_test)
        cv_scores[idx] = log_loss(y_test, preds)
    return np.mean(cv_scores)

study = optuna.create_study(direction='minimize', study_name='LGBM Classifier')
func = lambda trial: objective(trial, X, y)
study.optimize(func, n_trials=20)