gpt4 book ai didi

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

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

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

抱歉我的英语不好。

环境:Python 3.5、scikit-learn 0.20.3、lightgbm 2.2.3Inter 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/

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