티스토리 뷰

728x90

안녕하세요.

 

최근에 LightGBM 모델을 사용할 일이 있어서 예전에 쓰던 코드를 사용하다 보니,

 

TypeError: LGBMClassifier.fit() got an unexpected keyword argument 'early_stopping_rounds'

 

이런 에러가 났습니다. 

 

그래서 원인을 찾다가, lightGBM을 다운그레이드(pip install lightgbm==3.3.2)도 시도했는데, 그 조차도 쉽지 않았습니다. (m1 맥 사용중입니다.)

 

더보기

Collecting lightgbm==3.3.2
  Using cached lightgbm-3.3.2.tar.gz (1.5 MB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: wheel in /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages (from lightgbm==3.3.2) (0.41.2)
Requirement already satisfied: numpy in /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages (from lightgbm==3.3.2) (1.26.0)
Requirement already satisfied: scipy in /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages (from lightgbm==3.3.2) (1.11.2)
Requirement already satisfied: scikit-learn!=0.22.0 in /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages (from lightgbm==3.3.2) (1.3.1)
Requirement already satisfied: joblib>=1.1.1 in /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages (from scikit-learn!=0.22.0->lightgbm==3.3.2) (1.3.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages (from scikit-learn!=0.22.0->lightgbm==3.3.2) (3.2.0)
Building wheels for collected packages: lightgbm
  Building wheel for lightgbm (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [80 lines of output]
      running bdist_wheel
      /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      running build
      running build_py
      creating build
      creating build/lib
      creating build/lib/lightgbm
      copying lightgbm/callback.py -> build/lib/lightgbm
      copying lightgbm/compat.py -> build/lib/lightgbm
      copying lightgbm/plotting.py -> build/lib/lightgbm
      copying lightgbm/__init__.py -> build/lib/lightgbm
      copying lightgbm/engine.py -> build/lib/lightgbm
      copying lightgbm/dask.py -> build/lib/lightgbm
      copying lightgbm/basic.py -> build/lib/lightgbm
      copying lightgbm/libpath.py -> build/lib/lightgbm
      copying lightgbm/sklearn.py -> build/lib/lightgbm
      running egg_info
      writing lightgbm.egg-info/PKG-INFO
      writing dependency_links to lightgbm.egg-info/dependency_links.txt
      writing requirements to lightgbm.egg-info/requires.txt
      writing top-level names to lightgbm.egg-info/top_level.txt
      reading manifest file 'lightgbm.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      no previously-included directories found matching 'build'
      warning: no files found matching '*.so' under directory 'lightgbm'
      warning: no files found matching '*.so' under directory 'compile'
      warning: no files found matching '*.dll' under directory 'compile/Release'
      warning: no files found matching '*.dll' under directory 'compile/windows/x64/DLL'
      warning: no previously-included files matching '*.py[co]' found anywhere in distribution
      warning: no previously-included files found matching 'compile/external_libs/compute/.git'
      adding license file 'LICENSE'
      writing manifest file 'lightgbm.egg-info/SOURCES.txt'
      copying lightgbm/VERSION.txt -> build/lib/lightgbm
      INFO:wheel:installing to build/bdist.macosx-11.1-arm64/wheel
      running install
      INFO:LightGBM:Starting to compile the library.
      INFO:LightGBM:Starting to compile with CMake.
      Traceback (most recent call last):
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 95, in silent_call
          subprocess.check_call(cmd, stderr=log, stdout=log)
        File "/Users/seedbox/miniconda/lib/python3.10/subprocess.py", line 369, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['cmake', '/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/compile']' returned non-zero exit status 1.
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 334, in <module>
          setup(name='lightgbm',
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 185, in setup
          return run_commands(dist)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
          dist.run_commands()
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 968, in run_commands
          self.run_command(cmd)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
          super().run_command(command)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
          cmd_obj.run()
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/wheel/bdist_wheel.py", line 399, in run
          self.run_command("install")
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 319, in run_command
          self.distribution.run_command(command)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
          super().run_command(command)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
          cmd_obj.run()
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 248, in run
          compile_cpp(use_mingw=self.mingw, use_gpu=self.gpu, use_cuda=self.cuda, use_mpi=self.mpi,
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 198, in compile_cpp
          silent_call(cmake_cmd, raise_error=True, error_msg='Please install CMake and all required dependencies first')
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 99, in silent_call
          raise Exception("\n".join((error_msg, LOG_NOTICE)))
      Exception: Please install CMake and all required dependencies first
      The full version of error log was saved into /Users/seedbox/LightGBM_compilation.log
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for lightgbm
  Running setup.py clean for lightgbm
Failed to build lightgbm
Installing collected packages: lightgbm
  Running setup.py install for lightgbm ... error
  error: subprocess-exited-with-error
  
  × Running setup.py install for lightgbm did not run successfully.
  │ exit code: 1
  ╰─> [39 lines of output]
      running install
      /Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      INFO:LightGBM:Starting to compile the library.
      INFO:LightGBM:Starting to compile with CMake.
      Traceback (most recent call last):
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 95, in silent_call
          subprocess.check_call(cmd, stderr=log, stdout=log)
        File "/Users/seedbox/miniconda/lib/python3.10/subprocess.py", line 369, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['cmake', '/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/compile']' returned non-zero exit status 1.
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 334, in <module>
          setup(name='lightgbm',
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 185, in setup
          return run_commands(dist)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
          dist.run_commands()
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 968, in run_commands
          self.run_command(cmd)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
          super().run_command(command)
        File "/Users/seedbox/project/DATA_STUDY/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
          cmd_obj.run()
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 248, in run
          compile_cpp(use_mingw=self.mingw, use_gpu=self.gpu, use_cuda=self.cuda, use_mpi=self.mpi,
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 198, in compile_cpp
          silent_call(cmake_cmd, raise_error=True, error_msg='Please install CMake and all required dependencies first')
        File "/private/var/folders/4_/kbkdg_cn0z5_y3bg5394y50h0000gn/T/pip-install-dz1btnd5/lightgbm_e6dfeb7935044ded867b273865ac15cf/setup.py", line 99, in silent_call
          raise Exception("\n".join((error_msg, LOG_NOTICE)))
      Exception: Please install CMake and all required dependencies first
      The full version of error log was saved into /Users/seedbox/LightGBM_compilation.log
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> lightgbm

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

 

그래서 공식 문서에 https://lightgbm.readthedocs.io/en/latest/Installation-Guide.html#install-using-homebrew 있길래 이 조차도 계속 실패했다가, 2주전에 lightGBM 담당자 분이 올린 예제 코드가 있더군요.

 

저 처럼 시간 버리지 마시고 이거 사용해보세요.  early_stopping_rounds 방법은 2가지라고 합니다.

 

취향에 따라 사용하라고 하네요.

 

 

 

import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

dtrain = lgb.Dataset(X, label=y)
dvalid = lgb.Dataset(X_test, label=y_test)


class CustomMetric:
    """Metric that starts getting worse after the 4th iteration"""
    def __init__(self):
        self.scores = [1.0, 0.9, 0.8, 0.7, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8]

    def __call__(self, y_true, y_pred):
        is_higher_better = False
        return "decreasing_metric", self.scores.pop(0), is_higher_better

params = {
    "verbosity": 1,
    "max_depth": 3,
    "num_leaves": 7,
    "min_data_in_leaf": 5,
}

# approach 1: use early_stopping() callback
bst_1 = lgb.train(
    params=params,
    num_boost_round=10,
    train_set=dtrain,
    valid_sets=[dvalid],
    feval=CustomMetric(),
    callbacks=[
        lgb.early_stopping(stopping_rounds=3),
        lgb.log_evaluation(1)
    ]
)

# approach 2: pass "early_stopping_round" through paramms
bst_2 = lgb.train(
    params={**params, "early_stopping_round": 3},
    num_boost_round=10,
    train_set=dtrain,
    valid_sets=[dvalid],
    feval=CustomMetric(),
    callbacks=[
        lgb.log_evaluation(1)
    ]
)

## 예측 확률
pred_proba = bst_1.predict(X_test)

# 이진 분류 예측 결과값 얻기
threshold = 0.5  # 예측 결과값을 0 또는 1로 변환하는 임계값
pred_binary = (bst_1.predict(X_test) > threshold).astype(int)

https://github.com/microsoft/LightGBM/issues/5196#issuecomment-1711049304

728x90
댓글