Sorting a NumPy Array

I have an np array that is built as the intersection of two other arrays like this:

First array:

[['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ...,
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

      

Second array:

[['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ...,
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

      

There are actually several differences between the two arrays, but they are found mainly in the middle rows.

Code used to plot the intersection:

def multidim_intersect(arr1, arr2):
    arr1_view = arr1.view([('',arr1.dtype)]*arr1.shape[1])
    arr2_view = arr2.view([('',arr2.dtype)]*arr2.shape[1])
    intersected = np.intersect1d(arr1_view, arr2_view)
    return intersected.view(arr1.dtype).reshape(-1, arr1.shape[1])

      

Output array:

[['!' '!']
 ['!' '! !']
 ['!' '! ! !']
 ...,
 ['}' 'was']
 ['}' 'was postponed']
 ['}' '{of']]

      

As you can see, my new array is sorted differently than the original two arrays (which have multiple exclamation marks sorted before single exclamation marks, as is done in LC_ALL = C sort). Is there a way to sort my output array like my other arrays? Please note that the shape of the array is important.

@Mr E arr1 and arr2 were originally listed. I can't give you an exact copy, but I'll do my best to build an example that illustrates what I need.

arr1 = [['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['!' '!']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

arr2 = [['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['!' '!']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

      

Ideally the output would be:

[['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['!' '!']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

      

but instead:

[['!' '!']
 ['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

      

or something like that.

+3


source to share


1 answer


I don't understand the format of your input, but you can adapt it according to your needs.

The problem is that it numpy.intersect1d()

automatically sorts the output for whatever reason. Fortunately, it's not that hard to write your own intersection function using numpy.in1d()

. You can do something like this:

import numpy as np

arr1 = np.array([['! ! !' '! ! ! !'],
 ['! ! !' '! ! ! "'],
 ['! ! !' '! ! ! .'],
 ['!' '!'],
 ['a' 'ad'],   # Stuff you don't want to get back
 ['}' 'was postponed'],
 ['}' 'was'],
 ['}' '{of']])

arr2 = np.array([['! ! !' '! ! ! !'],
 ['! ! !' '! ! ! "'],
 ['! ! !' '! ! ! .'],
 ['!' '!'],
 ['b' 'ab'],   # Stuff you don't want to get back
 ['}' 'was postponed'],
 ['}' 'was'],
 ['}' '{of']])

inarr = np.in1d(arr1, arr2)

arr3 = np.empty( shape=(0, 0) )

for i in np.arange(len(arr1)):
  if (inarr[i]):
    arr3 = np.append(arr3,arr1[i])

for i in np.arange(len(arr3)):
  print(arr3[i])

      



Output:

! ! !! ! ! !
! ! !! ! ! "
! ! !! ! ! .
!!
}was postponed
}was
}{of

      

+1


source







All Articles