Skip to content

Instantly share code, notes, and snippets.

View smly's full-sized avatar
👁️‍🗨️

smly smly

👁️‍🗨️
View GitHub Profile
@smly
smly / lightgbm_integratio.patch
Last active July 9, 2023 08:20
Usage of custom eval metric function with Optuna
diff --git a/examples/pruning/lightgbm_integration.py b/examples/pruning/lightgbm_integration.py
index 8e623772..4c0c315c 100644
--- a/examples/pruning/lightgbm_integration.py
+++ b/examples/pruning/lightgbm_integration.py
@@ -21,6 +21,15 @@ import optuna
# FYI: Objective functions can take additional arguments
# (https://optuna.readthedocs.io/en/stable/faq.html#objective-func-additional-args).
def objective(trial):
+ def custom_accuracy_pct(preds, data):
+ y_true = data.get_label()
@smly
smly / dba.py
Created June 2, 2021 09:46
query expansion and database augmentation
import numpy as np
from .functions import l2norm_numpy
def qe_dba(
feats_test, feats_index, sims, topk_idx, alpha=3.0, qe=True, dba=True
):
# alpha-query expansion (DBA)
feats_concat = np.concatenate([feats_test, feats_index], axis=0)
@smly
smly / http_418.md
Created February 24, 2022 08:45
HTTP/1.1 418
$ curl -I https://eng.mil.ru/
HTTP/1.1 418
Date: Thu, 24 Feb 2022 07:58:56 GMT
Content-Length: 0
Connection: keep-alive
Server: Ministry of Defence of the Russian Federation
@smly
smly / switchbot.py
Last active August 8, 2021 09:52
CLI for swithbot
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Usage: switchbot [OPTIONS]
Options:
-d, --device [light|speaker.tv|speaker.mpd|aircon]
[required]
--on
--off
@smly
smly / keras_interval_evalution.py
Last active February 28, 2021 10:46
An example to check the AUC score on a validation set for each 10 epochs.
"""
An example to check the AUC score on a validation set for each 10 epochs.
I hope it will be helpful for optimizing number of epochs.
"""
# -*- coding: utf-8 -*-
import logging
from sklearn.metrics import roc_auc_score
from keras.callbacks import Callback
[I 2020-04-16 15:04:56,418] Using an existing study with name 'parallel' instead of creating a new one.
feature_fraction, val_score: inf: 0%| | 0/7 [00:00<?, ?it/s][W 2020-04-16 15:04:56,643] Setting status of trial#15 as TrialState.FAIL because of the following error: AttributeError("'numpy.float64' object has no attribute 'translate'")
Traceback (most recent call last):
File "/home/smly/gitws/optuna/optuna/study.py", line 682, in _run_trial
result = func(trial)
File "/home/smly/gitws/optuna/optuna/integration/lightgbm_tuner/optimize.py", line 229, in __call__
param_value = min(trial.suggest_uniform("feature_fraction", 0.4, 1.0 + EPS), 1.0)
File "/home/smly/gitws/optuna/optuna/trial.py", line 550, in suggest_uniform
return self._suggest
@smly
smly / pr3_test_code.out
Created August 6, 2020 07:51
(xfeat:PR#3) Test code and the result
col target
0 Cat 1
1 Dog 1
2 Dog 0
3 Dog 0
4 Fox 0
<class 'cudf.core.dataframe.DataFrame'>
col target col_le
0 Cat 1 0
1 Dog 1 1
diff --git a/optuna/integration/lightgbm_tuner/optimize.py b/optuna/integration/lightgbm_tuner/optimize.py
index f11c8326..4434e51c 100644
--- a/optuna/integration/lightgbm_tuner/optimize.py
+++ b/optuna/integration/lightgbm_tuner/optimize.py
@@ -112,7 +112,14 @@ class BaseTuner(object):
else:
raise NotImplementedError
+ if self.lgbm_params.get("metric") == "None":
+ if len(booster.best_score[valid_name].keys()) > 0:
import optuna.integration.lightgbm as lgb
import numpy as np
import optuna
def test_run() -> None:
params = {
"objective": "binary",
"metric": "binary_logloss",
} # type: Dict
import numpy as np
import sklearn.datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import mlflow
import optuna
import optuna.integration.lightgbm as lgb