class_docstr: Random under-sampling.
  
  This is a wrapper for classifiers. It will train the provided classifier by under-sampling the
  stream of given observations so that the class distribution seen by the classifier follows
  a given desired distribution. The implementation is a discrete version of rejection sampling.
  
  See [Working with imbalanced data](/user-guide/imbalanced-learning) for example usage.
  
  Parameters
  ----------
  classifier
  desired_dist
      The desired class distribution. The keys are the classes whilst the values are the desired
      class percentages. The values must sum up to 1.
  seed
      Random seed for reproducibility.
  
  Examples
  --------
  
  >>> from river import datasets
  >>> from river import evaluate
  >>> from river import imblearn
  >>> from river import linear_model
  >>> from river import metrics
  >>> from river import preprocessing
  
  >>> model = imblearn.RandomUnderSampler(
  ...     (
  ...         preprocessing.StandardScaler() |
  ...         linear_model.LogisticRegression()
  ...     ),
  ...     desired_dist={False: 0.4, True: 0.6},
  ...     seed=42
  ... )
  
  >>> dataset = datasets.CreditCard().take(3000)
  
  >>> metric = metrics.LogLoss()
  
  >>> evaluate.progressive_val_score(dataset, model, metric)
  LogLoss: 0.0336...
  
  References
  ----------
  [^1]: [Under-sampling a dataset with desired ratios](https://maxhalford.github.io/blog/undersampling-ratios/)
  [^2]: [Wikipedia article on rejection sampling](https://www.wikiwand.com/en/Rejection_sampling)
code: def learn_one(self, x, y, **kwargs):
      self._actual_dist[y] += 1
      f = self.desired_dist
      g = self._actual_dist
  
      # Check if the pivot needs to be changed
      if y != self._pivot:
          self._pivot = max(g.keys(), key=lambda y: f[y] / g[y])
      else:
          self.classifier.learn_one(x, y, **kwargs)
          return
  
      # Determine the sampling ratio if the class is not the pivot
      M = f[self._pivot] / g[self._pivot]  # Likelihood ratio
      ratio = f[y] / (M * g[y])
  
      if ratio < 1 and self._rng.random() < ratio:
          self.classifier.learn_one(x, y, **kwargs)
  
imports: from __future__ import annotations
  import collections
  import random
  import typing
  from river import base, utils
label_desc: Use image node_id 2 for calling the RandomUnderSampler obj's underlying member method code with example usage: obj.learn_one(x, y) without return types