Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix.
import numpy as np nums = np.random.randint(0,4,(6,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: [[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]]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer