使用 xgboost 分类器进行多类分类?
Multiclass classification with xgboost classifier?
我正在尝试使用 xgboost 进行多class class化,我已经使用此代码构建了它,
clf = xgb.XGBClassifier(max_depth=7, n_estimators=1000)
clf.fit(byte_train, y_train)
train1 = clf.predict_proba(train_data)
test1 = clf.predict_proba(test_data)
这给了我一些不错的结果。对于我的案例,我的对数损失低于 0.7。但是在浏览了几页之后,我发现我们必须在 XGBClassifier 中使用另一个 objective 来解决 multi-class 问题。以下是这些页面的推荐内容。
clf = xgb.XGBClassifier(max_depth=5, objective='multi:softprob', n_estimators=1000,
num_classes=9)
clf.fit(byte_train, y_train)
train1 = clf.predict_proba(train_data)
test1 = clf.predict_proba(test_data)
此代码也能正常工作,但与我的第一个代码相比需要花费很多时间才能完成。
为什么我的第一个代码也适用于多个 class 案例?我已经检查过它的默认 objective 是 binary:logistic 用于二进制 classification 但它对 multi-class 真的很有效吗?如果两者都正确,我应该使用哪一个?
默认情况下,XGBClassifier 使用 objective='binary:logistic'
。当您使用此 objective 时,它会采用以下任一策略:one-vs-rest
(也称为 one-vs-all)和 one-vs-one
。它可能不是您手头问题的正确选择。
当您使用 objective='multi:softprob'
时,输出是数据点数 * classes 数的向量。因此,代码的时间复杂度会增加。
尝试在您的代码中设置 objective=multi:softmax
。它更适合多class class化任务。
默认情况下,XGBClassifier 或许多分类器使用 objective 作为 binary 但它在内部所做的是分类(一个与其余)即如果你有 3 类 它将给出结果(0 vs 1&2)。如果你处理的超过 2 类,你应该总是使用 softmax。Softmax 将 logits 转化为概率,这将求和 1.On 在此基础上,它做出 类 具有最高 probabilities.As 的预测你可以看到复杂性增加,如 Saurabh 在他的回答所以需要更多时间。
其实即使XGBClassifier
默认的obj参数是binary:logistic
,内部也会判断标签y的class的个数。当class数大于2时,会修改obj参数为multi:softmax
.
https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py
class XGBClassifier(XGBModel, XGBClassifierBase):
# pylint: disable=missing-docstring,invalid-name,too-many-instance-attributes
def __init__(self, objective="binary:logistic", **kwargs):
super().__init__(objective=objective, **kwargs)
def fit(self, X, y, sample_weight=None, base_margin=None,
eval_set=None, eval_metric=None,
early_stopping_rounds=None, verbose=True, xgb_model=None,
sample_weight_eval_set=None, callbacks=None):
# pylint: disable = attribute-defined-outside-init,arguments-differ
evals_result = {}
self.classes_ = np.unique(y)
self.n_classes_ = len(self.classes_)
xgb_options = self.get_xgb_params()
if callable(self.objective):
obj = _objective_decorator(self.objective)
# Use default value. Is it really not used ?
xgb_options["objective"] = "binary:logistic"
else:
obj = None
if self.n_classes_ > 2:
# Switch to using a multiclass objective in the underlying
# XGB instance
xgb_options['objective'] = 'multi:softprob'
xgb_options['num_class'] = self.n_classes_
我正在尝试使用 xgboost 进行多class class化,我已经使用此代码构建了它,
clf = xgb.XGBClassifier(max_depth=7, n_estimators=1000)
clf.fit(byte_train, y_train)
train1 = clf.predict_proba(train_data)
test1 = clf.predict_proba(test_data)
这给了我一些不错的结果。对于我的案例,我的对数损失低于 0.7。但是在浏览了几页之后,我发现我们必须在 XGBClassifier 中使用另一个 objective 来解决 multi-class 问题。以下是这些页面的推荐内容。
clf = xgb.XGBClassifier(max_depth=5, objective='multi:softprob', n_estimators=1000,
num_classes=9)
clf.fit(byte_train, y_train)
train1 = clf.predict_proba(train_data)
test1 = clf.predict_proba(test_data)
此代码也能正常工作,但与我的第一个代码相比需要花费很多时间才能完成。
为什么我的第一个代码也适用于多个 class 案例?我已经检查过它的默认 objective 是 binary:logistic 用于二进制 classification 但它对 multi-class 真的很有效吗?如果两者都正确,我应该使用哪一个?
默认情况下,XGBClassifier 使用 objective='binary:logistic'
。当您使用此 objective 时,它会采用以下任一策略:one-vs-rest
(也称为 one-vs-all)和 one-vs-one
。它可能不是您手头问题的正确选择。
当您使用 objective='multi:softprob'
时,输出是数据点数 * classes 数的向量。因此,代码的时间复杂度会增加。
尝试在您的代码中设置 objective=multi:softmax
。它更适合多class class化任务。
默认情况下,XGBClassifier 或许多分类器使用 objective 作为 binary 但它在内部所做的是分类(一个与其余)即如果你有 3 类 它将给出结果(0 vs 1&2)。如果你处理的超过 2 类,你应该总是使用 softmax。Softmax 将 logits 转化为概率,这将求和 1.On 在此基础上,它做出 类 具有最高 probabilities.As 的预测你可以看到复杂性增加,如 Saurabh 在他的回答所以需要更多时间。
其实即使XGBClassifier
默认的obj参数是binary:logistic
,内部也会判断标签y的class的个数。当class数大于2时,会修改obj参数为multi:softmax
.
https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py
class XGBClassifier(XGBModel, XGBClassifierBase):
# pylint: disable=missing-docstring,invalid-name,too-many-instance-attributes
def __init__(self, objective="binary:logistic", **kwargs):
super().__init__(objective=objective, **kwargs)
def fit(self, X, y, sample_weight=None, base_margin=None,
eval_set=None, eval_metric=None,
early_stopping_rounds=None, verbose=True, xgb_model=None,
sample_weight_eval_set=None, callbacks=None):
# pylint: disable = attribute-defined-outside-init,arguments-differ
evals_result = {}
self.classes_ = np.unique(y)
self.n_classes_ = len(self.classes_)
xgb_options = self.get_xgb_params()
if callable(self.objective):
obj = _objective_decorator(self.objective)
# Use default value. Is it really not used ?
xgb_options["objective"] = "binary:logistic"
else:
obj = None
if self.n_classes_ > 2:
# Switch to using a multiclass objective in the underlying
# XGB instance
xgb_options['objective'] = 'multi:softprob'
xgb_options['num_class'] = self.n_classes_