Friday , June 18 2021
Home / Data Science / Numpy Slicing & Indexing

Numpy Slicing & Indexing

7 – Slicing & Indexing

Subset, Slice, Index and Iterate through Arrays

For one-dimensional arrays, indexing, slicing etc. is similar to python lists – indexing starts at 0.

Slicing arrays

Slicing in python means taking elements from one given index to another given index.

  1. We pass slice instead of index like this: [start:end].
  2. We can also define the step, like this: [start:end:step].
  3. If we don’t pass start its considered 0
  4. If we don’t pass end its considered length of array in that dimension
  5. If we don’t pass step its considered 1

Indexing and Slicing One Dimensional Arrays

In [51]:
array_1d = np.arange(10)
print(array_1d)
[0 1 2 3 4 5 6 7 8 9]
In [52]:
# Third element
print(array_1d[2])
2
In [54]:
# Specific elements
# Notice that array[2, 5, 6] will throw an error, you need to provide the indices as a list
print(array_1d[[2, 5, 6]])
[2 5 6]
In [55]:
# Slice third element onwards
print(array_1d[2:])
[2 3 4 5 6 7 8 9]
In [56]:
# Slice first three elements
print(array_1d[:3])
[0 1 2]
In [57]:
# Slice third to seventh elements
print(array_1d[2:7])
[2 3 4 5 6]
In [58]:
# Subset starting 0 at increment of 2 
print(array_1d[0::2])
[0 2 4 6 8]
In [59]:
# Last Element of af an array 
print(array_1d[-1])
9
In [60]:
# Iterations are also similar to lists
for i in array_1d:
    print(i**2)
0
1
4
9
16
25
36
49
64
81

Indexing and Slicing on 2-D Arrays

In [61]:
array_2d = np.array([[2, 5, 7, 5], [4, 6, 8, 10], [10, 12, 15, 19]])
print(array_2d)
[[ 2  5  7  5]
 [ 4  6  8 10]
 [10 12 15 19]]
In [62]:
# Third row second column
print(array_2d[2, 1])
12
In [63]:
# Slicing the second row, and all columns
# Notice that the resultant is itself a 1-D array
print(array_2d[1, :])
print(type(array_2d[1, :]))
[ 4  6  8 10]
<class 'numpy.ndarray'>
In [64]:
# Slicing all rows and the third column
print(array_2d[:, 2])
[ 7  8 15]
In [66]:
# Slicing all rows and all the columns
print(array_2d[:, :])
[[ 2  5  7  5]
 [ 4  6  8 10]
 [10 12 15 19]]
In [67]:
# Slicing all rows and the first three columns
print(array_2d[:, :3])
[[ 2  5  7]
 [ 4  6  8]
 [10 12 15]]
In [72]:
# Slicing fisrt two rows and all the columns
print(array_2d[:2,:])
[[ 2  5  7  5]
 [ 4  6  8 10]]

another method

In [69]:
# Slicing all rows using three dots and the first three columns
print(array_2d[..., :3])
[[ 2  5  7]
 [ 4  6  8]
 [10 12 15]]
In [74]:
# Slicing fisrt two rows and all the columns using three dots
print(array_2d[:2,...])
[[ 2  5  7  5]
 [ 4  6  8 10]]

Iterating on 2-D arrays is done with respect to the first axis (which is row, the second axis is column).

In [37]:
# Iterating over 2-D arrays
for row in array_2d:
    print(row)
[2 5 7 5]
[ 4  6  8 10]
[10 12 15 19]
In [38]:
# Iterating over 3-D arrays: Done with respect to the first axis
array_3d = np.arange(24).reshape(2, 3, 4) #2 Arrays of 3 rows and 4 columns : 2*3*4 = 24
print(array_3d)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
In [39]:
# Prints the two blocks
for row in array_3d:
    print(row)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]

Boolean Indexing

In [40]:
arr1 = np.arange(25).reshape((5,5))
arr1
Out[40]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
In [41]:
bools = np.array([True, True, False, True, False])
print(arr1[bools])
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [15 16 17 18 19]]
In [42]:
# negate the condition
print(arr1[~bools])    
[[10 11 12 13 14]
 [20 21 22 23 24]]

Find elements/indices by conditions

In [46]:
arr = np.arange(16).reshape(4,4)
arr
Out[46]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
In [47]:
# find the elements greater than 5 and return a flattened array
print(arr[arr>5])    # or arr[np.where(arr>5)]
[ 6  7  8  9 10 11 12 13 14 15]
In [48]:
# return values based on conditions 
# np.where(condition, true_return, false_return)
print(np.where(arr>5, -1, 10))
[[10 10 10 10]
 [10 10 -1 -1]
 [-1 -1 -1 -1]
 [-1 -1 -1 -1]]
In [49]:
# find the indices of the elements on conditions
print(np.argwhere(arr>5))
[[1 2]
 [1 3]
 [2 0]
 [2 1]
 [2 2]
 [2 3]
 [3 0]
 [3 1]
 [3 2]
 [3 3]]
In [ ]:
 

About Machine Learning

Check Also

Array Manipulation

9 – Manipulate an Array Numpy Array Manipulation¶ In [2]: import numpy as np In [3]: arr …

Leave a Reply

Your email address will not be published. Required fields are marked *