Undocumented
Class |
|
Translates slice objects to concatenation along an axis. |
Class |
|
Translates slice objects to concatenation along the second axis. |
Class |
|
A nicer way to build up index tuples for arrays. |
Class |
|
nd_grid instance which returns a dense multi-dimensional "meshgrid". |
Class | nd |
Construct a multi-dimensional "meshgrid". |
Class | ndenumerate |
Multidimensional index iterator. |
Class | ndindex |
An N-dimensional iterator object to index arrays. |
Class |
|
nd_grid instance which returns an open multi-dimensional "meshgrid". |
Class |
|
Translates slice objects to concatenation along the first axis. |
Function | diag |
Return the indices to access the main diagonal of an array. |
Function | diag |
Return the indices to access the main diagonal of an n-dimensional array. |
Function | fill |
Fill the main diagonal of the given array of any dimensionality. |
Function | ix_ |
Construct an open mesh from multiple sequences. |
Variable | array |
Undocumented |
Variable | c_ |
Undocumented |
Variable | index |
Undocumented |
Variable | mgrid |
Undocumented |
Variable | ogrid |
Undocumented |
Variable | r_ |
Undocumented |
Variable | s_ |
Undocumented |
Function | _diag |
Undocumented |
Function | _fill |
Undocumented |
Function | _ix |
Undocumented |
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].
See Also
Notes
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]]])
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. |
Return the indices to access the main diagonal of an n-dimensional array.
See diag_indices
for full details.
See Also
Notes
Parameters | |
arr:array , at least 2-D |
def fill_diagonal(a, val, wrap=False): (source) ¶
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.
See Also
Notes
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.
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. |
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]]].
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]])
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 | |
tuple of ndarrays | out - N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh. |