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

Code before:

    Returns
    -------
    sample_weight : ndarray of shape (n_samples,)
        Validated sample weight. It is guaranteed to be "C" contiguous.
    """
    xp, _, device = get_namespace_and_device(X, remove_types=(int, float))

    n_samples = _num_samples(X)

    max_float_type = _max_precision_float_dtype(xp, device)
    float_dtypes = (
        [xp.float32] if max_float_type == xp.float32 else [xp.float64, xp.float32]
    )
    if force_float_dtype and dtype is not None and dtype not in float_dtypes:
        dtype = max_float_type

    if sample_weight is None:
        sample_weight = xp.ones(n_samples, dtype=dtype, device=device)
    elif isinstance(sample_weight, numbers.Number):
        sample_weight = xp.full(n_samples, sample_weight, dtype=dtype, device=device)
    else:
        if force_float_dtype and dtype is None:
            dtype = float_dtypes
        if ensure_same_device:
            sample_weight = xp.asarray(sample_weight, device=device)
        sample_weight = check_array(
            sample_weight,
            accept_sparse=False,
            ensure_2d=False,
            dtype=dtype,
            order="C",
            copy=copy,
            input_name="sample_weight",
        )
        if sample_weight.ndim != 1:
            raise ValueError(
                f"Sample weights must be 1D array or scalar, got "
                f"{sample_weight.ndim}D array. Expected either a scalar value "
                f"or a 1D array of length {n_samples}."
            )

        if sample_weight.shape != (n_samples,):
            raise ValueError(
                "sample_weight.shape == {}, expected {}!".format(
                    sample_weight.shape, (n_samples,)
                )
            )

    if ensure_non_negative:
        check_non_negative(sample_weight, "`sample_weight`")

Review comment (suggestion):
We can probably do the dtype conversion there as well, no? That might spare a temporary allocation on the device in some cases.


```suggestion
            sample_weight = xp.asarray(sample_weight, dtype=dtype, device=device)
```

Code after:

    Returns
    -------
    sample_weight : ndarray of shape (n_samples,)
        Validated sample weight. It is guaranteed to be "C" contiguous.
    """
    xp, is_array_api, device = get_namespace_and_device(X, remove_types=(int, float))

    n_samples = _num_samples(X)

    max_float_type = _max_precision_float_dtype(xp, device)
    float_dtypes = (
        [xp.float32] if max_float_type == xp.float32 else [xp.float64, xp.float32]
    )
    if force_float_dtype and dtype is not None and dtype not in float_dtypes:
        dtype = max_float_type

    if sample_weight is None:
        sample_weight = xp.ones(n_samples, dtype=dtype, device=device)
    elif isinstance(sample_weight, numbers.Number):
        sample_weight = xp.full(n_samples, sample_weight, dtype=dtype, device=device)
    else:
        if force_float_dtype and dtype is None:
            dtype = float_dtypes
        if is_array_api and ensure_same_device:
            sample_weight = xp.asarray(sample_weight, device=device)
        sample_weight = check_array(
            sample_weight,
            accept_sparse=False,
            ensure_2d=False,
            dtype=dtype,
            order="C",
            copy=copy,
            input_name="sample_weight",
        )
        if sample_weight.ndim != 1:
            raise ValueError(
                f"Sample weights must be 1D array or scalar, got "
                f"{sample_weight.ndim}D array. Expected either a scalar value "
                f"or a 1D array of length {n_samples}."
            )

        if sample_weight.shape != (n_samples,):
            raise ValueError(
                "sample_weight.shape == {}, expected {}!".format(
                    sample_weight.shape, (n_samples,)
                )
            )

    if ensure_non_negative:
        check_non_negative(sample_weight, "`sample_weight`")