Repository: scikit-learn/scikit-learn
File: sklearn/utils/estimator_checks.py (Python)

Code before:
    y = _enforce_estimator_tags_y(estimator, y)
    sample_weight = [3] * n_samples
    # Test that estimators don't raise any exception
    estimator.fit(X, y, sample_weight=sample_weight)


def check_all_zero_sample_weights_error(name, estimator_orig):
    """Check that estimator raises error when all sample weights are 0."""
    estimator = clone(estimator_orig)

    X, y = make_classification(random_state=42)
    X = _enforce_estimator_tags_X(estimator, X)
    y = _enforce_estimator_tags_y(estimator, y)

    sample_weight = np.zeros(_num_samples(X))

    """The following estimators have custom error messages:

    NuSVC: Invalid input - all samples have zero or negative weights.

    Perceptron: The sample weights for validation set are all zero, consider using a
    different random state.

    SGDClassifier: The sample weights for validation set are all zero, consider using a
    different random state.
    """
    with raises(ValueError, match=r"(.*weight.*zero.*)|(.*zero.*weight.*)"):
        estimator.fit(X, y, sample_weight=sample_weight)


@ignore_warnings(category=FutureWarning)
def check_sample_weights_shape(name, estimator_orig):
    # check that estimators raise an error if sample_weight
    # shape mismatches the input
    estimator = clone(estimator_orig)
    X = np.array(
        [
            [1, 3],
            [1, 3],
            [1, 3],
            [1, 3],
            [2, 1],
            [2, 1],
            [2, 1],
            [2, 1],
            [3, 3],
            [3, 3],
            [3, 3],
            [3, 3],
            [4, 1],
            [4, 1],

Review comment (suggestion):
```suggestion
    # The following estimators have custom error messages:
    # - NuSVC: Invalid input - all samples have zero or negative weights.
    # - Perceptron: The sample weights for validation set are all zero, consider using
    #   a different random state.
    # - SGDClassifier: The sample weights for validation set are all zero, consider
    #   using a different random state.
```

Code after:
            [3, 1],
            [3, 2],
            [3, 3],
            [3, 4],
        ]
    )
    X = _NotAnArray(_enforce_estimator_tags_X(estimator_orig, X))
    y = _NotAnArray([1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2])
    weights = _NotAnArray([1] * 12)
    tags = get_tags(estimator)
    if not tags.target_tags.single_output and tags.target_tags.multi_output:
        y = _NotAnArray(y.data.reshape(-1, 1))
    estimator.fit(X, y, sample_weight=weights)


@ignore_warnings(category=(FutureWarning))
def check_sample_weights_list(name, estimator_orig):
    # check that estimators will accept a 'sample_weight' parameter of
    # type list in the 'fit' function.
    estimator = clone(estimator_orig)
    rnd = np.random.RandomState(0)
    n_samples = 30
    X = _enforce_estimator_tags_X(estimator_orig, rnd.uniform(size=(n_samples, 3)))
    y = np.arange(n_samples) % 3
    y = _enforce_estimator_tags_y(estimator, y)
    sample_weight = [3] * n_samples
    # Test that estimators don't raise any exception
    estimator.fit(X, y, sample_weight=sample_weight)


@ignore_warnings(category=FutureWarning)
def check_all_zero_sample_weights_error(name, estimator_orig):
    """Check that estimator raises error when all sample weights are 0."""
    estimator = clone(estimator_orig)

    X, y = make_classification(random_state=42)
    X = _enforce_estimator_tags_X(estimator, X)
    y = _enforce_estimator_tags_y(estimator, y)

    sample_weight = np.zeros(_num_samples(X))

    # The following estimators have custom error messages:
    # - NuSVC: Invalid input - all samples have zero or negative weights.
    # - Perceptron: The sample weights for validation set are all zero, consider using
    #   a different random state.
    # - SGDClassifier: The sample weights for validation set are all zero, consider
    #   using a different random state.
    # All other estimators: Sample weights must contain at least one non-zero number.
    with raises(ValueError, match=r"(.*weight.*zero.*)|(.*zero.*weight.*)"):
        estimator.fit(X, y, sample_weight=sample_weight)