[PYTHON] Survey log of how to optimize LightGBM hyperparameters using Optuna

Introduction

Recently I'm studying machine learning using Jupyter Lab. Among them, I thought that I would like to write an article about Optuna's research on LightGBM hyperparameter optimization, which had an old search result on Google.

environment

What i did

First rough summary

--I want to optimize LightGBM piper parameters with Optuna ――It doesn't work well even if I copy the code that came out by google --Somehow best_params is said to have no arguments --Apply the official document --Try the official document sample → It works --Check past samples from Github and confirm that there are best_params as of 1.3 It seems that it will not be 1.4 or later ――If you have a problem, read the official document. ――If the behavior is different from the information on the net, it's a good idea to check the version of the library.

I want to optimize LightGBM hyperparameters with Optuna

This is the first thing that came out when I tried google Hyperparameter automatic optimization by LightGBM Tuner extension Below is an excerpt of the code on the above page

booster = lgb.train(params, dtrain, valid_sets=dval,
                    verbose_eval=0,
                    best_params=best_params,
                    tuning_history=tuning_history)

When I tried the above, I got the following error. TypeError: __init__() got an unexpected keyword argument 'best_params'

HM. There is no keyword argument for best_params. Is said to be.

Official document confirmation

I wondered if the different arguments mean different versions, so I decided to check the official documentation. Optuna There was a link to Github, so I moved there optuna/optuna

Try the official documentation sample → works

Search in the repository with lightgbm at optuna / optuna Search results in repository

Looking at the search results, I found that examples / README.md was like a sample. Confirm it.

examples/lightgbm_tuner_simple.py

    #Some line breaks are used to make it easier to compare citation sources.
    model = lgb.train(
        params, dtrain, valid_sets=[dtrain, dval], 
        verbose_eval=100, 
        early_stopping_rounds=100
    )

There is no best_params here, so the current version looks like this. I tried it above and it worked! I did it.

By the way, when I checked, there is best_params up to v1.3.0, and [v1.4.0](https:: From //github.com/optuna/optuna/blob/v1.4.0/examples/lightgbm_tuner_simple.py), there was no best_params.

Solution & impression

Actually, I checked pages other than the above, but I omitted the ones that did not produce any results (or rather, I forgot) When investigating and resolving such problems and errors does not produce results, it does not come out at all. It's hard to get results. In such a case, it is recommended to change the day or consult with other people lightly because you can see the way of life unexpectedly.

Summary

If the result of google does not work, it is often due to different environment or version. If the specifications and samples are publicly available, it is often a shortcut to solve the problem by referring to the official documentation and Github. (Although this time is different) It is also important to check the content of the error message and search by message. If the number of arguments or the arguments themselves are different, it is likely that the version is different. If you can't find a solution, it's important to talk to someone else or change the date.

Recommended Posts

Survey log of how to optimize LightGBM hyperparameters using Optuna
How to set up SVM using Optuna
How to set xg boost using Optuna
How to set up Random forest using Optuna
How to change the log level of Azure SDK for Python
ABC170 E --How to solve without using multiset of Smart Infants
How to save only a part of a long video using OpenCV
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
How to log in to Docker + NGINX
Optimize RF or lightGBM with Optuna
How to test each version of IE using Selenium in modan.IE (VM)
How to find out the number of CPUs without using the sar command
[Python] Summary of how to use pandas
How to set optuna (how to write search space)
How to get rid of long comprehensions
How to check the version of Django
How to draw a graph using Matplotlib
How to install CatBoost [as of January 2020]
How to calculate Use% of df command
How to install a package using a repository
Log in to Slack using requests in Python
[Python2.7] Summary of how to use unittest
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
How to download youtube videos using pytube3
Summary of how to use Python list
Summary of how to write AWS Lambda
[Question] How to use plot_surface of python
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to build an environment for using multiple versions of Python on Mac