code: def to_categorical(
      Y: IntsXd,
      n_classes: Optional[int] = None,
      *,
      label_smoothing: float = 0.0,
  ) -> FloatsXd:
      if n_classes is None:
          n_classes = int(numpy.max(Y) + 1)  # type: ignore
  
      if label_smoothing < 0.0:
          raise ValueError(
              "Label-smoothing parameter has to be greater than or equal to 0"
          )
  
      if label_smoothing == 0.0:
          if n_classes == 0:
              raise ValueError("n_classes should be at least 1")
          nongold_prob = 0.0
      else:
          if not n_classes > 1:
              raise ValueError(
                  "n_classes should be greater than 1 when label smoothing is enabled,"
                  f"but {n_classes} was provided."
              )
          nongold_prob = label_smoothing / (n_classes - 1)
  
      max_smooth = (n_classes - 1) / n_classes
      if n_classes > 1 and label_smoothing >= max_smooth:
          raise ValueError(
              f"For {n_classes} classes "
              "label_smoothing parameter has to be less than "
              f"{max_smooth}, but found {label_smoothing}."
          )
  
      xp = get_array_module(Y)
      label_distr = xp.full(
          (n_classes, n_classes), nongold_prob, dtype="float32"
      )
      xp.fill_diagonal(label_distr, 1 - label_smoothing)
      return label_distr[Y]
  
imports: import contextlib
  import functools
  import inspect
  import os
  import platform
  import random
  import tempfile
  import threading
  from contextvars import ContextVar
  from dataclasses import dataclass
  from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, TypeVar, Union, cast
  import numpy
  from packaging.version import Version
  from wasabi import table
  from .compat import cupy, cupy_from_dlpack, has_cupy, has_cupy_gpu, has_gpu, has_mxnet, has_tensorflow, has_torch, has_torch_cuda_gpu, has_torch_mps
  from .compat import mxnet
  from .compat import tensorflow
  from .compat import torch
  from typing import TYPE_CHECKING
  from .None import types
  from .types import ArgsKwargs, ArrayXd, FloatsXd, IntsXd, Padded, Ragged
label_desc: Use image node_id 22 for calling a global function with example usage: to_categorical(Y, n_classes) and returns: label_distr