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

时间:2019-05-22 13:45:12

标签: 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)

1 个答案:

答案 0 :(得分:0)

对不起,我找到了答案。这里是LightGBM文档中的文字:“为了获得最佳速度,请将其设置为实际CPU内核的数量,而不是线程的数量”。因此设置n_jobs=-1并不是最佳选择。