w3resource

NumPy: Extract rows with unequal values from 10x3 matrix

NumPy: Array Object Exercise-188 with Solution

Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix.

Sample Solution:

Example-1:

Python Code:

# Importing the NumPy library
import numpy as np

# Generating a 2D NumPy array 'nums' filled with random integers from 0 to 3
nums = np.random.randint(0, 4, (6, 3))

# Displaying the original 2D array 'nums'
print("Original vector:")
print(nums)

# Checking for rows where consecutive elements are not equal within each row
# using np.logical_and.reduce and axis=1 to check row-wise
new_nums = np.logical_and.reduce(nums[:, 1:] == nums[:, :-1], axis=1)

# Filtering out rows with unequal consecutive values and storing in 'result'
result = nums[~new_nums]

# Displaying rows with unequal consecutive values
print("\nRows with unequal values:")
print(result)

Sample Output:

Original vector:
[[3 2 0]
 [2 3 1]
 [2 0 3]
 [3 3 1]
 [2 1 1]
 [3 0 2]]

Rows with unequal values:
[[3 2 0]
 [2 3 1]
 [2 0 3]
 [3 3 1]
 [2 1 1]
 [3 0 2]]

Explanation:

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

new_nums = np.logical_and.reduce(nums[:,1:] == nums[:,:-1], axis=1)

In the above code -

  • nums[:, 1:]: Takes all rows and all columns starting from the second column (index 1) to the end of nums.
  • nums[:, :-1]: Takes all rows and all columns up to, but not including, the last column of nums.
  • nums[:, 1:] == nums[:, :-1]: Compares each pair of adjacent elements in each row. The result is a boolean array of the same shape as “nums” but with the last column removed.
  • np.logical_and.reduce(...): Performs a logical AND operation along the specified axis (axis=1, or columns) of the boolean array. This results in a 1D array with a True value for rows where all elements are equal and a False value otherwise.

Example-2:

Python Code:

import numpy as np
nums = np.array([(1,1,1),
              (1,1,1),
              (1,2,3)])
print("Original vector:")
print(nums)
new_nums = np.logical_and.reduce(nums[:,1:] == nums[:,:-1], axis=1)
result = nums[~new_nums]
print("\nRows with unequal values:")
print(result)

Sample Output:

Original vector:
[[1 1 1]
 [1 1 1]
 [1 2 3]]

Rows with unequal values:
[[1 2 3]]

Explanation:

result = nums[~new_nums]

In the above code -

  • ~new_nums: Negates the boolean array new_nums, inverting the True and False values.
  • nums[~new_nums]: Filters the rows in nums using the negated boolean array ~new_nums. This keeps the rows where not all elements are equal.

print(result): Finally print() function prints the resulting 2D NumPy array with rows from the original nums array where not all elements in a row are the same.

Python-Numpy Code Editor:

Previous:Write a NumPy program to convert a given vector of integers to a matrix of binary representation.
Next: 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).

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-188.php