Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a NumPy array and permuting another one along with it

I have two, numpy arrays, the first, A, being one-dimensional, the second, B, is two-dimensional in the application I have in mind, but really could have any dimension. Every single index of B covers the same range as the single index of A.

Now, I'd like to sort A (in descending order) but would like to permute every dimension of B along with it. Mathematically speaking, if P is the permutation matrix that sorts A, I would like to transform B according to np.dot(P, np.dot(B, P.T)). E.g. consider this example where sorting coincidentally corresponds to reversing the order:

In [1]: import numpy as np

In [2]: A = np.array([1,2,3])

In [3]: B = np.random.rand(3,3); B
Out[3]: 
array([[ 0.67402953,  0.45017072,  0.24324747],
       [ 0.40559793,  0.79007712,  0.94247771],
       [ 0.47477422,  0.27599007,  0.13941255]])

In [4]: # desired output:

In [5]: A[::-1]
Out[5]: array([3, 2, 1])

In [6]: B[::-1,::-1]
Out[6]: 
array([[ 0.13941255,  0.27599007,  0.47477422],
       [ 0.94247771,  0.79007712,  0.40559793],
       [ 0.24324747,  0.45017072,  0.67402953]])

The application I have in mind is to obtain eigenvalues and eigenvectors of a nonsymmetric matrix using np.linalg.eig (in contrast to eigh, eig does not guarantee any ordering of the eigenvalues), sort them by absolute value, and truncate the space. It would be beneficial to permute the components of the matrix holding the eigenvectors along with the eigenvalues and perform the truncation by slicing it.

like image 518
Jonas Greitemann Avatar asked Feb 05 '16 09:02

Jonas Greitemann


People also ask

How do you sort an array based on the sorting of another array Python?

Use the zip function: zip( *sorted( zip(arr1, arr2) ) ) This will do what you need.

How do I put two NumPy arrays next to each other?

You can use the numpy. concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array.

Can NumPy have mixed data types?

Having a data type (dtype) is one of the key features that distinguishes NumPy arrays from lists. In lists, the types of elements can be mixed.


2 Answers

You can use np.argsort to get sorted indices of A. Then you can use these indices to rearrange B.

It is not entirely cear how you want to rearrange B...

p = np.argsort(A)

B[:, p][p, :]  # rearrange rows and column of B
B.transpose(p)  # rearrange dimensions of B

If you want to order eigenvectors according to eigenvalues, you should only rearrange the columns of the eigenvectors: (Also, it may make sense to use the absolute value, in case you get complex eigenvalues)

e, v = eig(x)
p = np.argsort(np.abs(e))[::-1]  # descending order
v = v[:, p]
like image 186
MB-F Avatar answered Sep 23 '22 06:09

MB-F


You can use numpy.argsort to get the index mapping. For example:

test=np.array([2,1,3])
test_array=np.array([[2,3,4],[1,2,3]])
rearranged_array=test_array[:,test.argsort()]

Here, test.argsort() yields [1,0,2].

like image 20
Christoph Avatar answered Sep 20 '22 06:09

Christoph