• 카테고리

    질문 & 답변
  • 세부 분야

    딥러닝 · 머신러닝

  • 해결 여부

    미해결

BayesianOptimization 과정 중에서 'module' object is not callable 오류가 발생합니다.

20.11.21 06:54 작성 조회수 217

0

from sklearn.metrics import  mean_squared_error
import lightgbm as lgb

def lgb_eval(num_leaves,feature_fraction,bagging_fraction,max_depth,min_data_in_leaf):

    params = {'num_leaves':int(round(num_leaves)),
      'min_data_in_leaf':min_data_in_leaf,
      'objective':'regression', 
      'max_depth':int(round(max_depth)),
      'learning_rate':0.02,
      "boosting":'gbdt',
      "feature_fraction":feature_fraction,
      "bagging_freq":1,
      "bagging_fraction": bagging_fraction,
      "bagging_seed":11,
      "metric":'rmse', 
      "random_state":2019}
    print("params:", params)
    
    lgb_model = lgb(**params)
    lgb_model.fit(X_train, y_train, eval_set=[(X_test, y_test)], early_stopping_rounds=30, eval_metric="rmse", verbose=100 )
    best_iter = lgb_model.best_iteration_
    print('best_iter:', best_iter)
    valid_proba = lgb_model.predict_proba(X_test, num_iteration=best_iter)[:, 1]
    rmse = np.sqrt(mean_squared_error(y_test, y_pred))
    print('rmse:',rmse)

    return rmse
#####################################

bayes_params = {
    'num_leaves': (24, 45),
    'feature_fraction':(0.5, 1), 
    'bagging_fraction': (0.5, 1),
    'max_depth': (4, 12),
    'min_data_in_leaf':(5, 50)
}

#################################
from bayes_opt import BayesianOptimization

BO_lgb = BayesianOptimization(lgb_eval, bayes_params, random_state=0)

###############################################
BO_lgb.maximize(init_points=5, n_iter=10)

안녕하세요 언제나 좋은 강의 감사합니다.

제가 lightgbm 모델을 BayesianOptimization을 통해 최적의 하이퍼 파라미터를 구하고자하는데,

param이 print되는 것 까지는 진행이 되고나서,  아래 오류와 같이 'module' object is not callable 라는 안내문이 출력되엇습니다. lgb 모듈이 불러와지지 않앗다는 의미인것 같은데 import로 호출한 것과는 별개의 문제인가요?

|   iter    |  target   | baggin... | featur... | max_depth | min_da... | num_le... |
-------------------------------------------------------------------------------------
params: {'num_leaves': 33, 'min_data_in_leaf': 29.51974323486036, 'objective': 'regression', 'max_depth': 9, 'learning_rate': 0.02, 'boosting': 'gbdt', 'feature_fraction': 0.8575946831862098, 'bagging_freq': 1, 'bagging_fraction': 0.7744067519636624, 'bagging_seed': 11, 'metric': 'rmse', 'random_state': 2019}
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
E:\anaconda\lib\site-packages\bayes_opt\target_space.py in probe(self, params)
    190         try:
--> 191             target = self._cache[_hashable(x)]
    192         except KeyError:

KeyError: (0.7744067519636624, 0.8575946831862098, 8.822107008573152, 29.51974323486036, 32.896750786116996)

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-103-adbdc94e6584> in <module>
----> 1 BO_lgb.maximize(init_points=5, n_iter=10)

E:\anaconda\lib\site-packages\bayes_opt\bayesian_optimization.py in maximize(self, init_points, n_iter, acq, kappa, kappa_decay, kappa_decay_delay, xi, **gp_params)
    183                 iteration += 1
    184 
--> 185             self.probe(x_probe, lazy=False)
    186 
    187             if self._bounds_transformer:

E:\anaconda\lib\site-packages\bayes_opt\bayesian_optimization.py in probe(self, params, lazy)
    114             self._queue.add(params)
    115         else:
--> 116             self._space.probe(params)
    117             self.dispatch(Events.OPTIMIZATION_STEP)
    118 

E:\anaconda\lib\site-packages\bayes_opt\target_space.py in probe(self, params)
    192         except KeyError:
    193             params = dict(zip(self._keys, x))
--> 194             target = self.target_func(**params)
    195             self.register(x, target)
    196         return target

<ipython-input-101-a7b26b7824de> in lgb_eval(num_leaves, feature_fraction, bagging_fraction, max_depth, min_data_in_leaf)
     22 
     23     # 모델 훈련
---> 24     lgb_model = lgb(**params)
     25     lgb_model.fit(X_train, y_train, eval_set=[(X_test, y_test)], early_stopping_rounds=30, eval_metric="rmse", verbose=100 )
     26     best_iter = lgb_model.best_iteration_

TypeError: 'module' object is not callable

답변 1

답변을 작성해보세요.

0

안녕하십니까,

LightGBM Classifier 객체 생성시의 오류군요.

lgb_model = lgb.LGBMClassifier(**params) 로 수행해보시지요.

또는

from lightgbm import LGBMClassifier

lgb_model = LGBMClassifier(**params)

감사합니다.