gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:49:28 27 4
gpt4 key购买 nike

我有一个维度为 (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)

显示trainX的类型:

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_enumerators = 5。现在至少在 62 秒内完成。这仍然比 RandomForestClassifier 慢 60 倍左右。
  • 使用 n_enumerators = 5,我删除了 n_jobs=-1 并设置了 n_jobs=1。然后它在大约 107 秒内完成(比 RandomForestClassifier 慢大约 100 倍)。如果我将 n_jobs 增加到 4,则速度可达 27 秒。仍比 RandomForestClassifier 慢约 27 倍。
  • 如果我保留默认的估计器数量,它仍然永远不会完成。
<小时/>

这里是使用虚假数据重现问题的完整代码。我为两个分类器设置了 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 .

关于machine-learning - 当你有大量类时,为什么 xgboost 这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47594499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com