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

Code before:
    assert_allclose(_convert_to_numpy(sample_weight, xp), 2 * np.ones(5))

    # check wrong number of dimensions
    with pytest.raises(ValueError, match=r"Sample weights must be 1D array or scalar"):
        _check_sample_weight(xp.ones((2, 4)), X=xp.ones((2, 2)))

    # check incorrect n_samples
    msg = re.escape(f"sample_weight.shape == {xp.ones(4).shape}, expected (2,)!")
    with pytest.raises(ValueError, match=msg):
        _check_sample_weight(xp.ones(4), X=xp.ones((2, 2)))

    # float32 dtype is preserved
    X = xp.ones((5, 2))
    sample_weight = xp.ones(5, dtype=xp.float32)
    sample_weight = _check_sample_weight(sample_weight, X)
    assert sample_weight.dtype == xp.float32

    # check negative weight when ensure_non_negative=True
    X = xp.ones((5, 2))
    sample_weight = xp.ones(_num_samples(X))
    sample_weight[-1] = -10
    err_msg = "Negative values in data passed to `sample_weight`"
    with pytest.raises(ValueError, match=err_msg):
        _check_sample_weight(sample_weight, X, ensure_non_negative=True)

    # check all zero weights when allow_zero_weights=False
    X = xp.ones((5, 2))
    sample_weight = xp.zeros(_num_samples(X))
    err_msg = "Sample weights must contain at least one non-zero number."
    with pytest.raises(ValueError, match=err_msg):
        _check_sample_weight(sample_weight, X, allow_zero_weights=False)


def test_check_sample_weight():
    # check array order
    sample_weight = np.ones(10)[::2]
    assert not sample_weight.flags["C_CONTIGUOUS"]
    sample_weight = _check_sample_weight(sample_weight, X=np.ones((5, 1)))
    assert sample_weight.flags["C_CONTIGUOUS"]

    _check_sample_weight_common(np)

    # int dtype will be converted to float64 instead
    X = np.ones((5, 2), dtype=int)
    sample_weight = _check_sample_weight(None, X, dtype=X.dtype)
    assert sample_weight.dtype == np.float64


@pytest.mark.parametrize(
    "array_namespace,device,dtype", yield_namespace_device_dtype_combinations()
)

Review comment (suggestion):
```suggestion
    # check error raised when allow_zero_weights=False
```

Code after:
    with pytest.raises(ValueError, match=r"Sample weights must be 1D array or scalar"):
        _check_sample_weight(xp.ones((2, 4)), X=xp.ones((2, 2)))

    # check incorrect n_samples
    msg = re.escape(f"sample_weight.shape == {xp.ones(4).shape}, expected (2,)!")
    with pytest.raises(ValueError, match=msg):
        _check_sample_weight(xp.ones(4), X=xp.ones((2, 2)))

    # float32 dtype is preserved
    X = xp.ones((5, 2))
    sample_weight = xp.ones(5, dtype=xp.float32)
    sample_weight = _check_sample_weight(sample_weight, X)
    assert sample_weight.dtype == xp.float32

    # check negative weight when ensure_non_negative=True
    X = xp.ones((5, 2))
    sample_weight = xp.ones(_num_samples(X))
    sample_weight[-1] = -10
    err_msg = "Negative values in data passed to `sample_weight`"
    with pytest.raises(ValueError, match=err_msg):
        _check_sample_weight(sample_weight, X, ensure_non_negative=True)

    # check error raised when allow_all_zero_weights=False
    X = xp.ones((5, 2))
    sample_weight = xp.zeros(_num_samples(X))
    err_msg = "Sample weights must contain at least one non-zero number."
    with pytest.raises(ValueError, match=err_msg):
        _check_sample_weight(sample_weight, X, allow_all_zero_weights=False)


def test_check_sample_weight():
    # check array order
    sample_weight = np.ones(10)[::2]
    assert not sample_weight.flags["C_CONTIGUOUS"]
    sample_weight = _check_sample_weight(sample_weight, X=np.ones((5, 1)))
    assert sample_weight.flags["C_CONTIGUOUS"]

    _check_sample_weight_common(np)

    # int dtype will be converted to float64 instead
    X = np.ones((5, 2), dtype=int)
    sample_weight = _check_sample_weight(None, X, dtype=X.dtype)
    assert sample_weight.dtype == np.float64


@pytest.mark.parametrize(
    "array_namespace,device,dtype", yield_namespace_device_dtype_combinations()
)
def test_check_sample_weight_array_api(array_namespace, device, dtype):
    xp = _array_api_for_tests(array_namespace, device)
    with config_context(array_api_dispatch=True):