MCPcopy Create free account
hub / github.com/numpy/numpy / ndindex

Class ndindex

numpy/lib/index_tricks.py:619–703  ·  view source on GitHub ↗

An N-dimensional iterator object to index arrays. Given the shape of an array, an `ndindex` instance iterates over the N-dimensional index of the array. At each iteration a tuple of indices is returned, the last dimension is iterated over first. Parameters ---------- s

Source from the content-addressed store, hash-verified

617
618@set_module('numpy')
619class ndindex:
620 """
621 An N-dimensional iterator object to index arrays.
622
623 Given the shape of an array, an `ndindex` instance iterates over
624 the N-dimensional index of the array. At each iteration a tuple
625 of indices is returned, the last dimension is iterated over first.
626
627 Parameters
628 ----------
629 shape : ints, or a single tuple of ints
630 The size of each dimension of the array can be passed as
631 individual parameters or as the elements of a tuple.
632
633 See Also
634 --------
635 ndenumerate, flatiter
636
637 Examples
638 --------
639 Dimensions as individual arguments
640
641 >>> for index in np.ndindex(3, 2, 1):
642 ... print(index)
643 (0, 0, 0)
644 (0, 1, 0)
645 (1, 0, 0)
646 (1, 1, 0)
647 (2, 0, 0)
648 (2, 1, 0)
649
650 Same dimensions - but in a tuple ``(3, 2, 1)``
651
652 >>> for index in np.ndindex((3, 2, 1)):
653 ... print(index)
654 (0, 0, 0)
655 (0, 1, 0)
656 (1, 0, 0)
657 (1, 1, 0)
658 (2, 0, 0)
659 (2, 1, 0)
660
661 """
662
663 def __init__(self, *shape):
664 if len(shape) == 1 and isinstance(shape[0], tuple):
665 shape = shape[0]
666 x = as_strided(_nx.zeros(1), shape=shape,
667 strides=_nx.zeros_like(shape))
668 self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
669 order='C')
670
671 def __iter__(self):
672 return self
673
674 def ndincr(self):
675 """
676 Increment the multi-dimensional index by one.

Callers 3

padFunction · 0.90
apply_along_axisFunction · 0.90
test_ndindexFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_ndindexFunction · 0.72