英文:
How to differentiate model types from xgboost XGBRFClassifier and XGBClassifier
问题
我正在构建一个支持xgboost
中的XGBClassifier
和XGBRFClassifier
的模块。我想要在我的模块中区分它们,但我注意到:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论