COO.maybe_densify

COO.maybe_densify(max_size=1000, min_density=0.25)[source]

Converts this COO array to a numpy.ndarray if not too costly.

Parameters:
  • max_size (int) – Maximum number of elements in output
  • min_density (float) – Minimum density of output
Returns:

The dense array.

Return type:

numpy.ndarray

Raises:

ValueError – If the returned array would be too large.

Examples

Convert a small sparse array to a dense array.

>>> s = COO.from_numpy(np.random.rand(2, 3, 4))
>>> x = s.maybe_densify()
>>> np.allclose(x, s.todense())
True

You can also specify the minimum allowed density or the maximum number of output elements. If both conditions are unmet, this method will throw an error.

>>> x = np.zeros((5, 5), dtype=np.uint8)
>>> x[2, 2] = 1
>>> s = COO.from_numpy(x)
>>> s.maybe_densify(max_size=5, min_density=0.25)
Traceback (most recent call last):
    ...
ValueError: Operation would require converting large sparse array to dense