2017-09-17 28 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