Skip to main content
`Ctrl`+`K`

# skbio.stats.composition.multi_replace

skbio.stats.composition.multi_replace(mat, delta=None)

  Replace all zeros with small non-zero values.

  It uses the multiplicative replacement strategy [1], replacing zeros with a small positive \(\delta\) and ensuring that the compositions still add up to 1.

  Parameters:
    mat array_like of shape (n_compositions, n_components)

      A matrix of proportions.

    delta float, optional

      A small number to be used to replace zeros. If not specified, the default value is \(\delta = \frac{1}{N^2}\) where \(N\) is the number of components.

  Returns:
    ndarray of shape (n_compositions, n_components)

      The matrix where all of the values are non-zero and each composition (row) adds up to 1.

  Raises:
    ValueError

      If negative proportions are created due to a large `delta`.

  Notes

  This method will result in negative proportions if a large delta is chosen.

  References

  [1]

  J. A. Martin-Fernandez. “Dealing With Zeros and Missing Values in Compositional Data Sets Using Nonparametric Imputation”

  Examples

      >>> import numpy as np
      >>> from skbio.stats.composition import multi_replace
      >>> X = np.array([[.2, .4, .4, 0],[0, .5, .5, 0]])
      >>> multi_replace(X)
      array([[ 0.1875,  0.375 ,  0.375 ,  0.0625],
             [ 0.0625,  0.4375,  0.4375,  0.0625]])