Basic indexing and slicing in NumPy

 Basic indexing and slicing

  • ndarrays can be indexed using the standard Python x[obj] syntax, where x is the array and obj the selection.
  • There are different kinds of indexing available depending on obj: basic indexing, advanced indexing, and field access.

Single element indexing
>>> x = np.arange(10)
>>> x[2]
2
>>> x[-2]
8
>>> x.shape = (2, 5)  # now x is 2-dimensional
>>> x[1, 3]
8
>>> x[1, -1]
9

Slicing and striding( stepping) and Dimensional indexing tools
>>> x[..., 0]
array([[1, 2, 3],
      [4, 5, 6]])
>>> x[:, :, 0]
array([[1, 2, 3],
      [4, 5, 6]])

  • Single element indexing works exactly like Python sequences.
  • It is 0-based and accepts negative indices for indexing from the end of the array.
  • It is not necessary to separate each dimension’s index into its own set of square brackets.
  • The basic concept of slicing extends Python’s basic slicing concept to N dimensions.
  • slice object constructed by start: stop step notation inside of brackets.
  • The simplest case of indexing:- N integers returns an array scalar representing the corresponding item.
  • In Python, all indices are zero-based,  the valid range is 0≤ nidi, where di is the i-th element of the shape of the array.
  • Negative indices are interpreted as counting from the end of the array (i.e., if ni<0, it means ni+di).
  • Facilitate the easy matching of array shapes with expressions and in assignments.
  • Ellipsis expands to the number of objects needed for the selection tuple to index all dimensions.
  • Example:-
  • This is equivalent to:
  •  NumPy arrays can be sliced. Arrays specify a slice for each dimension of the array:
>>> a = np.arange(10)**3 
>>> a
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729]) 
>>> a[2] 
>>> a[2:5] 
array([ 8, 27, 64]) 
>>> # equivalent to a[0:6:2] = 1000; 
>>> # from start to position 6, exclusive, set every 2nd element to 1000  
>>> a[:6:2] = 1000 
>>> a 
array([1000,    1, 1000,   27, 1000,  125,  216,  343,  512,  729]) 
>>> a[::-1]  # reversed a 
array([ 729,  512,  343,  216,  125, 1000,   27, 1000,    1, 1000]) 
>>> for i in a
...     print(i**(1 / 3.)) 
... 
9.999999999999998
1.0
9.999999999999998
3.0
9.999999999999998
4.999999999999999
5.999999999999999
6.999999999999999
7.999999999999999
.999999999999998

Multidimensional arrays can have one index per axis. These tuple indices are separated by commas:
>>> def f(x, y):
...     return 10 * x + y
...
>>> b = np.fromfunction(f, (5, 4), dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])
>>> b[2, 3]
23
>>> b[0:5, 1]  # each row in the second column of b
array([ 1, 11, 21, 31, 41])
>>> b[:, 1]    # equivalent to the previous example
array([ 1, 11, 21, 31, 41])
>>> b[1:3, :]  # each column in the second and third row of b
array([[10, 11, 12, 13],
       [20, 21, 22, 23]])

When fewer indices are provided than the number of axes, the missing indices are considered complete slices:
>>> b[-1]   # the last row. Equivalent to b[-1, :]
array([40, 41, 42, 43])

in the expression b[i] , i use for many instances, : represent the remaining axes. Ex. b[i, ...].

The dots (...) represent as many colons as needed to produce a complete indexing tuple. For example, if x is an array with 5 axes, then

·                     x[1, 2, ...] is equivalent to x[1, 2, :, :, :],

·                     x[..., 3] to x[:, :, :, :, 3] and

·                     x[4, ..., 5, :] to x[4, :, :, 5, :].


>>> c = np.array([[[  0,  1,  2],  # a 3D array (two stacked 2D arrays)

...                [ 10, 12, 13]],

...               [[100, 101, 102],

...                [110, 112, 113]]])

>>> c.shape

(2, 2, 3)

>>> c[1, ...]  # same as c[1, :, :] or c[1]

array([[100, 101, 102],

       [110, 112, 113]])

>>> c[..., 2]  # same as c[:, :, 2]

array([[  2,  13],

       [102, 113]])


Iteration in multidimensional arrays is done with the first axis:

>>> for row in b:

...     print(row)

...

[0 1 2 3]

[10 11 12 13]

[20 21 22 23]

[30 31 32 33]

[40 41 42 43]


if wants to perform an operation on each element in the array, use the flat attribute which is an iterator over all the elements of the array:

>>> for element in b.flat:

...     print(element)

...

0

1

2

3

10

11

12

13

20

21

22

23

30

31

32

33

40

41

42

43

Post a Comment

0 Comments