code: def _infer_dimension(spectrum, n_samples):
      """Infers the dimension of a dataset with a given spectrum.
  
      The returned value will be in [1, n_features - 1].
      """
      xp, _ = get_namespace(spectrum)
  
      ll = xp.empty_like(spectrum)
      ll[0] = -xp.inf  # we don't want to return n_components = 0
      for rank in range(1, spectrum.shape[0]):
          ll[rank] = _assess_dimension(spectrum, rank, n_samples)
      return xp.argmax(ll)
  
imports: from math import log, sqrt
  from numbers import Integral, Real
  import numpy
  from scipy import linalg
  from scipy.sparse import issparse
  from scipy.sparse.linalg import svds
  from scipy.special import gammaln
  from ..base import _fit_context
  from ..utils import check_random_state
  from ..utils._arpack import _init_arpack_v0
  from ..utils._array_api import _convert_to_numpy, get_namespace
  from ..utils._param_validation import Interval, RealNotInt, StrOptions
  from ..utils.extmath import fast_logdet, randomized_svd, stable_cumsum, svd_flip
  from ..utils.sparsefuncs import _implicit_column_offset, mean_variance_axis
  from ..utils.validation import check_is_fitted
  from ._base import _BasePCA
label_desc: Use image node_id 2 for calling a global function with example usage: _infer_dimension(spectrum, n_samples) and returns: xp