2017-09-17 222 views
0

我想在python中使用scikit-learn API创建一个xgboost回归模型,指定一个权重列。下面是一个最小的代码示例:sample_weight在XGBregressor中不被识别

from xgboost import XGBRegressor 
import pandas as pd 
import numpy as np 
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) 
model = XGBRegressor() 
model.fit(df[['A','B']],df['D'],sample_weight=df['C']) 

当我这样做,我得到下面的输出:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-12-2d43e3c01bbb> in <module>() 
     6 
     7 
----> 8 model.fit(df[['A','B']],df['D'],sample_weight=df['C']) 

TypeError: fit() got an unexpected keyword argument 'sample_weight' 

据我所知道的,语法是正确的,根据文档: https://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.sklearn

有人报告这个问题的XGBoost开发商前一段时间,它似乎已被固定的,所以我不知道这是为什么仍然发生:

https://github.com/dmlc/xgboost/pull/1874

如何安装修复此问题的xgboost版本?我正在使用Ubuntu 64位上的Jupyter Notebook和Anaconda。我应该在没有Anaconda的情况下尝试吗?

回答

0

我能够通过从github安装xgboost而不是从pip安装它来解决此问题。我还没有得到它与蟒蛇一起工作,但下面做了我的伎俩:

sudo apt-get install python3.6 
sudo apt-get install git 
git clone –recursive https://github.com/dmlc/xgboost 
cd xgboost; make -j4 
cd python-package; python3 setup.py install