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

Skip to content

MNT Adjust code after NEP 51 numpy scalar formatting changes #27042

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 7 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion sklearn/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def fit(self, X, y, sample_weight=None):
"The constant target value must be present in "
"the training data. You provided constant={}. "
"Possible values are: {}.".format(
self.constant, list(self.classes_[k])
self.constant, self.classes_[k].tolist()
)
)
raise ValueError(err_msg)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/impute/tests/test_impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def test_imputation_median_special_cases():
@pytest.mark.parametrize("dtype", [None, object, str])
def test_imputation_mean_median_error_invalid_type(strategy, dtype):
X = np.array([["a", "b", 3], [4, "e", 6], ["g", "h", 9]], dtype=dtype)
msg = "non-numeric data:\ncould not convert string to float: '"
msg = "non-numeric data:\ncould not convert string to float:"
Copy link
Member Author

Choose a reason for hiding this comment

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

I remember here why I removed the quote here. We reraise an error from numpy ... we could also rewrite the error message if we think this is really important.

raise new_ve from None

In [1]: import numpy as np

In [2]: np.array(['a']).astype(np.float64)
--------------------------------------------------------------------------
ValueError                               Traceback (most recent call last)
Cell In[2], line 1
----> 1 np.array(['a']).astype(np.float64)

ValueError: could not convert string to float: np.str_('a')

Copy link
Member

Choose a reason for hiding this comment

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

I think rewriting the error message will be confusing for the user. Not sure, but it somehow feels wrong to modify an exception that we don't generate.

Copy link
Member Author

Choose a reason for hiding this comment

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

I remember here why I removed the quote here. We reraise an error from numpy ... we could also rewrite the error message if we think this is really important.

raise new_ve from None

In [1]: import numpy as np

In [2]: np.array(['a']).astype(np.float64)
--------------------------------------------------------------------------
ValueError                               Traceback (most recent call last)
Cell In[2], line 1
----> 1 np.array(['a']).astype(np.float64)

ValueError: could not convert string to float: np.str_('a')

with pytest.raises(ValueError, match=msg):
imputer = SimpleImputer(strategy=strategy)
imputer.fit_transform(X)
Expand All @@ -272,7 +272,7 @@ def test_imputation_mean_median_error_invalid_type_list_pandas(strategy, type):
if type == "dataframe":
pd = pytest.importorskip("pandas")
X = pd.DataFrame(X)
msg = "non-numeric data:\ncould not convert string to float: '"
msg = "non-numeric data:\ncould not convert string to float:"
with pytest.raises(ValueError, match=msg):
imputer = SimpleImputer(strategy=strategy)
imputer.fit_transform(X)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/preprocessing/_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ def _map_drop_idx_to_infrequent(self, feature_idx, drop_idx):
if infrequent_indices is not None and drop_idx in infrequent_indices:
categories = self.categories_[feature_idx]
raise ValueError(
f"Unable to drop category {categories[drop_idx]!r} from feature"
f" {feature_idx} because it is infrequent"
f"Unable to drop category {categories[drop_idx].item()!r} from"
f" feature {feature_idx} because it is infrequent"
)
return default_to_infrequent[drop_idx]

Expand Down
4 changes: 3 additions & 1 deletion sklearn/utils/class_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def compute_class_weight(class_weight, *, classes, y):

n_weighted_classes = len(classes) - len(unweighted_classes)
if unweighted_classes and n_weighted_classes != len(class_weight):
unweighted_classes_user_friendly_str = np.array(unweighted_classes).tolist()
raise ValueError(
f"The classes, {unweighted_classes}, are not in class_weight"
f"The classes, {unweighted_classes_user_friendly_str}, are not in"
" class_weight"
)

return weight
Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ def _check_pos_label_consistency(pos_label, y_true):
or np.array_equal(classes, [1])
)
):
classes_repr = ", ".join(repr(c) for c in classes)
classes_repr = ", ".join([repr(c) for c in classes.tolist()])
raise ValueError(
f"y_true takes value in {{{classes_repr}}} and pos_label is not "
"specified: either make y_true take value in {0, 1} or "
Expand Down