Xgboost: check_estimator fails for XGBClassifier

Created on 6 May 2020  路  2Comments  路  Source: dmlc/xgboost

The following checks fail for me using xgboost==1.0.2:

from xgboost import XGBClassifier
from sklearn.utils.estimator_checks import check_estimator
check_estimator(XGBClassifier)

This means that XGBClassifier doesn't fully comply with the sklearn estimator API, so some functionality in sklearn (like gridsearches) may not work properly or give unexpected results. See here for more info from the sklearn docs.

Is this something which has been encountered before? I'm happy to have a go at a PR for this, unless it is something which is known and has been decided intentionally (for example to support **kwargs in the classifier).

Most helpful comment

@hcho3 It's actually possible but with some tricks (pass all but one-two checks from all that come from check_estimator()). Refer to https://github.com/microsoft/LightGBM/blob/18c706dc840c72341a2a85e05dfb5fad6366902a/tests/python_package_test/test_sklearn.py#L284-L305

Also, in recent versions of scikit-learn it is possible to use generators or parametrization for checks: https://github.com/microsoft/LightGBM/issues/2628#issuecomment-570532274 and https://github.com/microsoft/LightGBM/issues/2947 respectively.

But another problem is that even passing that test is not sufficient to be sure that your estimator is fully compatible with scikit-learn. Refer to https://github.com/scikit-learn/scikit-learn/issues/15392#issuecomment-554780779 and https://github.com/microsoft/LightGBM/issues/2966#issuecomment-611521270.

All 2 comments

It is actually not possible to pass check_estimator() check entirely, because XGBoost allows NaN as part of input whereas scikit-learn estimators do not. I tried complying with the check in https://github.com/dmlc/xgboost/pull/3674 but found it untenable, so I reverted it.

@hcho3 It's actually possible but with some tricks (pass all but one-two checks from all that come from check_estimator()). Refer to https://github.com/microsoft/LightGBM/blob/18c706dc840c72341a2a85e05dfb5fad6366902a/tests/python_package_test/test_sklearn.py#L284-L305

Also, in recent versions of scikit-learn it is possible to use generators or parametrization for checks: https://github.com/microsoft/LightGBM/issues/2628#issuecomment-570532274 and https://github.com/microsoft/LightGBM/issues/2947 respectively.

But another problem is that even passing that test is not sufficient to be sure that your estimator is fully compatible with scikit-learn. Refer to https://github.com/scikit-learn/scikit-learn/issues/15392#issuecomment-554780779 and https://github.com/microsoft/LightGBM/issues/2966#issuecomment-611521270.

Was this page helpful?
0 / 5 - 0 ratings