code: def check_array(
      array,
      accept_sparse=False,
      *,
      accept_large_sparse=True,
      dtype="numeric",
      order=None,
      copy=False,
      force_all_finite=True,
      ensure_2d=True,
      allow_nd=False,
      ensure_min_samples=1,
      ensure_min_features=1,
      estimator=None,
      input_name="",
  ):
      """Input validation on an array, list, sparse matrix or similar.
  
      By default, the input is checked to be a non-empty 2D array containing
      only finite values. If the dtype of the array is object, attempt
      converting to float, raising on failure.
  
      Parameters
      ----------
      array : object
          Input object to check / convert.
  
      accept_sparse : str, bool or list/tuple of str, default=False
          String[s] representing allowed sparse matrix formats, such as 'csc',
          'csr', etc. If the input is sparse but not in the allowed format,
          it will be converted to the first listed format. True allows the input
          to be any format. False means that a sparse matrix input will
          raise an error.
  
      accept_large_sparse : bool, default=True
          If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by
          accept_sparse, accept_large_sparse=False will cause it to be accepted
          only if its indices are stored with a 32-bit dtype.
  
          .. versionadded:: 0.20
  
      dtype : 'numeric', type, list of type or None, default='numeric'
          Data type of result. If None, the dtype of the input is preserved.
          If "numeric", dtype is preserved unless array.dtype is object.
          If dtype is a list of types, conversion on the first type is only
          performed if the dtype of the input is not in the list.
  
      order : {'F', 'C'} or None, default=None
          Whether an array will be forced to be fortran or c-style.
          When order is None (default), then if copy=False, nothing is ensured
          about the memory layout of the output array; otherwise (copy=True)
          the memory layout of the returned array is kept as close as possible
          to the original array.
  
      copy : bool, default=False
          Whether a forced copy will be triggered. If copy=False, a copy might
          be triggered by a conversion.
  
      force_all_finite : bool or 'allow-nan', default=True
          Whether to raise an error on np.inf, np.nan, pd.NA in array. The
          possibilities are:
  
          - True: Force all values of array to be finite.
          - False: accepts np.inf, np.nan, pd.NA in array.
          - 'allow-nan': accepts only np.nan and pd.NA values in array. Values
            cannot be infinite.
  
          .. versionadded:: 0.20
             ``force_all_finite`` accepts the string ``'allow-nan'``.
  
          .. versionchanged:: 0.23
             Accepts `pd.NA` and converts it into `np.nan`
  
      ensure_2d : bool, default=True
          Whether to raise a value error if array is not 2D.
  
      allow_nd : bool, default=False
          Whether to allow array.ndim > 2.
  
      ensure_min_samples : int, default=1
          Make sure that the array has a minimum number of samples in its first
          axis (rows for a 2D array). Setting to 0 disables this check.
  
      ensure_min_features : int, default=1
          Make sure that the 2D array has some minimum number of features
          (columns). The default value of 1 rejects empty datasets.
          This check is only enforced when the input data has effectively 2
          dimensions or is originally 1D and ``ensure_2d`` is True. Setting to 0
          disables this check.
  
      estimator : str or estimator instance, default=None
          If passed, include the name of the estimator in warning messages.
  
      input_name : str, default=""
          The data name used to construct the error message. In particular
          if `input_name` is "X" and the data has NaN values and
          allow_nan is False, the error message will link to the imputer
          documentation.
  
          .. versionadded:: 1.1.0
  
      Returns
      -------
      array_converted : object
          The converted and validated array.
      """
      if isinstance(array, np.matrix):
          raise TypeError(
              "np.matrix is not supported. Please convert to a numpy array with "
              "np.asarray. For more information see: "
              "https://numpy.org/doc/stable/reference/generated/numpy.matrix.html"
          )
  
      xp, is_array_api_compliant = get_namespace(array)
  
      # store reference to original array to check if copy is needed when
      # function returns
      array_orig = array
  
      # store whether originally we wanted numeric dtype
      dtype_numeric = isinstance(dtype, str) and dtype == "numeric"
  
      dtype_orig = getattr(array, "dtype", None)
      if not is_array_api_compliant and not hasattr(dtype_orig, "kind"):
          # not a data type (e.g. a column named dtype in a pandas DataFrame)
          dtype_orig = None
  
      # check if the object contains several dtypes (typically a pandas
      # DataFrame), and store them. If not, store None.
      dtypes_orig = None
      pandas_requires_conversion = False
      if hasattr(array, "dtypes") and hasattr(
          array.dtypes, "__array__"
      ):
          # throw warning if columns are sparse. If all columns are sparse, then
          # array.sparse exists and sparsity will be preserved (later).
          with suppress(ImportError):
              from pandas import SparseDtype
  
              def is_sparse(dtype):
                  return isinstance(dtype, SparseDtype)
  
              if (
                  not hasattr(array, "sparse")
                  and array.dtypes.apply(is_sparse).any()
              ):
                  warnings.warn(
                      "pandas.DataFrame with sparse columns found."
                      "It will be converted to a dense numpy array."
                  )
  
          dtypes_orig = list(array.dtypes)
          pandas_requires_conversion = any(
              _pandas_dtype_needs_early_conversion(i)
              for i in dtypes_orig
          )
          if all(
              isinstance(dtype_iter, np.dtype)
              for dtype_iter in dtypes_orig
          ):
              dtype_orig = np.result_type(*dtypes_orig)
          elif pandas_requires_conversion and any(
              d == object for d in dtypes_orig
          ):
              # Force object if any of the dtypes is an object
              dtype_orig = object
  
      elif (
          _is_extension_array_dtype(array) or hasattr(array, "iloc")
      ) and hasattr(array, "dtype"):
          # array is a pandas series
          pandas_requires_conversion = (
              _pandas_dtype_needs_early_conversion(array.dtype)
          )
          if isinstance(array.dtype, np.dtype):
              dtype_orig = array.dtype
          else:
              # Set to None to let array.astype work out the best dtype
              dtype_orig = None
  
      if dtype_numeric:
          if (
              dtype_orig is not None
              and hasattr(dtype_orig, "kind")
              and dtype_orig.kind == "O"
          ):
              # if input is object, convert to float.
              dtype = xp.float64
          else:
              dtype = None
  
      if isinstance(dtype, (list, tuple)):
          if dtype_orig is not None and dtype_orig in dtype:
              # no dtype conversion required
              dtype = None
          else:
              # dtype conversion required. Let's select the first element of the
              # list of accepted types.
              dtype = dtype[0]
  
      if pandas_requires_conversion:
          # pandas dataframe requires conversion earlier to handle extension dtypes with
          # nans
          # Use the original dtype for conversion if dtype is None
          new_dtype = dtype_orig if dtype is None else dtype
          array = array.astype(new_dtype)
          # Since we converted here, we do not need to convert again later
          dtype = None
  
      if dtype is not None and _is_numpy_namespace(xp):
          dtype = np.dtype(dtype)
  
      if force_all_finite not in (True, False, "allow-nan"):
          raise ValueError(
              'force_all_finite should be a bool or "allow-nan". Got {!r} instead'.format(
                  force_all_finite
              )
          )
  
      if dtype is not None and _is_numpy_namespace(xp):
          # convert to dtype object to conform to Array API to be use `xp.isdtype` later
          dtype = np.dtype(dtype)
  
      estimator_name = _check_estimator_name(estimator)
      context = (
          " by %s" % estimator_name if estimator is not None else ""
      )
  
      # When all dataframe columns are sparse, convert to a sparse array
      if hasattr(array, "sparse") and array.ndim > 1:
          with suppress(ImportError):
              from pandas import SparseDtype  # noqa: F811
  
              def is_sparse(dtype):
                  return isinstance(dtype, SparseDtype)
  
              if array.dtypes.apply(is_sparse).all():
                  # DataFrame.sparse only supports `to_coo`
                  array = array.sparse.to_coo()
                  if array.dtype == np.dtype("object"):
                      unique_dtypes = set(
                          [dt.subtype.name for dt in array_orig.dtypes]
                      )
                      if len(unique_dtypes) > 1:
                          raise ValueError(
                              "Pandas DataFrame with mixed sparse extension arrays "
                              "generated a sparse matrix with object dtype which "
                              "can not be converted to a scipy sparse matrix."
                              "Sparse extension arrays should all have the same "
                              "numeric type."
                          )
  
      if sp.issparse(array):
          _ensure_no_complex_data(array)
          array = _ensure_sparse_format(
              array,
              accept_sparse=accept_sparse,
              dtype=dtype,
              copy=copy,
              force_all_finite=force_all_finite,
              accept_large_sparse=accept_large_sparse,
              estimator_name=estimator_name,
              input_name=input_name,
          )
      else:
          # If np.array(..) gives ComplexWarning, then we convert the warning
          # to an error. This is needed because specifying a non complex
          # dtype to the function converts complex to real dtype,
          # thereby passing the test made in the lines following the scope
          # of warnings context manager.
          with warnings.catch_warnings():
              try:
                  warnings.simplefilter("error", ComplexWarning)
                  if dtype is not None and xp.isdtype(
                      dtype, "integral"
                  ):
                      # Conversion float -> int should not contain NaN or
                      # inf (numpy#14412). We cannot use casting='safe' because
                      # then conversion float -> int would be disallowed.
                      array = _asarray_with_order(
                          array, order=order, xp=xp
                      )
                      if xp.isdtype(
                          array.dtype,
                          ("real floating", "complex floating"),
                      ):
                          _assert_all_finite(
                              array,
                              allow_nan=False,
                              msg_dtype=dtype,
                              estimator_name=estimator_name,
                              input_name=input_name,
                          )
                      array = xp.astype(array, dtype, copy=False)
                  else:
                      array = _asarray_with_order(
                          array, order=order, dtype=dtype, xp=xp
                      )
              except ComplexWarning as complex_warning:
                  raise ValueError(
                      "Complex data not supported\n{}\n".format(array)
                  ) from complex_warning
  
          # It is possible that the np.array(..) gave no warning. This happens
          # when no dtype conversion happened, for example dtype = None. The
          # result is that np.array(..) produces an array of complex dtype
          # and we need to catch and raise exception for such cases.
          _ensure_no_complex_data(array)
  
          if ensure_2d:
              # If input is scalar raise error
              if array.ndim == 0:
                  raise ValueError(
                      "Expected 2D array, got scalar array instead:\narray={}.\n"
                      "Reshape your data either using array.reshape(-1, 1) if "
                      "your data has a single feature or array.reshape(1, -1) "
                      "if it contains a single sample.".format(array)
                  )
              # If input is 1D raise error
              if array.ndim == 1:
                  raise ValueError(
                      "Expected 2D array, got 1D array instead:\narray={}.\n"
                      "Reshape your data either using array.reshape(-1, 1) if "
                      "your data has a single feature or array.reshape(1, -1) "
                      "if it contains a single sample.".format(array)
                  )
  
          if (
              dtype_numeric
              and hasattr(array.dtype, "kind")
              and array.dtype.kind in "USV"
          ):
              raise ValueError(
                  "dtype='numeric' is not compatible with arrays of bytes/strings."
                  "Convert your data to numeric values explicitly instead."
              )
          if not allow_nd and array.ndim >= 3:
              raise ValueError(
                  "Found array with dim %d. %s expected <= 2."
                  % (array.ndim, estimator_name)
              )
  
          if force_all_finite:
              _assert_all_finite(
                  array,
                  input_name=input_name,
                  estimator_name=estimator_name,
                  allow_nan=force_all_finite == "allow-nan",
              )
  
      if ensure_min_samples > 0:
          n_samples = _num_samples(array)
          if n_samples < ensure_min_samples:
              raise ValueError(
                  "Found array with %d sample(s) (shape=%s) while a"
                  " minimum of %d is required%s."
                  % (
                      n_samples,
                      array.shape,
                      ensure_min_samples,
                      context,
                  )
              )
  
      if ensure_min_features > 0 and array.ndim == 2:
          n_features = array.shape[1]
          if n_features < ensure_min_features:
              raise ValueError(
                  "Found array with %d feature(s) (shape=%s) while"
                  " a minimum of %d is required%s."
                  % (
                      n_features,
                      array.shape,
                      ensure_min_features,
                      context,
                  )
              )
  
      if copy:
          if _is_numpy_namespace(xp):
              # only make a copy if `array` and `array_orig` may share memory`
              if np.may_share_memory(array, array_orig):
                  array = _asarray_with_order(
                      array, dtype=dtype, order=order, copy=True, xp=xp
                  )
          else:
              # always make a copy for non-numpy arrays
              array = _asarray_with_order(
                  array, dtype=dtype, order=order, copy=True, xp=xp
              )
  
      return array
  
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 19 for calling a global function with example usage: check_array(array, accept_sparse) and returns: array, isinstance, isinstance