Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: Make compute_sample_weight work on sparse target - #23115

Merged
glemaitre merged 6 commits into
scikit-learn:mainfrom
kernc:compute_sample_weight+sparse
May 6, 2022
Merged

FIX: Make compute_sample_weight work on sparse target#23115
glemaitre merged 6 commits into
scikit-learn:mainfrom
kernc:compute_sample_weight+sparse

Conversation

@kernc

@kernc kernc commented Apr 12, 2022

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

What does this implement/fix? Explain your changes.

This PR fixes compute_sample_weight() to work with sparse arrays, so that the models that take class_weight="balanced" don't crash, for example:

import numpy as np
from scipy import sparse
from sklearn.linear_model import RidgeClassifier
X = np.array([[1, 2],
              [2, 3]])
y = sparse.csr_matrix(np.array([[0, 1],
                                [1, 0]]))
clf = RidgeClassifier(class_weight='balanced')
clf.fit(X, y)
-------------------------------------------------
File ./sklearn/utils/class_weight.py:40, in compute_class_weight(class_weight, classes, y)
     38 from ..preprocessing import LabelEncoder
---> 40 if set(y) - set(classes):
     41     raise ValueError("classes should include all valid labels that can be in y")

TypeError: unhashable type: 'csr_matrix'

Sparse targets are frequent in multi-label learning.

Any other comments?

Sparse zeros in target are considered a separate class.

@kernc
kernc force-pushed the compute_sample_weight+sparse branch from b64ed29 to d5e07b1 Compare April 12, 2022 00:52

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for the fix!

Comment thread sklearn/utils/tests/test_class_weight.py Outdated
Comment thread sklearn/utils/tests/test_class_weight.py Outdated
Comment thread doc/whats_new/v1.1.rst Outdated
:func:`~sklearn.set_config`.
:pr:`22856` by `Jérémie du Boisberranger <jeremiedbb>`.

- |Fix| :func:`utils.class_weight.compute_sample_weight` now works with sparse `y`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's move the entry in 1.1.1 that will be the bug release that comes just right after 1.1.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jeremiedbb will maybe move this when preparing the final release.

@glemaitre glemaitre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks @kernc

Comment thread sklearn/utils/tests/test_class_weight.py Outdated
@glemaitre
glemaitre merged commit 00c1363 into scikit-learn:main May 6, 2022
@glemaitre

Copy link
Copy Markdown
Member

CIs are green. Thanks @kernc. Merging.

glemaitre added a commit to glemaitre/scikit-learn that referenced this pull request May 19, 2022
glemaitre added a commit that referenced this pull request May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants