当你有大量 类 时,为什么 xgboost 这么慢?

Why is xgboost so slow when you have a large number of classes?

我有一个维度为 (40000, 21) 的稀疏数据集。我正在尝试使用 xgboost 为其构建分类模型。不幸的是,它太慢了,对我来说永远不会终止。但是,在同一数据集上,scikit-learn 的 RandomForestClassifer 大约需要 1 秒。这是我使用的代码:

from xgboost import XGBClassifier
from sklearn.ensemble import RandomForestClassifier
[...]
t0 = time()
rf = RandomForestClassifier(n_jobs=-1)
rf.fit(trainX, trainY)
print("RF score", rf.score(testX, testY))
print("Time to fit and score random forest", time()-t0)

t0 = time()
clf = XGBClassifier(n_jobs=-1)
clf.fit(trainX, trainY, verbose=True)
print(clf.score(testX, testY))
print("Time taken to fit and score xgboost", time()-t0)

显示火车类型X:

print(repr(trainX))    
<40000x21 sparse matrix of type '<class 'numpy.int64'>'
    with 360000 stored elements in Compressed Sparse Row format>

请注意,我使用了除 n_jobs 之外的所有默认参数。

What am I doing wrong?


In [3]: print(xgboost.__version__)
0.6
print(sklearn.__version__)
0.19.1

到目前为止,我根据评论中的建议尝试了以下方法:


这是使用假数据重现问题的完整代码。我为两个分类器都设置了 n_estimators=50,这将 RandomForestClassifier 减慢到大约 16 秒。另一方面,Xgboost 对我来说仍然永远不会终止。

#!/usr/bin/python3

from sklearn.datasets import make_classification
from xgboost import XGBClassifier
from sklearn.ensemble import RandomForestClassifier
from time import time

(trainX, trainY) = make_classification(n_informative=10, n_redundant=0, n_samples=50000, n_classes=120)

print("Shape of trainX and trainY", trainX.shape, trainY.shape)
t0 = time()
rf = RandomForestClassifier(n_estimators=50, n_jobs=-1)
rf.fit(trainX, trainY)
print("Time elapsed by RandomForestClassifier is: ", time()-t0)
t0 = time()
xgbrf = XGBClassifier(n_estimators=50, n_jobs=-1,verbose=True)
xgbrf.fit(trainX, trainY)
print("Time elapsed by XGBClassifier is: ", time()-t0)

事实证明,xgboost 的 运行 时间与 类 的数量呈二次方关系。参见 https://github.com/dmlc/xgboost/issues/2926