kmario23
kmario23

Reputation: 61355

Retain order when taking unique rows in a NumPy array

I have three 2D arrays a1, a2, and a3

In [165]: a1
Out[165]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [166]: a2
Out[166]: 
array([[ 9, 10, 11],
       [15, 16, 17],
       [18, 19, 20]])

In [167]: a3 
Out[167]: 
array([[6, 7, 8],
       [4, 5, 5]])

And I stacked these arrays into a single array:

In [168]: stacked = np.vstack((a1, a2, a3))

In [170]: stacked 
Out[170]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [ 9, 10, 11],
       [15, 16, 17],
       [18, 19, 20],
       [ 6,  7,  8],
       [ 4,  5,  5]])

Now, I want to get rid of the duplicate rows. So, numpy.unique does the job.

In [169]: np.unique(stacked, axis=0)
Out[169]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 4,  5,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [15, 16, 17],
       [18, 19, 20]])

However, there is one issue here. The original order is lost when taking the unique rows. How can I retain the original ordering and still take the unique rows?

So, the expected output should be:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [15, 16, 17],
       [18, 19, 20],
       [ 4,  5,  5]])

Upvotes: 6

Views: 2070

Answers (2)

farhad
farhad

Reputation: 97

after get the stacked array

step 1: get the indexes of the rows for sorted unique array

row_indexes = np.unique(stacked, return_index=True, axis=0)[1]

Note: row_indexes is holding the indexes for sorted array

step 2 : now iterate through the stacked array with the sorted index

sorted_index=sorted(row_indexes)
new_arr=[]
for i in range(len(sorted_index)):
    new_arr.append(stacked[sorted_index[i]]

thats it!!!!!

Upvotes: 0

BENY
BENY

Reputation: 323226

Using return_index

_,idx=np.unique(stacked, axis=0,return_index=True)

stacked[np.sort(idx)]
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [15, 16, 17],
       [18, 19, 20],
       [ 4,  5,  5]])

Upvotes: 9

Related Questions