Write a NumPy program to find and store non-zero unique rows in an array after comparing each row with other row in a given matrix.
import numpy as np arra = np.array([[ 1, 1, 0], [ 0, 0, 0], [ 0, 2, 3], [ 0, 0, 0], [ 0, -1, 1], [ 0, 0, 0]]) print("Original array:") print(arra) temp = {(0, 0, 0)} result = [] for idx, row in enumerate(map(tuple, arra)): if row not in temp: result.append(idx) print("\nNon-zero unique rows:") print(arra[result])
Sample Output:
Original array: [[ 1 1 0] [ 0 0 0] [ 0 2 3] [ 0 0 0] [ 0 -1 1] [ 0 0 0]] Non-zero unique rows: [[ 1 1 0] [ 0 2 3] [ 0 -1 1]]
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