code: def _check_sample_weight(
      sample_weight, X, dtype=None, copy=False, only_non_negative=False
  ):
      """Validate sample weights.
  
      Note that passing sample_weight=None will output an array of ones.
      Therefore, in some cases, you may want to protect the call with:
      if sample_weight is not None:
          sample_weight = _check_sample_weight(...)
  
      Parameters
      ----------
      sample_weight : {ndarray, Number or None}, shape (n_samples,)
          Input sample weights.
  
      X : {ndarray, list, sparse matrix}
          Input data.
  
      only_non_negative : bool, default=False,
          Whether or not the weights are expected to be non-negative.
  
          .. versionadded:: 1.0
  
      dtype : dtype, default=None
          dtype of the validated `sample_weight`.
          If None, and the input `sample_weight` is an array, the dtype of the
          input is preserved; otherwise an array with the default numpy dtype
          is be allocated.  If `dtype` is not one of `float32`, `float64`,
          `None`, the output will be of dtype `float64`.
  
      copy : bool, default=False
          If True, a copy of sample_weight will be created.
  
      Returns
      -------
      sample_weight : ndarray of shape (n_samples,)
          Validated sample weight. It is guaranteed to be "C" contiguous.
      """
      n_samples = _num_samples(X)
  
      if dtype is not None and dtype not in [np.float32, np.float64]:
          dtype = np.float64
  
      if sample_weight is None:
          sample_weight = np.ones(n_samples, dtype=dtype)
      elif isinstance(sample_weight, numbers.Number):
          sample_weight = np.full(n_samples, sample_weight, dtype=dtype)
      else:
          if dtype is None:
              dtype = [np.float64, np.float32]
          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(
                  "Sample weights must be 1D array or scalar"
              )
  
          if sample_weight.shape != (n_samples,):
              raise ValueError(
                  "sample_weight.shape == {}, expected {}!".format(
                      sample_weight.shape, (n_samples,)
                  )
              )
  
      if only_non_negative:
          check_non_negative(sample_weight, "`sample_weight`")
  
      return sample_weight
  
imports: import numbers
  import operator
  import warnings
  from contextlib import suppress
  from functools import reduce, wraps
  from inspect import Parameter, isclass, signature
  import joblib
  import numpy
  import scipy.sparse
  from ..None import get_config
  from ..exceptions import DataConversionWarning, NotFittedError, PositiveSpectrumWarning
  from ..utils._array_api import _asarray_with_order, _is_numpy_namespace, get_namespace
  from ..utils.fixes import ComplexWarning
  from ._isfinite import FiniteStatus, cy_isfinite
  from .fixes import _object_dtype_isnan
label_desc: Use image node_id 32 for calling a global function with example usage: _check_sample_weight(sample_weight, X, dtype, copy, only_non_negative) and returns: sample_weight