python - Filter numpy array if elements in subarrays are repeated position-wise in the other subarrays -




unluckily terribly similar to: filter numpy array if list within contains @ least 1 value of previous row question asked minutes ago.

in case have list

b = np.array([[1,2], [1,8], [2,3], [4,2], [5,6], [7,8], [3,3], [10,1]]) 

what want different now.

i want start @ beginning of list , each subarray. want check whether element in position (with respect subarray) encountered in position i in other subarrays. hence, removing such elements.

for instance:

  • look @ [1,2]: eliminate [1,8] cause 1 in position 0, eliminate [4,2] cause 2 in position 1. not eliminate [10,1] or [2,3] since 1 , 2 in different positions.
  • look @ [2,3] ,eliminate [3,3] since 3 in position 1.
  • look @ [5,6], nothing eliminate.
  • look @ [7,8], nothing eliminate

so result b = np.array([[1,2], [2,3], [5,6], 7,8], [10,1]])

my try can see in previous post tried different things. now, noticed a==b gives useful array, used filtering, can't quite decide how put together.

edit:

my initial solution doesn't consistently produce result you're looking for, example @ bottom.

so here's alternative solution, iterates through rows seems necessary:

ar = b.copy() new_rows = [] while ar.shape[0]:     new_rows.append(ar[0])     ar = ar[(ar != ar[0]).all(axis=1)] np.stack(new_rows) out[463]: array([[ 1,  2],        [ 2,  3],        [ 5,  6],        [ 7,  8],        [10,  1]]) 

original answer:

you can use np.unique argument return_index=true identify rows first contain value in given column. can select these rows, in order, , same next column.

ar = b.copy() num_cols = ar.shape[1] col in range(num_cols):     ar = ar[np.sort(np.unique(ar[:, col], return_index=true)[1])] ar out[30]:  array([[ 1,  2],        [ 2,  3],        [ 5,  6],        [ 7,  8],        [10,  1]]) 

case original fails:

consider ar = b[:, ::-1], columns in reversed order.

then,

num_cols = ar.shape[1]     col in range(num_cols):         ar = ar[np.sort(np.unique(ar[:, col], return_index=true)[1])] 

gives

ar out[426]:  array([[ 2,  1],        [ 3,  2],        [ 6,  5],        [1,  10]]) 

missing desired [8, 7] row.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -