COO.from_iter

classmethod COO.from_iter(x, shape=None, fill_value=None, dtype=None)[source]

Converts an iterable in certain formats to a COO array. See examples for details.

Parameters
  • x (Iterable or Iterator) – The iterable to convert to COO.

  • shape (tuple[int], optional) – The shape of the array.

  • fill_value (scalar) – The fill value for this array.

  • dtype (numpy.dtype) – The dtype of the input array. Inferred from the input if not given.

Returns

out – The output COO array.

Return type

COO

Examples

You can convert items of the format [((i, j, k), value), ((i, j, k), value)] to COO. Here, the first part represents the coordinate and the second part represents the value.

>>> x = [((0, 0), 1), ((1, 1), 1)]
>>> s = COO.from_iter(x)
>>> s.todense()
array([[1, 0],
       [0, 1]])

You can also have a similar format with a dictionary.

>>> x = {(0, 0): 1, (1, 1): 1}
>>> s = COO.from_iter(x)
>>> s.todense()
array([[1, 0],
       [0, 1]])

The third supported format is (data, (..., row, col)).

>>> x = ([1, 1], ([0, 1], [0, 1]))
>>> s = COO.from_iter(x)
>>> s.todense()
array([[1, 0],
       [0, 1]])

You can also pass in a collections.Iterator object.

>>> x = [((0, 0), 1), ((1, 1), 1)].__iter__()
>>> s = COO.from_iter(x)
>>> s.todense()
array([[1, 0],
       [0, 1]])