w3resource

NumPy: Extract any array of shape from a given array of integer values


Extract shape (6,6,3) array from a 12x12x4 matrix.

Write a NumPy program to create a 12x12x4 array with random values and extract any array of shape(6,6,3) from the said array.

Sample Solution:

Python Code:

# Importing NumPy library
import numpy as np

# Creating a NumPy array of shape (8, 8, 3) filled with random float values between 0 and 1
nums = np.random.random((8, 8, 3))

# Displaying the original array
print("Original array:")
print(nums)

# Extracting a sub-array of shape (6, 6, 3) from the original array using slicing
new_nums = nums[:6, :6, :]

# Displaying the extracted sub-array
print("\nExtract array of shape (6, 6, 3) from the said array:")
print(new_nums) 

Sample Output:

Original array:
[[[0.67420382 0.5592584  0.27096382]
  [0.2896283  0.68346522 0.13996167]
  [0.74283318 0.06323309 0.0980022 ]
  [0.43203954 0.38888969 0.44801756]
  [0.04391897 0.4851516  0.34044817]
  [0.2202623  0.81434798 0.51900666]
  [0.12442371 0.91247823 0.01874549]
  [0.2287782  0.88089638 0.40583551]]

 [[0.42389039 0.21162537 0.39794135]
  [0.70118645 0.77166133 0.02891853]
  [0.13635083 0.77918942 0.93913105]
  [0.82898906 0.7946305  0.01150909]
  [0.05929642 0.59762989 0.7248887 ]
  [0.15162473 0.55354106 0.92956369]
  [0.21183182 0.92999156 0.57835296]
  [0.41929845 0.19565816 0.18662498]]

 [[0.87032715 0.50662785 0.54217065]
  [0.82355975 0.20349718 0.01538959]
  [0.24959705 0.62056295 0.07042258]
  [0.64422139 0.88416751 0.49174671]
  [0.21436044 0.55439367 0.61435127]
  [0.38492133 0.66637382 0.66080096]
  [0.3957137  0.48913863 0.67103063]
  [0.81766502 0.38401956 0.06543795]]

 [[0.78656194 0.86104692 0.70951371]
  [0.50050861 0.14680231 0.08608443]
  [0.73766323 0.32358634 0.70397593]
  [0.04155761 0.23332385 0.98193348]
  [0.60442283 0.6441864  0.40536901]
  [0.81750344 0.87604709 0.03573838]
  [0.14154893 0.24558416 0.62685956]
  [0.44853079 0.90103491 0.4339039 ]]

 [[0.51162248 0.32017957 0.28754968]
  [0.27526172 0.06626226 0.60503387]
  [0.90903854 0.05226501 0.26241159]
  [0.73163092 0.98252245 0.44887237]
  [0.94349225 0.14615167 0.83662707]
  [0.25880778 0.61251959 0.82794232]
  [0.00672891 0.1271131  0.65880109]
  [0.88851577 0.75109775 0.56399842]]

 [[0.85965509 0.11357479 0.27325381]
  [0.74156642 0.35108524 0.40305073]
  [0.44791592 0.28270286 0.45377936]
  [0.01543443 0.14978493 0.47738367]
  [0.63671823 0.75239388 0.59118693]
  [0.55932007 0.32759274 0.25519358]
  [0.79183605 0.18399144 0.84579649]
  [0.06608463 0.63129404 0.78672545]]

 [[0.23577256 0.24679561 0.46901338]
  [0.43949749 0.93467498 0.9023869 ]
  [0.58850225 0.24534939 0.92965553]
  [0.26322984 0.13130557 0.67981953]
  [0.36389878 0.74552644 0.25606283]
  [0.77564163 0.50464125 0.3598317 ]
  [0.49057984 0.9482408  0.84635511]
  [0.70071485 0.43268376 0.39706312]]

 [[0.28021231 0.47537742 0.72971633]
  [0.87380873 0.83031311 0.56713737]
  [0.23093306 0.22830678 0.54439754]
  [0.88130002 0.37081258 0.78148687]
  [0.00318428 0.62297164 0.58875116]
  [0.68102061 0.31822913 0.04432477]
  [0.70410386 0.56770957 0.42998752]
  [0.5891714  0.25692428 0.19184309]]]

Extract array of shape (6,6,3) from the said array:
[[[0.67420382 0.5592584  0.27096382]
  [0.2896283  0.68346522 0.13996167]
  [0.74283318 0.06323309 0.0980022 ]
  [0.43203954 0.38888969 0.44801756]
  [0.04391897 0.4851516  0.34044817]
  [0.2202623  0.81434798 0.51900666]]

 [[0.42389039 0.21162537 0.39794135]
  [0.70118645 0.77166133 0.02891853]
  [0.13635083 0.77918942 0.93913105]
  [0.82898906 0.7946305  0.01150909]
  [0.05929642 0.59762989 0.7248887 ]
  [0.15162473 0.55354106 0.92956369]]

 [[0.87032715 0.50662785 0.54217065]
  [0.82355975 0.20349718 0.01538959]
  [0.24959705 0.62056295 0.07042258]
  [0.64422139 0.88416751 0.49174671]
  [0.21436044 0.55439367 0.61435127]
  [0.38492133 0.66637382 0.66080096]]

 [[0.78656194 0.86104692 0.70951371]
  [0.50050861 0.14680231 0.08608443]
  [0.73766323 0.32358634 0.70397593]
  [0.04155761 0.23332385 0.98193348]
  [0.60442283 0.6441864  0.40536901]
  [0.81750344 0.87604709 0.03573838]]

 [[0.51162248 0.32017957 0.28754968]
  [0.27526172 0.06626226 0.60503387]
  [0.90903854 0.05226501 0.26241159]
  [0.73163092 0.98252245 0.44887237]
  [0.94349225 0.14615167 0.83662707]
  [0.25880778 0.61251959 0.82794232]]

 [[0.85965509 0.11357479 0.27325381]
  [0.74156642 0.35108524 0.40305073]
  [0.44791592 0.28270286 0.45377936]
  [0.01543443 0.14978493 0.47738367]
  [0.63671823 0.75239388 0.59118693]
  [0.55932007 0.32759274 0.25519358]]]

Explanation:

In the above exercise -

nums = np.random.random((8, 8, 3)): This code first creates a 3-dimensional array nums with shape (8, 8, 3) filled with random values between 0 and 1.

new_nums = nums[:6, :6, :]: This code slices the “nums” array to create a new array new_nums. The slicing operation is performed on the first two dimensions (axis 0 and axis 1) using the slice :6, which means selecting elements from the beginning up to (but not including) index 6. The third dimension (axis 2) is kept entirely using the slice :.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to extract a sub-array of shape (6,6,3) from a randomly generated 12x12x4 matrix using slicing.
  • Create a function that selects a contiguous block from a 3D array based on provided start indices and block shape.
  • Implement a solution that uses np.copy to ensure the extracted sub-array is independent of the original array.
  • Test the extraction function with different starting indices to confirm flexible sub-array selection.

Python-Numpy Code Editor:

Previous: Write a NumPy program to remove the first dimension from a given array of shape (1,3,4).
Next: Write a NumPy program to create to concatenate two given arrays of shape (2, 2) and (2,1).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.