Rap*_*ael 0 machine-learning scikit-learn xgboost
我有一个稀疏的维度数据集(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)
Run Code Online (Sandbox Code Playgroud)
显示 trainX 的类型:
print(repr(trainX))
<40000x21 sparse matrix of type '<class 'numpy.int64'>'
with 360000 stored elements in Compressed Sparse Row format>
Run Code Online (Sandbox Code Playgroud)
请注意,我使用除 n_jobs 之外的所有默认参数。
我究竟做错了什么?
In [3]: print(xgboost.__version__)
0.6
print(sklearn.__version__)
0.19.1
Run Code Online (Sandbox Code Playgroud)
到目前为止,我根据评论中的建议尝试了以下操作:
n_enumerators = 5。现在至少在 62 秒内完成。这仍然比 RandomForestClassifier 慢 60 倍左右。n_enumerators = 5我的移除n_jobs=-1和设置n_jobs=1。然后它在大约 107 秒内完成(比 RandomForestClassifier 慢大约 100 倍)。如果我增加到n_jobs4,则速度可达 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7060 次 |
| 最近记录: |