text_plain_output_1.png
from sklearn.model_selection import train_test_split

import lightgbm as lgb

import numpy as np # linear algebra

import optuna

import optuna

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import sklearn

train = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv', nrows=30000)

features = [col for col in list(train.columns) if 'feature' in col]
train = train[train['weight'] != 0]
train['action'] = (train['resp'].values > 0).astype(int)
f_mean = train.mean()
train = train.fillna(f_mean)
X = train.loc[:, features]
y = train.loc[:, 'action']
del train
X = np.array(X)
y = np.array(y)

import optuna
def objective(trial):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
    dtrain = lgb.Dataset(X_train, label=y_train)
    param = {'objective': 'binary', 'metric': 'binary_logloss', 'verbosity': -1, 'boosting_type': 'gbdt', 'lambda_l1': trial.suggest_float('lambda_l1', 1e-08, 10.0, log=True), 'lambda_l2': trial.suggest_float('lambda_l2', 1e-08, 10.0, log=True), 'num_leaves': trial.suggest_int('num_leaves', 2, 256), 'feature_fraction': trial.suggest_float('feature_fraction', 0.4, 1.0), 'bagging_fraction': trial.suggest_float('bagging_fraction', 0.4, 1.0), 'bagging_freq': trial.suggest_int('bagging_freq', 1, 7), 'min_child_samples': trial.suggest_int('min_child_samples', 5, 100)}
    gbm = lgb.train(param, dtrain)
    preds = gbm.predict(X_test)
    pred_labels = np.rint(preds)
    accuracy = sklearn.metrics.accuracy_score(y_test, pred_labels)
    return accuracy

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100)