w3resource

Combine slicing and Indexing in NumPy to select elements

NumPy: Advanced Indexing Exercise-18 with Solution

Combining Slicing and Indexing:

Write a Numpy program that creates a 3D NumPy array and use a combination of slicing and integer indexing to select a specific slice and then index into it.

Sample Solution:

Python Code:

import numpy as np

# Create a 3D NumPy array of shape (4, 5, 6) with random integers
array_3d = np.random.randint(0, 100, size=(4, 5, 6))

# Use slicing to select a specific slice along the first axis
slice_2d = array_3d[2, :, :]

# Define the row and column indices to further index into the 2D slice
row_indices = np.array([0, 2, 4])
col_indices = np.array([1, 3, 5])

# Use integer indexing to select specific elements from the 2D slice
selected_elements = slice_2d[row_indices, col_indices]

# Print the original array, the 2D slice, and the selected elements
print('Original 3D array:\n', array_3d)
print('2D slice (3rd slice along first axis):\n', slice_2d)
print('Row indices:', row_indices)
print('Column indices:', col_indices)
print('Selected elements from 2D slice:\n', selected_elements)

Output:

Original 3D array:
 [[[81 97 48  7 72 45]
  [50 71 41 71 60 88]
  [42 26 78 23 47 64]
  [49  5 94 62  4  5]
  [12 29 69 68 90  6]]

 [[15  1 54 84 50 94]
  [97 70 17 43 15 24]
  [30 83 91 86 36 54]
  [34 92  8 78 58 76]
  [13 51 98 44 64  9]]

 [[46 57 40 48 67 40]
  [33 68 34 31 69  5]
  [13 34 24 23 54 57]
  [67  0 51 22 84 21]
  [67 80 51 66 86  9]]

 [[33 39 45 19 52 83]
  [94 85 76 23 34 43]
  [85 40 29 89 46 26]
  [ 3 70 71 20 14 60]
  [47 71 86 36 90  2]]]
2D slice (3rd slice along first axis):
 [[46 57 40 48 67 40]
 [33 68 34 31 69  5]
 [13 34 24 23 54 57]
 [67  0 51 22 84 21]
 [67 80 51 66 86  9]]
Row indices: [0 2 4]
Column indices: [1 3 5]
Selected elements from 2D slice:
 [57 23  9]

Explanation:

  • Import Libraries:
    • Imported numpy as np for array creation and manipulation.
  • Create 3D NumPy Array:
    • Create a 3D NumPy array named array_3d with random integers ranging from 0 to 99 and a shape of (4, 5, 6).
  • Select Specific Slice:
    • Used slicing to select the 3rd slice along the first axis (array_3d[2, :, :]), resulting in a 2D slice.
  • Define Indices:
    • Defined row_indices and col_indices arrays to specify the rows and columns to be indexed within the 2D slice.
  • Integer Indexing:
    • Used integer indexing to select specific elements from the 2D slice based on the defined row and column indices.
  • Print Results:
    • Print the original 3D array, the 2D slice, and the selected elements to verify the indexing operation.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Extract smaller Subarrays using integer Indexing in NumPy.
Next: Boolean Indexing on higher dimensions in NumPy arrays.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/combine-slicing-and-indexing-in-numpy-to-select-elements.php