如何区分 xgboost 的 XGBRFClassifier 和 XGBClassifier 模型类型。

huangapple go评论64阅读模式
英文:

How to differentiate model types from xgboost XGBRFClassifier and XGBClassifier

问题

我正在构建一个支持xgboost中的XGBClassifierXGBRFClassifier的模块。我想要在我的模块中区分它们,但我注意到:

from xgboost import XGBRFClassifier, XGBClassifier

xgbrf_model = XGBRFClassifier()
isinstance(xgbrf_model, XGBClassifier) # True

这显然不应该是真的。我想坚持使用isinstance,而不是尝试:

type(xgbrf_model) == XGBClassifier # False
英文:

I'm building a module that supports both XGBClassifier and XGBRFClassifier from xgboost. I want to differentiate them in my module but I noticed that:


from xgboost import XGBRFClassifier, XGBClassifier

xgbrf_model = XGBRFClassifier()
isinstance(xgbrf_model, XGBClassifier) # True

This should obviously not be true. I want to stick to isinstance and not try:

type(xgbrf_model) == XGBClassifier # False

答案1

得分: 1

如下翻译:

截止到XGBoost版本v1.7.3,XGBRFClassifier类是XGBClassifier类的直接子类:
https://github.com/dmlc/xgboost/blob/v1.7.3/python-package/xgboost/sklearn.py#L1627

因此,这个isinstance检查正确地评估为True

最佳做法是从更具体的类检查到不太具体的类。也就是说,首先检查XGBRFClassifier,如果评估为False,然后才检查XGBClassifier

英文:

> This should obviously not be true ..isinstance(XGBRFClassifier(), XGBClassifier)

As of XGBoost version v1.7.3, the XGBRFClassifier class is a direct subclass of the XGBClassifier class:
https://github.com/dmlc/xgboost/blob/v1.7.3/python-package/xgboost/sklearn.py#L1627

Therefore, this isinstance check correctly evaluates to True.

Your best bet is to perform checks from more specific classes to less specific classes. That is, check for XGBRFClassifier first, and if that evaluates to False, only then check for XGBClassifier.

huangapple
  • 本文由 发表于 2023年2月7日 01:14:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定