首页 > 解决方案 > 如何在嵌套的 sklearn 对象中优化 n_jobs 的使用?

问题描述

我正在使用带有 xgboost 和其他几个分类器的 StackingClassifier 运行一个集成模型,并希望利用始终可用的最多内核数(在我的情况下为 96 个)。

如果我必须选择,似乎并行化的最佳位置将在分类器本身内而不是在 StackingClassifier 中,因为它只需要运行交叉验证而不是树构建,但是即使在 StackingClassifier、随机森林中设置 n_jobs=1使用所有内核,但 xgboost 不使用多个内核。如何配置它以使 xgboost 使用所有可用内核?

rfc = RandomForestClassifier(
                            n_estimators=1000,
                            max_depth=None,
                            max_features = 0.2,
                            bootstrap=True,
                            random_state=0,
                            verbose=2,
                            n_jobs=-1
                            )

xgb = XGBClassifier(
                    n_estimators=1000,
                    max_depth=24,
                    max_features=0.2,
                    bootstrap=True,
                    objective="multi:softprob",
                    num_class=2,
                    colsample_bytree=0.3,
                    gamma=1, 
                    learning_rate=0.01,
                    subsample=0.9,
                    random_state=0,
                    verbosity=2,
                    nthread=-1,
                    n_jobs=-1)
estimators = [
    ('rfc', rfc)
    ]


stacker = StackingClassifier(
                    estimators=estimators, 
                    final_estimator=xgb,
                    stack_method="predict_proba",
                    verbose=2,
                    cv=2,
                    n_jobs=1,
                    passthrough=True)

calibrator = CalibratedClassifierCV(
                                    base_estimator = stacker,
                                    method="sigmoid",
                                    cv=2) 

标签: python-3.xmachine-learningscikit-learnxgboost

解决方案


推荐阅读