-
-
Notifications
You must be signed in to change notification settings - Fork 26k
MNT Move entropy
to private function
#31294
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
90ae669
rm entropy
lucyleeow c93e951
Merge branch 'main' into doc_entropy
lucyleeow 0fb9aeb
make private
lucyleeow bc84d0a
fix
lucyleeow d949916
rm from cluster
lucyleeow 160ea9f
fix import
lucyleeow dedf049
deprecate
lucyleeow 267eb27
add test
lucyleeow 0488045
amend message
lucyleeow 58f272e
Merge branch 'main' into doc_entropy
lucyleeow a83a469
review
lucyleeow 19caca6
use same docstring for the public function
jeremiedbb 810b75b
add whatsnew
lucyleeow 7d07e09
fix whats new
lucyleeow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- :func:`metrics.cluster.entropy` is deprecated and will be removed in v1.10. | ||
By :user:`Lucy Liu <lucyleeow>` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import numpy as np | ||
from scipy import sparse as sp | ||
|
||
from ...utils import deprecated | ||
from ...utils._array_api import _max_precision_float_dtype, get_namespace_and_device | ||
from ...utils._param_validation import Hidden, Interval, StrOptions, validate_params | ||
from ...utils.multiclass import type_of_target | ||
|
@@ -530,8 +531,8 @@ def homogeneity_completeness_v_measure(labels_true, labels_pred, *, beta=1.0): | |
if len(labels_true) == 0: | ||
return 1.0, 1.0, 1.0 | ||
|
||
entropy_C = entropy(labels_true) | ||
entropy_K = entropy(labels_pred) | ||
entropy_C = _entropy(labels_true) | ||
entropy_K = _entropy(labels_pred) | ||
|
||
contingency = contingency_matrix(labels_true, labels_pred, sparse=True) | ||
MI = mutual_info_score(None, None, contingency=contingency) | ||
|
@@ -1042,7 +1043,7 @@ def adjusted_mutual_info_score( | |
# Calculate the expected value for the mutual information | ||
emi = expected_mutual_information(contingency, n_samples) | ||
# Calculate entropy for each labeling | ||
h_true, h_pred = entropy(labels_true), entropy(labels_pred) | ||
h_true, h_pred = _entropy(labels_true), _entropy(labels_pred) | ||
normalizer = _generalized_average(h_true, h_pred, average_method) | ||
denominator = normalizer - emi | ||
# Avoid 0.0 / 0.0 when expectation equals maximum, i.e. a perfect match. | ||
|
@@ -1168,7 +1169,7 @@ def normalized_mutual_info_score( | |
return 0.0 | ||
|
||
# Calculate entropy for each labeling | ||
h_true, h_pred = entropy(labels_true), entropy(labels_pred) | ||
h_true, h_pred = _entropy(labels_true), _entropy(labels_pred) | ||
|
||
normalizer = _generalized_average(h_true, h_pred, average_method) | ||
return float(mi / normalizer) | ||
|
@@ -1272,13 +1273,7 @@ def fowlkes_mallows_score(labels_true, labels_pred, *, sparse="deprecated"): | |
return float(np.sqrt(tk / pk) * np.sqrt(tk / qk)) if tk != 0.0 else 0.0 | ||
|
||
|
||
@validate_params( | ||
{ | ||
"labels": ["array-like"], | ||
}, | ||
prefer_skip_nested_validation=True, | ||
) | ||
def entropy(labels): | ||
def _entropy(labels): | ||
"""Calculate the entropy for a labeling. | ||
|
||
Parameters | ||
|
@@ -1312,3 +1307,25 @@ def entropy(labels): | |
# Always convert the result as a Python scalar (on CPU) instead of a device | ||
# specific scalar array. | ||
return float(-xp.sum((pi / pi_sum) * (xp.log(pi) - log(pi_sum)))) | ||
|
||
|
||
# TODO(1.10): Remove | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am assuming this won't make it into 1.7, so deprecate 1.8 / remove 1.10 ? |
||
@deprecated("`entropy` is deprecated in 1.8 and will be removed in 1.10.") | ||
def entropy(labels): | ||
"""Calculate the entropy for a labeling. | ||
|
||
Parameters | ||
---------- | ||
labels : array-like of shape (n_samples,), dtype=int | ||
The labels. | ||
|
||
Returns | ||
------- | ||
entropy : float | ||
The entropy for a labeling. | ||
|
||
Notes | ||
----- | ||
The logarithm used is the natural logarithm (base-e). | ||
""" | ||
return _entropy(labels) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.