w3resource

NumPy: Find rows of a given array of shape that contain elements of each row of another given array of shape

NumPy: Array Object Exercise-189 with Solution

Write a NumPy program to find rows of a given array of shape (8,3) that contain elements of each row of another given array of shape (2,2).

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Generating two NumPy arrays 'nums1' and 'nums2' filled with random integers from 0 to 5
nums1 = np.random.randint(0, 6, (6, 4))
nums2 = np.random.randint(0, 6, (2, 3))

# Displaying the original arrays 'nums1' and 'nums2'
print("Original arrays:")
print(nums1)
print("\n", nums2)

# Comparing elements of 'nums1' with elements of 'nums2' using broadcasting
temp = (nums1[..., np.newaxis, np.newaxis] == nums2)

# Counting the number of occurrences of elements from 'nums2' within each row of 'nums1'
# Checking if the count is greater than or equal to the number of elements in 'nums2' rows
# Obtaining rows from 'nums1' that contain elements of each row of 'nums2'
rows = (temp.sum(axis=(1, 2, 3)) >= nums2.shape[1]).nonzero()[0]

# Displaying rows of 'nums1' that contain elements of each row of 'nums2'
print("\nRows of a given array that contain elements of each row of another given array:")
print(rows) 

Sample Output:

Original arrays:
[[5 2 5 1]
 [5 4 1 3]
 [0 1 1 1]
 [2 0 4 0]
 [2 5 1 5]
 [4 0 4 0]]

 [[2 3 1]
 [1 1 4]]

Rows of a given array that contain elements of each row of another given array:
[0 1 2 4]

Explanation:

In the above exercise -

np.random.randint(0, 6, (6, 4)): Generates a 2D NumPy array nums1 with shape (6, 4) and random integers in the range of [0, 6).

np.random.randint(0, 6, (2, 3)): Generates a 2D NumPy array nums2 with shape (2, 3) and random integers in the range of [0, 6).

temp = (nums1[..., np.newaxis, np.newaxis] == nums2)

In the above code -

  • nums1[..., np.newaxis, np.newaxis]: Adds two new axes at the end of nums1, resulting in a 4D array of shape (6, 4, 1, 1).
  • nums1[..., np.newaxis, np.newaxis] == nums2: Compares nums1 and nums2 element-wise with broadcasting, resulting in a 4D boolean array of shape (6, 4, 2, 3).

rows = (temp.sum(axis=(1,2,3)) >= nums2.shape[1]).nonzero()[0]

In the above code –

  • temp.sum(axis=(1, 2, 3)): Sums the boolean array temp along axes 1, 2, and 3, resulting in a 1D array with the count of matching elements for each row of nums1.
  • (temp.sum(axis=(1, 2, 3)) >= nums2.shape[1]): Compares the count of matching elements for each row of nums1 to the number of columns in nums2. The result is a 1D boolean array of shape (6,).
  • (temp.sum(axis=(1, 2, 3)) >= nums2.shape[1]).nonzero()[0]: Retrieves the indices of True values in the 1D boolean array, indicating the rows in nums1 that have at least as many occurrences of each element in nums2 along the rows.

print(rows): Finally print() function prints the row indices in nums1 that meet the specified condition.

Python-Numpy Code Editor:

Previous:Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix.
Next: Write a NumPy program to create a record array from a given regular array.

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.