machine-learning - 为什么使用 LGB 时 10 倍交叉验证甚至比 1 倍拟合更快?

标签 machine-learning scikit-learn cross-validation model-fitting lightgbm

我正在使用 LGB 来处理机器学习任务。但我发现当我使用 sklearn API cross_val_score 并设置 cv=10 时,时间成本比单折拟合要少。我使用 train_test_split 分割数据集,然后在训练集上拟合 LGBClassifier。后者的时间成本比前者多很多,为什么?

抱歉我的英语不好。

环境:Python 3.5、scikit-learn 0.20.3、lightgbm 2.2.3 Inter Xeon CPU E5-2650 v4 内存128GB

X = train_df.drop(['uId', 'age'], axis=1)
Y = train_df.loc[:, 'age']
X_test = test_df.drop(['uId'], axis=1)

X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.1,
                                                  stratify=Y)

# (1809000, 12) (1809000,) (201000, 12) (201000,) (502500, 12)
print(X_train.shape, Y_train.shape, X_val.shape, Y_val.shape, X_test.shape)

from lightgbm import LGBMClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
import time

lgb = LGBMClassifier(n_jobs=-1)

tic = time.time()
scores = cross_val_score(lgb, X, Y,
                         scoring='accuracy', cv=10, n_jobs=-1)
toc = time.time()
# 0.3738402985074627 0.0009231167322574765 300.1487271785736
print(np.mean(scores), np.std(scores), toc-tic)

tic = time.time()
lgb.fit(X_train, Y_train)
toc = time.time()
# 0.3751492537313433 472.1763586997986 (is much more than 300)
print(accuracy_score(Y_val, lgb.predict(X_val)), toc-tic)

最佳答案

抱歉,我找到答案了。 LightGBM 的文档中写道:“为了获得最佳速度,请将其设置为实际 CPU 核心数,而不是线程数”。所以设置n_jobs=-1并不是最好的选择。

关于machine-learning - 为什么使用 LGB 时 10 倍交叉验证甚至比 1 倍拟合更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56258322/

相关文章:

python - 在Sklearn中正确计算F1分数

python - 如何打印基于SVM分类器的分类点

python - 使用 Sklearn featurehasher

python - 如何在 CV-ing 数据集时实现基于比率的 SMOTE 过采样

machine-learning - 是使用逻辑回归进行情感分析时获得积极或消极程度的一种方法

python - One-Hot 编码训练和测试数据时形状不匹配。将 get_dummies 与管道一起使用时,Train_Data 比 Test_data 具有更多的虚拟列

python - 如何从 gridsearchcv 绘制决策树?

python - Keras 没有做出好的预测

python - 如何在不使用 One Hot 编码的情况下将行数据转换为列

python - tensorflow 逻辑回归的准确性非常差