0

I'm using XGBRegressor algorithm in Jupyter Notebook. Here is my script:

from xgboost import XGBRegressor #xgb regressor module

xgb = XGBRegressor(n_estimators = 3000,learning_rate=0.01)
xgb.fit(X_train,y_train)

But after running the script, I faced the following error,

AttributeError                            Traceback (most recent call last)
Cell In[287], line 4
      1 from xgboost import XGBRegressor #xgb regressor module
      3 xgb = XGBRegressor(n_estimators = 3000,learning_rate=0.01)
----> 4 xgb.fit(X_train,y_train)
      5 pred_xgb = xgb.predict(X_test)
      6 s6 = r2_score(Y_test,pred_xgb)

File F:\Anaconda\lib\site-packages\xgboost\sklearn.py:320, in XGBModel.fit(self, X, y, sample_weight, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set)
    317     else:
    318         params.update({'eval_metric': eval_metric})
--> 320 self._Booster = train(params, trainDmatrix,
    321                       self.n_estimators, evals=evals,
    322                       early_stopping_rounds=early_stopping_rounds,
    323                       evals_result=evals_result, obj=obj, feval=feval,
    324                       verbose_eval=verbose, xgb_model=xgb_model)
    326 if evals_result:
    327     for val in evals_result.items():

File F:\Anaconda\lib\site-packages\xgboost\training.py:200, in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, xgb_model, callbacks, learning_rates)
    196     warnings.warn("learning_rates parameter is deprecated - use callback API instead",
    197                   DeprecationWarning)
    198     callbacks.append(callback.reset_learning_rate(learning_rates))
--> 200 return _train_internal(params, dtrain,
    201                        num_boost_round=num_boost_round,
    202                        evals=evals,
    203                        obj=obj, feval=feval,
    204                        xgb_model=xgb_model, callbacks=callbacks)

File F:\Anaconda\lib\site-packages\xgboost\training.py:32, in _train_internal(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks)
     29     for eval_metric in eval_metrics:
     30         params += [('eval_metric', eval_metric)]
---> 32 bst = Booster(params, [dtrain] + [d[0] for d in evals])
     33 nboost = 0
     34 num_parallel_tree = 1

File F:\Anaconda\lib\site-packages\xgboost\core.py:862, in Booster.__init__(self, params, cache, model_file)
    859 self.handle = ctypes.c_void_p()
    860 _check_call(_LIB.XGBoosterCreate(dmats, c_bst_ulong(len(cache)),
    861                                  ctypes.byref(self.handle)))
--> 862 self.set_param({'seed': 0})
    863 self.set_param(params or {})
    864 if model_file is not None:

File F:\Anaconda\lib\site-packages\xgboost\core.py:995, in Booster.set_param(self, params, value)
    985 def set_param(self, params, value=None):
    986     """Set parameters into the Booster.
    987 
    988     Parameters
   (...)
    993        value of the specified parameter, when params is str key
    994     """
--> 995     if isinstance(params, collections.Mapping):
    996         params = params.items()
    997     elif isinstance(params, STRING_TYPES) and value is not None:

AttributeError: module 'collections' has no attribute 'Mapping'

How can I resolve the issue?

I'm using Python 3.10.12

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Etemon
  • 53
  • 2
  • 11
  • what is your ```X_train, y_train```. Can you give a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Suraj Shourie Aug 28 '23 at 15:23
  • @SurajShourie They are floating point numbers I have seven feature and one target value. Overall it is 250 rows data. Also please note that I hadn't this issue with other regression algorithms such as random forest and Gradient Boost. – Etemon Aug 28 '23 at 15:29
  • I think your Python version is `3.10` or newer which seems to be the issue. You should downgrade to `3.8` or lower and that should fix the problem. You can checkout [these](https://stackoverflow.com/questions/70195545/module-collections-has-no-attribute-mapping-issue-on-macos-for-sdk-installa) [threads](https://github.com/GoogleCloudPlatform/gsutil/issues/1331) for reference. – Ro.oT Aug 28 '23 at 15:36
  • Check the version of python and xgb ```!python --version xgb.__version__``` – Suraj Shourie Aug 28 '23 at 15:37
  • I think you're using an older version of xgb as well (doesn't match the source code for latest version) – Suraj Shourie Aug 28 '23 at 15:37
  • Thanks for the help. It is "Python 3.10.12" – Etemon Aug 28 '23 at 15:40

0 Answers0