code: def _unique(values, *, return_inverse=False):
      """Helper function to find unique values with support for python objects.
  
      Uses pure python method for object dtype, and numpy method for
      all other dtypes.
  
      Parameters
      ----------
      values : ndarray
          Values to check for unknowns.
  
      return_inverse : bool, default=False
          If True, also return the indices of the unique values.
  
      Returns
      -------
      unique : ndarray
          The sorted unique values.
  
      unique_inverse : ndarray
          The indices to reconstruct the original array from the unique array.
          Only provided if `return_inverse` is True.
      """
      if values.dtype == object:
          series_unique = (
              md.Series(md.Series(values).unique()).sort_values().values
          )
          if return_inverse:
              return series_unique, _map_to_integer(
                  values, series_unique
              )
          else:
              return series_unique
  
      out = mt.unique(values, return_inverse=return_inverse)
  
      if return_inverse:
          uniques, inverse = out
      else:
          uniques = out
  
      # np.unique will have duplicate missing values at the end of `uniques`
      # here we clip the nans and remove it from uniques
      uniques = uniques.rechunk(
          tuple((s,) for idx, s in enumerate(uniques.shape))
      )
      nan_idx = mt.searchsorted(uniques, mt.nan)
      uniques = uniques.map_chunk(
          lambda c, idx: c[: idx + 1],
          args=(nan_idx,),
          dtype=uniques.dtype,
          shape=(np.nan,) * uniques.ndim,
      )
      if return_inverse:
  
          def inv_mapper(c, idx):
              if c.flags.writeable:
                  c[c > idx] = idx
              else:  # pragma: no cover
                  # If c is got from the shared memory, it is immutable.
                  c = np.select([c <= idx], [c], idx)
              return c
  
          inverse = inverse.map_chunk(
              inv_mapper,
              args=(nan_idx,),
              dtype=inverse.dtype,
              shape=((np.nan,),) * inverse.ndim,
          )
  
          return uniques, inverse
      return uniques
  
file_path: mars/mars/learn/utils/_encode.py
imports: import numpy
  from typing import NamedTuple
  from ...None import tensor, dataframe
  from .core import is_scalar_nan
label_desc: Use image node_id 1 for calling a global function with example usage: _unique(values) and returns: uniques, uniques,  inverse, series_unique,  _map_to_integer, series_unique, c