简体   繁体   中英

Numpy sorting array (sparse)

I'm trying to work out why this code doesn't sort the array...

Arbitrary vector.

x = array([[3, 2, 4, 5, 7, 4, 3, 4, 3, 3, 1, 4, 6, 3, 2, 4, 3, 2]])
xCoo = sps.coo_matrix(x)
perm = np.argsort(x)
xCoo.col = perm[xCoo.col]
print(xCoo.toarray()) # array([3, 2, 4, 5, 7, 4, 3, 4, 3, 3, 1, 4, 6, 3, 2, 4, 3, 2])

I'm not sure what I've misunderstood. What's the correct way to do this?

Thank you.

PS I'm aware that I can just call sort on the array; however, I went to apply this same permutation over and over again.

The first complication is the np.argsort(x) returns a 2d array. Lets do the sort on flattened x to get a simpler 1d perm :

In [1118]: perm=np.argsort(x,None)

In [1119]: perm
Out[1119]: 
array([10, 17,  1, 14, 13,  9, 16,  0,  6,  8,  5, 11,  2, 15,  7,  3, 12,
        4], dtype=int32)

this sorts x as we expect, right?

In [1120]: x[:,perm]
Out[1120]: array([[1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 6, 7]])

now apply it in the same way to xCoo , except we have to convert it to lil format. coo format isn't subscriptable:

In [1121]: xCoo.tolil()[:,perm].A
Out[1121]: array([[1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 6, 7]], dtype=int32)

To apply perm directly to the attributes of xCoo , we need to do another sort:

xCoo.col = np.argsort(perm)[xCoo.col]   # <====

This works for multirow xCoo with zeros.

You can also sort the data:

xCoo.data = xCoo.data[perm[xCoo.col]]

These work here, but they need more testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM