Python NumPy Array Slicing

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:October 12, 2022

Python NumPy array slicing is used to extract some portion of data from the actual array. Slicing in python means extracting data from one given index to another given index, however, NumPy slicing is slightly different. Slicing can be done with the help of (:). A NumPy array slicing object is constructed by giving start, stop, and step parameters to the built-in slicing function. This slicing object is passed to the array to extract some portion of the array.

The syntax of Python NumPy slicing is [start : stop : step]

Start : This index by default considers as ‘0’

stop : This index considers as a length of the array.

step : By default it is considered as ‘1’.

In this article, I will explain Python NumPy array slicing and how to extract some parts of data from 1-dimensional arrays, 2-dimensional arrays, and 3-dimensional arrays.

Another way to access elements from NumPy arrays is NumPy array indexing.

Note that arrays in the ndarray object follow the zero-based index.

1. Quick Examples of Python NumPy Array Slicing

If you are in a hurry, below are some quick examples of how to Python NumPy array slicing.


# Below are a quick examples

# Example 1: Create numpy arrays
arr = np.array([3, 5, 7, 9, 11, 15, 18, 22])

# Example 2: Use slicing a 1D arrays
arr2 = arr[1:6]

# Example 3: Slice Starting from 3rd value to end
arr2 = arr[3:]

# Example 4: Slice 0 to 4 index
arr2 = arr[:5]

# Example 5: Use negative slicing
arr2 = arr[-4:-2]

# Example 6: Use step value
arr2 = arr[::3]

# Example 7: Use slicing with interval
arr2 = arr[3::4]

# Example 8: Use slicing a 2D arrays
arr = np.array([[3, 5, 7, 9, 11],
               [2, 4, 6, 8, 10]])
arr2 = arr[1:,1:3]

# Example 9: Slicing 3-D arrays
arr = np.array([[[3, 5, 7, 9, 11],
                 [2, 4, 6, 8, 10]],
                [[5, 7, 8, 9, 2],
                 [7, 2, 3, 6, 7]]])
arr2 = arr[0,1,0:2]

2. Slicing 1-Dimensional NumPy Arrays

Using slicing operation we can extract elements of a 1-D NumPy array. For example,

arr[1:6] syntax to slice elements from index 1 to index 6 from the following 1-D array.


# import numpy module
import numpy as np

# Create NumPy arrays
arr = np.array([3, 5, 7, 9, 11, 15, 18, 22])

# Use slicing to get 1-D arrays elements
arr2 = arr[1:6]
print(arr2) 

# OutPut
# [ 5  7  9 11 15]

From the above, you can observe that the result included the start index which is 5, but excluded the end index which is 18.

When we pass the elements from index 3 to the end as a parameter of the slicing operation, we will get elements of an array from index 3 to the last index.


# Starting position from 3 to the end
arr2 = arr[3:]
print(arr2) 

# Output
# [ 9 11 15 18 22]

In order to slicing of 1-D array pass elements from a starting index to the 5th index as a parameter of the slicing operation, we will get elements of an array from the start index to before mentioned ending index.


# Returns first 5 values (0 to 4 index)
arr2 = arr[:5]
print(arr2)  

# Output
# [ 3  5  7  9 11]

Use the step value to determine the step of the slicing. It returns every other element from the entire array. Follow the syntax of slicing [start: stop: step] here, parameters are separated by a colon (:).


# Use step value to get elements
arr2 = arr[::3]
print(arr2)

# Output
#[ 3  9 18]

3. Use Negative Slicing To Get Elements of NumPy Array

As a part of extracting elements of NumPy array from ending, we have to use negative slicing, which is extracting elements of an array from ending.

arr[-4:-2] to slice from index -4 to index -2 from the end. Minus operator to refer to an index from the end.


# Use negative slicing to get elements 
arr2 = arr[-4:-2]
print(arr2) 

# Output
# [11 15]

From the above, we can see we have sliced elements of an array from the end.

4. Use Slicing With Interval 0f NumPy Arrays

arr[3::4] in this slice 3 is the starting point and 4 is the interval. So the returning array stars from the element in index three. After that, it takes every fourth element of the array until the end.


# Use slicing with interval
arr2 = arr[3::4]
print(arr2) 

# Output
# [ 9 22]

5. Slicing 2-Dimensional NumPy Arrays

Use slicing a 2-dimensional array in both axes to obtain a rectangular subset of the original array. You can use arr[1:,1:3] to select rows 1: one to the end of the bottom of the array and columns 1:3 (columns 1 and 2).


# Create NumPy arrays
arr = np.array([[3, 5, 7, 9, 11],
               [2, 4, 6, 8, 10]])
               

# Use slicing a 2-D arrays
arr2 = arr[1:,1:3]
print(arr2)  

# Output
#[[4 6]]

6. Slicing 3-Dimensional NumPy Arrays

To extract elements of a 3-D NumPy array using slice operation first we have to create a 3-dimensional array and then, apply slice operation.


# Slicing 3-D arrays
arr = np.array([[[3, 5, 7, 9, 11],
                 [2, 4, 6, 8, 10]],
                [[5, 7, 8, 9, 2],
                 [7, 2, 3, 6, 7]]])
               
arr2 = arr[0,1,0:2]
print(arr2)

# Output:
# [2 4]

7. Conclusion

In this article, I have explained Python NumPy array slicing techniques for extracting elements from 1-D arrays, 2-D arrays, and 3-D arrays with examples.

Happy Learning!!

References

Related Articles

Leave a Reply

You are currently viewing Python NumPy Array Slicing