Numpy array slicing: How to Slice Numpy Array in Python

0
50
Numpy array slicing - How to Slice Numpy Array in Python

Slicing in Python is a feature that allows accessing elements of iterators like strings, tuples, and lists. You can also use them to change or remove the elements of mutable iterators, such as lists.

Indexing is used to obtain individual items from the array, but it can also get entire rows, columns from multi-dimensional arrays.

Numpy slicing array

To slice a numpy array in Python, use the indexing. Slicing in Python means taking items from one given index to another given index. The slice returns a completely new list.

We pass slice instead of an index like this: [start: end].

We can also define the step, like this: [start: end: step].

If we don’t pass the start parameter, it is considered 0.

If we don’t pass the end parameter, it is considered the length of the array in that dimension.

If we don’t pass the step parameter, it is considered 1.

Slice array elements

Let’s slice items from index 0 to index 5 from the following array.

# app.py

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(arr[0:5])

Output

[1 2 3 4 5]

If you see the output carefully, then the sliced array includes the start index but excludes the end index.

Let’s slice the array from the 5th index.

# app.py

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(arr[5:])

Output

[ 6  7  8  9 10]

Negative Slicing

To use negative slicing, use the minus operator to refer to an index from the end.

# app.py

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(arr[-4: -2])

Output

[7 8]

Passing a step parameter

Use the step value to determine the step of the slicing.

# app.py

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(arr[2: 9: 2])

Output

[3 5 7 9]

To return every other element from the entire array, use the following code.

# app.py

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(arr[::2])

Output

[1 3 5 7 9]

Slicing 2D Arrays in Python

From the second element, slice elements from index 1 to index 4 (not included).

# app.py

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr[1, 1:3])

Output

[6 7]

From both elements, return index 3.

# app.py

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr[0: 2, 3])

Slicing a 3D Numpy array in Python

To slice a 3D array in Python, use all 3 axes to obtain a cuboid subset of the original array.

# app.py

import numpy as np

arr = np.array([[[21, 56, 12], [13, 46, 15], [16, 18, 18]],
                [[20, 19, 22], [23, 29, 25], [26, 18, 28]],
                [[30, 26, 32], [33, 6, 35], [36, 10, 38]]])

print(arr[:2, 1:, :2])

Output

[[[13 46]
  [16 18]]

 [[23 29]
  [26 18]]]

This selects:

  1. planes: 2 (the first 2 planes).
  2. rows: (the last 2 rows).
  3. columns: 2 (the first 2 columns).

Conclusion

Numpy array slicing extends Python’s fundamental concept of slicing to N dimensions. Essential slicing occurs when obj is a slice object (constructed by start: stop: step notation inside brackets), an integer, or a tuple of slice objects and integers. That is it for numpy array slicing.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.