module documentation

Undocumented

Variable array​_function​_dispatch Undocumented
Variable c​_ Undocumented
Variable index​_exp Undocumented
Variable mgrid Undocumented
Variable ogrid Undocumented
Variable r​_ Undocumented
Variable s​_ Undocumented
Class ​Axis​Concatenator Translates slice objects to concatenation along an axis.
Class ​CClass Translates slice objects to concatenation along the second axis.
Class ​Index​Expression A nicer way to build up index tuples for arrays.
Class ​MGrid​Class nd_grid instance which returns a dense multi-dimensional "meshgrid".
Class nd​_grid Construct a multi-dimensional "meshgrid".
Class ndenumerate Multidimensional index iterator.
Class ndindex An N-dimensional iterator object to index arrays.
Class ​OGrid​Class nd_grid instance which returns an open multi-dimensional "meshgrid".
Class ​RClass Translates slice objects to concatenation along the first axis.
Function ​_diag​_indices​_from Undocumented
Function ​_fill​_diagonal​_dispatcher Undocumented
Function ​_ix​_​_dispatcher Undocumented
Function diag​_indices Return the indices to access the main diagonal of an array.
Function diag​_indices​_from Return the indices to access the main diagonal of an n-dimensional array.
Function fill​_diagonal Fill the main diagonal of the given array of any dimensionality.
Function ix​_ Construct an open mesh from multiple sequences.
array_function_dispatch =

Undocumented

c_ =

Undocumented

index_exp =

Undocumented

mgrid =

Undocumented

ogrid =

Undocumented

r_ =

Undocumented

s_ =

Undocumented

def _diag_indices_from(arr):

Undocumented

def _fill_diagonal_dispatcher(a, val, wrap=None):

Undocumented

def _ix__dispatcher(*args):

Undocumented

@set_module('numpy')
def diag_indices(n, ndim=2):

Return the indices to access the main diagonal of an array.

This returns a tuple of indices that can be used to access the main diagonal of an array a with a.ndim >= 2 dimensions and shape (n, n, ..., n). For a.ndim = 2 this is the usual diagonal, for a.ndim > 2 this is the set of indices to access a[i, i, ..., i] for i = [0..n-1].

Parameters

n : int
The size, along each dimension, of the arrays for which the returned indices can be used.
ndim : int, optional
The number of dimensions.

See Also

diag_indices_from

Notes

New in version 1.4.0.

Examples

Create a set of indices to access the diagonal of a (4, 4) array:

>>> di = np.diag_indices(4)
>>> di
(array([0, 1, 2, 3]), array([0, 1, 2, 3]))
>>> a = np.arange(16).reshape(4, 4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> a[di] = 100
>>> a
array([[100,   1,   2,   3],
       [  4, 100,   6,   7],
       [  8,   9, 100,  11],
       [ 12,  13,  14, 100]])

Now, we create indices to manipulate a 3-D array:

>>> d3 = np.diag_indices(2, 3)
>>> d3
(array([0, 1]), array([0, 1]), array([0, 1]))

And use it to set the diagonal of an array of zeros to 1:

>>> a = np.zeros((2, 2, 2), dtype=int)
>>> a[d3] = 1
>>> a
array([[[1, 0],
        [0, 0]],
       [[0, 0],
        [0, 1]]])
@array_function_dispatch(_diag_indices_from)
def diag_indices_from(arr):

Return the indices to access the main diagonal of an n-dimensional array.

See diag_indices for full details.

Parameters

arr : array, at least 2-D

See Also

diag_indices

Notes

New in version 1.4.0.
@array_function_dispatch(_fill_diagonal_dispatcher)
def fill_diagonal(a, val, wrap=False):

Fill the main diagonal of the given array of any dimensionality.

For an array a with a.ndim >= 2, the diagonal is the list of locations with indices a[i, ..., i] all identical. This function modifies the input array in-place, it does not return a value.

Parameters

a : array, at least 2-D.
Array whose diagonal is to be filled, it gets modified in-place.
val : scalar or array_like
Value(s) to write on the diagonal. If val is scalar, the value is written along the diagonal. If array-like, the flattened val is written along the diagonal, repeating if necessary to fill all diagonal entries.
wrap : bool
For tall matrices in NumPy version up to 1.6.2, the diagonal "wrapped" after N columns. You can have this behavior with this option. This affects only tall matrices.

See also

diag_indices, diag_indices_from

Notes

New in version 1.4.0.

This functionality can be obtained via diag_indices, but internally this version uses a much faster implementation that never constructs the indices and uses simple slicing.

Examples

>>> a = np.zeros((3, 3), int)
>>> np.fill_diagonal(a, 5)
>>> a
array([[5, 0, 0],
       [0, 5, 0],
       [0, 0, 5]])

The same function can operate on a 4-D array:

>>> a = np.zeros((3, 3, 3, 3), int)
>>> np.fill_diagonal(a, 4)

We only show a few blocks for clarity:

>>> a[0, 0]
array([[4, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])
>>> a[1, 1]
array([[0, 0, 0],
       [0, 4, 0],
       [0, 0, 0]])
>>> a[2, 2]
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 4]])

