ndarray.__getitem__¶
-
ndarray.__getitem__(self, key)¶ Return self[key].
Returns a sliced view of this array if the elements fetched are contiguous in memory; otherwise, returns a newly created NDArray. This functions supports advanced indexing defined in the following reference with some restrictions. Boolean indexing is supported only for a single boolean ndarray as a key. Mixing boolean ndarray with other index types is not supported in
advancedindexing.For basic indexing, i.e., if
keyconsists only of integers,slice,Ellipsis(...) andNone, a mutable view is returned that shares memory with this array if the accessed portion is contiguous in memory. Otherwise, a newly createdndarrayis returned.This functions supports advanced indexing as defined in the NumPy advanced indexing documentation.
Parameters: - key : int, slice, list, np.ndarray, mx.np.ndarray, or tuple of all previous types
Indexing key.
Examples
The default is to give explicit indices for all axes:
>>> x = np.arange(6).reshape(2, 3) >>> x array([[0., 1., 2.], [3., 4., 5.]]) >>> x[0, :2] array([0., 1.]) >>> x[:, :-1] array([[0., 1.], [3., 4.]])
If fewer indices are given, they are automatically supplemented by an appropriate number of
slice(None)(“:”) to the right. For instance, a single integer indexes along the first axis:>>> x[0] array([0., 1., 2.]) >>> x[1:] array([[3., 4., 5.]])
To omit a range of axes that should be kept as-is, an Ellipsis (“
...”) can be used:>>> x = np.arange(16).reshape(2, 2, 2, 2) >>> x[0, ..., 1] array([[1., 3.], [5., 7.]]) >>> x[0, :, :, 1] # equivalent array([[1., 3.], [5., 7.]])
New axes of length 1 can be created by inserting
None(numpy.newaxis) in the index:>>> x = np.arange(6).reshape(2, 3) >>> x[None, :, :] array([[[0., 1., 2.], [3., 4., 5.]]]) >>> x[None, :, :].shape (1, 2, 3)
If the indexed portion of the array is contiguous in memory, no data is copied. Instead, a shared-memory view of the original array is returned, and changes to that view affect the original array:
>>> x = np.arange(8).reshape(2, 2, 2) >>> y = x[0] # contiguous >>> y array([[0., 1.], [2., 3.]]) >>> y[:] = -1 >>> x array([[[-1., -1.], [-1., -1.]], [[ 4., 5.], [ 6., 7.]]]) >>> x = np.arange(8).reshape(2, 2, 2) >>> y = x[1, :1, :] # contiguous >>> y array([[4., 5.]]) >>> y[:] = -1 >>> x array([[[ 0., 1.], [ 2., 3.]], [[-1., -1.], [ 6., 7.]]]) >>> x = np.arange(0, 8).reshape(2, 2, 2) >>> y = x[:, :, 1] # not contiguous >>> y array([[1., 3.], [5., 7.]]) >>> y[:] = -1 >>> x array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
If the indexing key contains list, numpy.ndarray or NDArray objects, advanced indexing is triggered, which always returns a copy:
>>> x = np.arange(8).reshape(2, 2, 2) >>> x[[0, 1]] array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]]) >>> x[[0, 1], :] # equivalent array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]]) >>> y = np.array([0, 1], dtype='int32') >>> x[1:, y] array([[[4., 5.], [6., 7.]]]) >>> y = np.array([0, 1], dtype='int32') >>> x[1:, y] array([[[4., 5.], [6., 7.]]])
Get negative elements in an ndarray through boolean array indexing >>> x = np.array([1., -1., -2., 3]) >>> x[x < 0] array([-1., -2.])