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

Suppress categorical warning (fixes #3379) #4762

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,9 @@ def _lazy_init(self, data, label=None, reference=None,
if categorical_indices:
for cat_alias in _ConfigAliases.get("categorical_feature"):
if cat_alias in params:
_log_warning(f'{cat_alias} in param dict is overridden.')
# If the params[cat_alias] is equal to categorical_indices, do not report the warning.
if not(isinstance(params[cat_alias], list) and set(params[cat_alias]) == categorical_indices):
_log_warning(f'{cat_alias} in param dict is overridden.')
params.pop(cat_alias, None)
params['categorical_column'] = sorted(categorical_indices)

Expand Down Expand Up @@ -1765,6 +1767,32 @@ def __init_from_csc(self, csc, params_str, ref_dataset):
ctypes.byref(self.handle)))
return self

@staticmethod
def _compare_params_for_warning(params, other_params):
"""Compare params.

It is only for the warning purpose. Thus some keys are ignored.

Returns
-------
compare_result: bool
If they are equal, return True; Otherwise, return False.
"""
ignore_keys = _ConfigAliases.get("categorical_feature")
if params is None:
params = {}
if other_params is None:
other_params = {}
for k in other_params:
if k not in ignore_keys:
if k not in params or params[k] != other_params[k]:
return False
for k in params:
if k not in ignore_keys:
if k not in other_params or params[k] != other_params[k]:
return False
return True

def construct(self):
"""Lazy init.

Expand All @@ -1776,8 +1804,10 @@ def construct(self):
if self.handle is None:
if self.reference is not None:
reference_params = self.reference.get_params()
if self.get_params() != reference_params:
_log_warning('Overriding the parameters from Reference Dataset.')
params = self.get_params()
if params != reference_params:
if self._compare_params_for_warning(params, reference_params) is False:
Copy link
Collaborator

Choose a reason for hiding this comment

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

According to get_params, both params and reference_params can be None. In this case they cannot be directly iterated in the _compare_params_for_warning method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks shiyu. I also found this issue in the CI. Will add a check on this.

_log_warning('Overriding the parameters from Reference Dataset.')
self._update_params(reference_params)
if self.used_indices is None:
# create valid
Expand Down Expand Up @@ -2062,11 +2092,11 @@ def set_categorical_feature(self, categorical_feature):
self.categorical_feature = categorical_feature
return self._free_handle()
elif categorical_feature == 'auto':
_log_warning('Using categorical_feature in Dataset.')
return self
else:
_log_warning('categorical_feature in Dataset is overridden.\n'
f'New categorical_feature is {sorted(list(categorical_feature))}')
if self.categorical_feature != 'auto':
_log_warning('categorical_feature in Dataset is overridden.\n'
f'New categorical_feature is {sorted(list(categorical_feature))}')
self.categorical_feature = categorical_feature
return self._free_handle()
else:
Expand Down