The wrap option affects only tall matrices:

>>> # tall matrices no wrap
>>> a = np.zeros((5, 3), int)
>>> np.fill_diagonal(a, 4)
>>> a
array([[4, 0, 0],
       [0, 4, 0],
       [0, 0, 4],
       [0, 0, 0],
       [0, 0, 0]])
>>> # tall matrices wrap
>>> a = np.zeros((5, 3), int)
>>> np.fill_diagonal(a, 4, wrap=True)
>>> a
array([[4, 0, 0],
       [0, 4, 0],
       [0, 0, 4],
       [0, 0, 0],
       [4, 0, 0]])
>>> # wide matrices
>>> a = np.zeros((3, 5), int)
>>> np.fill_diagonal(a, 4, wrap=True)
>>> a
array([[4, 0, 0, 0, 0],
       [0, 4, 0, 0, 0],
       [0, 0, 4, 0, 0]])

The anti-diagonal can be filled by reversing the order of elements using either numpy.flipud or numpy.fliplr.

>>> a = np.zeros((3, 3), int);
>>> np.fill_diagonal(np.fliplr(a), [1,2,3])  # Horizontal flip
>>> a
array([[0, 0, 1],
       [0, 2, 0],
       [3, 0, 0]])
>>> np.fill_diagonal(np.flipud(a), [1,2,3])  # Vertical flip
>>> a
array([[0, 0, 3],
       [0, 2, 0],
       [1, 0, 0]])

Note that the order in which the diagonal is filled varies depending on the flip function.

Construct an open mesh from multiple sequences.

This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions.

Using ix_ one can quickly construct index arrays that will index the cross product. a[np.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

Parameters

args : 1-D sequences
Each sequence should be of integer or boolean type. Boolean sequences will be interpreted as boolean masks for the corresponding dimension (equivalent to passing in np.nonzero(boolean_sequence)).

Returns

out : tuple of ndarrays
N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh.

See Also

ogrid, mgrid, meshgrid

Examples

>>> a = np.arange(10).reshape(2, 5)
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> ixgrid = np.ix_([0, 1], [2, 4])
>>> ixgrid
(array([[0],
       [1]]), array([[2, 4]]))
>>> ixgrid[0].shape, ixgrid[1].shape
((2, 1), (1, 2))
>>> a[ixgrid]
array([[2, 4],
       [7, 9]])
>>> ixgrid = np.ix_([True, True], [2, 4])
>>> a[ixgrid]
array([[2, 4],
       [7, 9]])
>>> ixgrid = np.ix_([True, True], [False, False, True, False, True])
>>> a[ixgrid]
array([[2, 4],
       [7, 9]])