class_docstr: Balance binary or multiclass datasets by duplicating rarer class samples.
  
  This class balances a dataset by duplicating samples of the rarer class so
  that the sum of all example weights from all classes is the same. (Up to
  integer rounding of course). This can be useful when you're working on an
  imabalanced dataset where there are far fewer examples of some classes than
  others.
  
  This class differs from `BalancingTransformer` in that it actually
  duplicates rarer class samples rather than just increasing their sample
  weights. This may be more friendly for models that are numerically fragile
  and can't handle imbalanced example weights.
  
  Examples
  --------
  Here's an example for a binary dataset.
  
  >>> n_samples = 10
  >>> n_features = 3
  >>> n_tasks = 1
  >>> n_classes = 2
  >>> import deepchem as dc
  >>> import numpy as np
  >>> ids = np.arange(n_samples)
  >>> X = np.random.rand(n_samples, n_features)
  >>> y = np.random.randint(n_classes, size=(n_samples, n_tasks))
  >>> w = np.ones((n_samples, n_tasks))
  >>> dataset = dc.data.NumpyDataset(X, y, w, ids)
  >>> transformer = dc.trans.DuplicateBalancingTransformer(dataset=dataset)
  >>> dataset = transformer.transform(dataset)
  
  And here's a multiclass dataset example.
  
  >>> n_samples = 50
  >>> n_features = 3
  >>> n_tasks = 1
  >>> n_classes = 5
  >>> ids = np.arange(n_samples)
  >>> X = np.random.rand(n_samples, n_features)
  >>> y = np.random.randint(n_classes, size=(n_samples, n_tasks))
  >>> w = np.ones((n_samples, n_tasks))
  >>> dataset = dc.data.NumpyDataset(X, y, w, ids)
  >>> transformer = dc.trans.DuplicateBalancingTransformer(dataset=dataset)
  >>> dataset = transformer.transform(dataset)
  
  See Also
  --------
  deepchem.trans.BalancingTransformer: Balance by changing sample weights.
  
  Note
  ----
  This transformer is only well-defined for singletask datasets. (Since
  examples are actually duplicated, there's no meaningful way to duplicate
  across multiple tasks in a way that preserves the balance.)
  
  This transformer is only meaningful for classification datasets where `y`
  takes on a limited set of values. This class transforms all of `X`, `y`,
  `w`, `ids`.
  
  Raises
  ------
  `ValueError` if the provided dataset is multitask.
code: def __init__(self, dataset: Dataset):
      super(DuplicateBalancingTransformer, self).__init__(
          transform_X=True,
          transform_y=True,
          transform_w=True,
          transform_ids=True,
          dataset=dataset,
      )
  
      if len(dataset.get_task_names()) > 1:
          raise ValueError(
              "This transformation is only defined for singletask datsets."
          )
  
      # Get the labels/weights
      y = dataset.y
      w = dataset.w
      # Normalize shapes
      if len(y.shape) == 1:
          y = np.reshape(y, (len(y), 1))
      if len(w.shape) == 1:
          w = np.reshape(w, (len(w), 1))
      if len(y.shape) != 2:
          raise ValueError("y must be of shape (N,) or (N, n_tasks)")
      if len(w.shape) != 2:
          raise ValueError("w must be of shape (N,) or (N, n_tasks)")
      self.classes = sorted(np.unique(y))
      # Remove labels with zero weights
      y = y[w != 0]
      class_weights = []
      # Note that we may have 0 elements of a given class since we remove those
      # labels with zero weight.
      for c in self.classes:
          # this works because y is 1D
          c_weight = np.sum(w[y == c])
          class_weights.append(c_weight)
      weight_largest = max(class_weights)
      # This is the right ratio since int(N/num_c) * num_c \approx N
      # for all classes
      duplication_ratio = [
          int(weight_largest / float(c_weight)) if c_weight > 0 else 0
          for c_weight in class_weights
      ]
      self.duplication_ratio = duplication_ratio
  
file_path: deepchem/deepchem/trans/duplicate.py
imports: import logging
  import numpy
  from typing import Tuple
  from deepchem.data import Dataset
  from deepchem.trans.transformers import Transformer
label_desc: Use image node_id 1 to create a new DuplicateBalancingTransformer object from inherited base classes: Transformer with example: obj = DuplicateBalancingTransformer(dataset)