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

FIX changed_only=True with kwargs parameters #17205

Merged
merged 7 commits into from May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions sklearn/utils/_pprint.py
Expand Up @@ -66,6 +66,7 @@
from inspect import signature
import pprint
from collections import OrderedDict
from collections import defaultdict

from ..base import BaseEstimator
from .._config import get_config
Expand Down Expand Up @@ -94,6 +95,11 @@ def _changed_params(estimator):
estimator.__init__)
init_params = signature(init_func).parameters
init_params = {name: param.default for name, param in init_params.items()}
# We make init_params a defaultdict to be nice with third party libraries
# that have **kwargs in __init__. The parameters in kwargs would cause a
# KeyError otherwise.
init_params = defaultdict(lambda: None, init_params)
NicolasHug marked this conversation as resolved.
Show resolved Hide resolved

for k, v in params.items():
if (repr(v) != repr(init_params[k]) and
not (is_scalar_nan(init_params[k]) and is_scalar_nan(v))):
Expand Down
16 changes: 16 additions & 0 deletions sklearn/utils/tests/test_pprint.py
Expand Up @@ -2,6 +2,7 @@
from pprint import PrettyPrinter

import numpy as np
import pytest

from sklearn.utils._pprint import _EstimatorPrettyPrinter
from sklearn.linear_model import LogisticRegressionCV
Expand Down Expand Up @@ -538,3 +539,18 @@ def test_builtin_prettyprinter():
# Used to be a bug

PrettyPrinter().pprint(LogisticRegression())

def test_kwargs_in_init():
# Make sure the changed_only=True mode is OK when an argument is passed as
# kwargs.
# Non-regression test for
# https://github.com/scikit-learn/scikit-learn/issues/17206

pytest.importorskip("lightgbm")
from lightgbm import LGBMClassifier # noqa
Copy link
Member

Choose a reason for hiding this comment

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

better to implement something with def __init__(**kw) here??


# metric is part of **kwargs
est = LGBMClassifier(metric='auc', max_depth=10)
Copy link
Member

Choose a reason for hiding this comment

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

should test something=None for non-regression?


expected = "LGBMClassifier(max_depth=10, metric='auc')"
assert expected == est.__repr__()