简体   繁体   English

AttributeError: 模块“xgboost”没有属性“XGBRegressor”

[英]AttributeError: module ‘xgboost’ has no attribute ‘XGBRegressor’

I am trying to run xgboost using spyder and python, but I keep getting this error:我正在尝试使用 spyder 和 python 运行 xgboost,但我不断收到此错误:

AttributeError: module 'xgboost' has no attribute 'XGBRegressor' AttributeError: 模块“xgboost”没有属性“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)我有 Python 3.5.2 :: Anaconda 4.2.0 (x86_64)

How do I solve this?我该如何解决这个问题?

We probably have the same problem. 我们可能有同样的问题。

I solved it by telling Python explicitly where to find xgboost library. 我通过明确告诉Python在哪里找到xgboost库来解决它。

The reason is that I have more than one scripts with the name xgboost.py. 原因是我有多个脚本,名称为xgboost.py。 Python might have imported one of them mistakenly, so that it cannot find the definition of 'XGBRegressor'. Python可能错误地导入了其中一个,因此无法找到“ 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 对我来说,PATH_TO_YOUR_setup.py_file是〜/ xgboost / python-package

由于您的dir调用基本上丢失了所有内容,因此我怀疑,无论您从哪里启动脚本,都有一个xgboost子文件夹,其中包含一个空的__init__.py ,该子文件夹首先由import找到。

I had the exact same problem with Python 3.6.2 and Anaconda 1.6.8 on windows10 64bits (fall creator update) 我在Windows10 64位上使用Python 3.6.2和Anaconda 1.6.8时遇到了完全相同的问题(秋季创建者更新)

To get it to work, here is what I did : 为了使其正常工作,这是我所做的:

1/ Uninstall xgboost from within anaconda, in the chosen environement. 1 /在选定的环境中,从anaconda中卸载xgboost。

2/ Manually deleted the xgboost directory in C:\\ProgramData\\Anaconda3 2 /手动删除C:\\ ProgramData \\ Anaconda3中的xgboost目录

3/ Downloaded xgboost from This page 3 /从此页面下载的xgboost

4/ From Anaconda, launch a command prompt from (from the environment you want xgboost into of course) 4 /在Anaconda中,从中启动命令提示符(当然是从您要进入xgboost的环境中启动)

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) 5 /将CD下载到您将whl文件下载到的目录,然后键入:pip install xgboost‑0.6 + 20171121‑cp36‑cp36m‑win_amd64.whl(或您下载的文件的确切名称)

I did all these steps and xgboost worked properly 我做了所有这些步骤,xgboost正常工作

就我而言,我很容易使用

from xgboost import XGBRegressor

Probably there are many files with the same name of "xgboost".That is why python tries to load one of these instead of the original package file.可能有很多同名的“xgboost”文件。这就是为什么python试图加载其中一个而不是原始包文件的原因。

Tip Check your working directory and see if there are any py files with the same name of "xgboost.py".提示检查您的工作目录,看看是否有与“xgboost.py”相同名称的 py 文件。 If so, Change the name to something else.如果是这样,请将名称更改为其他名称。

I had to make sure to follow all the download instructions from the xgboost website. 我必须确保遵循xgboost网站上的所有下载说明。 After installing and compiling, i forgot to run these. 安装和编译后,我忘了运行这些。 https://xgboost.readthedocs.io/en/latest/build.html#python-package-installation https://xgboost.readthedocs.io/zh-CN/latest/build.html#python-package-installation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM