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

21,508

Solution 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

Solution 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.

Solution 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

Solution 4

For my case, I solve this problem pretty easily using

from xgboost import XGBRegressor

Share:
21,508
Amit
Author by

Amit

Updated on July 05, 2022

Comments

  • Amit
    Amit almost 2 years

    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?