AttributeError: module ‘xgboost’ has no attribute

2020-02-13 06:22发布

问题:

I am trying to run xgboost using spyder and python, but I keep getting this error:

AttributeError: module ‘xgboost’ has no attribute ‘XGBRegressor’

Here is the code:

import xgboost as xgb 

xgb.XGBRegressor(max_depth=3, learning_rate=0.1, n_estimators=100, silent=True, 
                 objective='reg:linear', gamma=0, min_child_weight=1, 
                 max_delta_step=0, subsample=1, colsample_bytree=1, 
                 seed=0, missing=None)

Error is

Traceback (most recent call last):

  File "<ipython-input-33-d257a9a2a5d8>", line 1, in <module>
    xgb.XGBRegressor(max_depth=3, learning_rate=0.1, n_estimators=100, silent=True,

AttributeError: module 'xgboost' has no attribute 'XGBRegressor'

I have Python 3.5.2 :: Anaconda 4.2.0 (x86_64)

How do I solve this?

回答1:

We probably have the same problem.

I solved it by telling Python explicitly where to find xgboost library.

The reason is that I have more than one scripts with the name xgboost.py. Python might have imported one of them mistakenly, so that it cannot find the definition of 'XGBRegressor'.

Here is the command I used:

export PYTHONPATH=PATH_TO_YOUR_setup.py_file

For me, PATH_TO_YOUR_setup.py_file is ~/xgboost/python-package



回答2:

Since your dir call is missing basically everything, my suspicion is that wherever you're starting your script from has an xgboost subfolder with an empty __init__.py in it that is being found first by your import.



回答3:

I had the exact same problem with Python 3.6.2 and Anaconda 1.6.8 on windows10 64bits (fall creator update)

To get it to work, here is what I did :

1/ Uninstall xgboost from within anaconda, in the chosen environement.

2/ Manually deleted the xgboost directory in C:\ProgramData\Anaconda3

3/ Downloaded xgboost from This page

4/ From Anaconda, launch a command prompt from (from the environment you want xgboost into of course)

5/ CD to the directory you downloaded the whl file to and type : pip install xgboost‑0.6+20171121‑cp36‑cp36m‑win_amd64.whl (or the exact name of the file you downloaded)

I did all these steps and xgboost worked properly



回答4:

I had to make sure to follow all the download instructions from the xgboost website. After installing and compiling, i forgot to run these. https://xgboost.readthedocs.io/en/latest/build.html#python-package-installation