diff --git a/sklearn/dummy.py b/sklearn/dummy.py index 1db664826f5c9..63318b07ce580 100644 --- a/sklearn/dummy.py +++ b/sklearn/dummy.py @@ -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) diff --git a/sklearn/impute/tests/test_impute.py b/sklearn/impute/tests/test_impute.py index 936847e55e324..9d02eb7722cfc 100644 --- a/sklearn/impute/tests/test_impute.py +++ b/sklearn/impute/tests/test_impute.py @@ -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:" with pytest.raises(ValueError, match=msg): imputer = SimpleImputer(strategy=strategy) imputer.fit_transform(X) @@ -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) diff --git a/sklearn/preprocessing/_encoders.py b/sklearn/preprocessing/_encoders.py index 4cdd864e92c81..8f441907d5471 100644 --- a/sklearn/preprocessing/_encoders.py +++ b/sklearn/preprocessing/_encoders.py @@ -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] diff --git a/sklearn/utils/class_weight.py b/sklearn/utils/class_weight.py index 0b59f63190c3b..19e7bcb7ba17a 100644 --- a/sklearn/utils/class_weight.py +++ b/sklearn/utils/class_weight.py @@ -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 diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index a37aef144f332..20e878256803b 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -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 "