Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python][tests] minor Python tests cleanup #3860

Merged
merged 15 commits into from
Jan 26, 2021
Merged

[python][tests] minor Python tests cleanup #3860

merged 15 commits into from
Jan 26, 2021

Conversation

StrikerRUS
Copy link
Collaborator

Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@StrikerRUS StrikerRUS merged commit 9eeac3c into master Jan 26, 2021
@StrikerRUS StrikerRUS deleted the py_tests branch January 26, 2021 19:46
@wyfSunflower
Copy link

Failed to run test_sklearn.py with scikit-learn 0.23.1 in python 3.8 on Ubuntu 20
图片
test_sklearn.py::TestSklearn::test_check_is_fitted FAILED [100%]
test_sklearn.py:1095 (TestSklearn.test_check_is_fitted)
self = <test_sklearn.TestSklearn testMethod=test_check_is_fitted>

@unittest.skipIf(sk_version < '0.22.0', 'scikit-learn version is less than 0.22')
def test_check_is_fitted(self):
    X, y = load_digits(n_class=2, return_X_y=True)
    est = lgb.LGBMModel(n_estimators=5, objective="binary")
    clf = lgb.LGBMClassifier(n_estimators=5)
    reg = lgb.LGBMRegressor(n_estimators=5)
    rnk = lgb.LGBMRanker(n_estimators=5)
    models = (est, clf, reg, rnk)
    for model in models:
        self.assertRaises(lgb.compat.LGBMNotFittedError,
                          check_is_fitted,
                          model)
    est.fit(X, y)
    clf.fit(X, y)
    reg.fit(X, y)
    rnk.fit(X, y, group=np.ones(X.shape[0]))
    for model in models:
      check_is_fitted(model)

test_sklearn.py:1113:


../../../../anaconda3/envs/py38/lib/python3.8/site-packages/sklearn/utils/validation.py:73: in inner_f
return f(**kwargs)


estimator = LGBMModel(n_estimators=5, objective='binary'), attributes = None

@_deprecate_positional_args
def check_is_fitted(estimator, attributes=None, *, msg=None, all_or_any=all):
    """Perform is_fitted validation for estimator.

    Checks if the estimator is fitted by verifying the presence of
    fitted attributes (ending with a trailing underscore) and otherwise
    raises a NotFittedError with the given message.

    This utility is meant to be used internally by estimators themselves,
    typically in their own predict / transform methods.

    Parameters
    ----------
    estimator : estimator instance.
        estimator instance for which the check is performed.

    attributes : str, list or tuple of str, default=None
        Attribute name(s) given as string or a list/tuple of strings
        Eg.: ``["coef_", "estimator_", ...], "coef_"``

        If `None`, `estimator` is considered fitted if there exist an
        attribute that ends with a underscore and does not start with double
        underscore.

    msg : string
        The default error message is, "This %(name)s instance is not fitted
        yet. Call 'fit' with appropriate arguments before using this
        estimator."

        For custom messages if "%(name)s" is present in the message string,
        it is substituted for the estimator name.

        Eg. : "Estimator, %(name)s, must be fitted before sparsifying".

    all_or_any : callable, {all, any}, default all
        Specify whether all or any of the given attributes must exist.

    Returns
    -------
    None

    Raises
    ------
    NotFittedError
        If the attributes are not found.
    """
    if isclass(estimator):
        raise TypeError("{} is a class, not an instance.".format(estimator))
    if msg is None:
        msg = ("This %(name)s instance is not fitted yet. Call 'fit' with "
               "appropriate arguments before using this estimator.")

    if not hasattr(estimator, 'fit'):
        raise TypeError("%s is not an estimator instance." % (estimator))

    if attributes is not None:
        if not isinstance(attributes, (list, tuple)):
            attributes = [attributes]
        attrs = all_or_any([hasattr(estimator, attr) for attr in attributes])
    else:
        attrs = [v for v in vars(estimator)
                 if v.endswith("_") and not v.startswith("__")]

    if not attrs:
      raise NotFittedError(msg % {'name': type(estimator).__name__})

E sklearn.exceptions.NotFittedError: This LGBMModel instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

../../../../anaconda3/envs/py38/lib/python3.8/site-packages/sklearn/utils/validation.py:1020: NotFittedError

Assertion failed

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants