开发者

how to sort a scipy array with order attribute when it does not have the field names?

I have a 2d scipy array(The oigion is the one color channel of an rgb image). I want to find the specific occurance of an element. I can use

   np.argsort(arr)
开发者_运维知识库

to do the job.

But the problem is that I want to do it in a specific order along axis. The scipy example for doing this is as follows

   x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])
   np.argsort(x, order=('x','y'))

In the above example they specified the name of the filed and the type. After that they used the argument "order" to specify the order of sorting.

In my case I don't have the filed names... How can I do it?

Thanks a lot.


Okay, suppose you have an ordinary numpy array:

In [34]: x = np.array([(1, 0), (0, 1)])

You can view it as a so-called structured array by using view:

In [35]: y = x.ravel().view(dtype=[('x', x.dtype), ('y', x.dtype)])

Now you can use np.argsort on y with the order parameter:

In [36]: np.argsort(y, order=('x','y'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