w3resource

NumPy: Create an array (a) of shape 3, 4, 8

NumPy: Array Object Exercise-162 with Solution

Create an array (a) of shape 3, 4, 8 (K=3, J=4, I=8). tidx is an array of the same length as a.shape[1], i.e. contains J = 4 elements where each index denotes which element of K should be chosen.
Write a NumPy program to select from the first axis (K) by the indices tidx to get an array of shape (J=4, I=8) back.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Generating a random 3D array of integers between 0 and 9 with a shape of (3, 4, 8)
a = np.random.randint(0, 10, (3, 4, 8))

# Displaying the original array and its shape
print("Original array and shape:")
print(a)
print(a.shape)

# Printing a separator
print("--------------------------------")

# Generating random indices within a specified range (0 to 2) with a length of 4
tidx = np.random.randint(0, 3, 4)

# Displaying the generated random indices
print("tidex: ", tidx)

# Extracting specific elements using the generated random indices along different axes of the array
# The resulting array contains elements from 'a' based on the generated indices along the first axis, the second axis as the arange, and the third axis
print("Result:")
print(a[tidx, np.arange(len(tidx)), :]) 

Sample Output:

Original array and shape:
[[[3 2 2 7 7 7 0 3]
  [5 8 4 2 9 9 3 9]
  [6 8 2 8 5 7 8 7]
  [5 2 4 0 4 9 2 5]]

 [[4 3 1 8 2 5 2 0]
  [9 1 5 8 8 5 6 5]
  [3 2 2 0 1 5 6 1]
  [5 1 9 4 2 6 9 2]]

 [[4 6 6 3 8 6 8 8]
  [3 9 2 6 3 3 1 0]
  [5 4 0 6 0 2 7 8]
  [6 3 1 8 8 1 5 7]]]
(3, 4, 8)
--------------------------------
tidex:  [0 2 2 2]
Result:
[[3 2 2 7 7 7 0 3]
 [3 9 2 6 3 3 1 0]
 [5 4 0 6 0 2 7 8]
 [6 3 1 8 8 1 5 7]]

Explanation:

In the above code –

  • a = np.random.randint(0, 10, (3, 4, 8)): This statement creates a NumPy array a of random integers between 0 (inclusive) and 10 (exclusive) with a shape of (3, 4, 8).
  • print(a.shape): This statement prints the shape of the array ‘a’, which is (3, 4, 8).
  • tidx = np.random.randint(0, 3, 4): This statement generates a NumPy array ‘tidx ‘ containing 4 random integers between 0 (inclusive) and 3 (exclusive).
  • print("tidex: ",tidx): Print the array ‘tidx’.
  • print(a[tidx, np.arange(len(tidx)),:]): This statement select rows from the 3D array a using the indices in ‘tidx’. For each index in ‘tidx’, the corresponding row in the same position in np.arange(len(tidx)) is selected. In other words, for each column in the 4x8 matrix, one row is selected from one of the 3 "layers" based on the index in ‘tidx’. The result is a 2D array with the shape (4, 8). Finally print() function prints the resulting array.

Note:

numpy.arange([start, ]stop, [step, ]dtype=None) function: The numpy.arange() function is used to generate an array with evenly spaced values within a specified interval. The function returns a one-dimensional array of type numpy.ndarray.

Python-Numpy Code Editor:

Previous: Write a NumPy program to create a white image of size 512x256.
Next: Create two arrays of six elements. Write a NumPy program to count the number of instances of a value occurring in one aray on the condition of another array.

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/python-numpy-exercise-162.php