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
As of XGBoost version v1.7.3, the
XGBRFClassifierclass is a direct subclass of theXGBClassifierclass: https://github.com/dmlc/xgboost/blob/v1.7.3/python-package/xgboost/sklearn.py#L1627Therefore, this
isinstancecheck correctly evaluates toTrue.Your best bet is to perform checks from more specific classes to less specific classes. That is, check for
XGBRFClassifierfirst, and if that evaluates toFalse, only then check forXGBClassifier.