根据给定索引就地numpy数组排序

编程入门 行业动态 更新时间:2024-10-18 01:39:30
本文介绍了根据给定索引就地numpy数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有些问题迫在眉睫,但是我还没有找到具体的答案.我正在尝试沿给定轴对numpy 3D数组进行一些就地排序.我不想简单排序,但我想根据自己的索引求助于数组.例如

There are some questions that come close, but I haven't found a specific answer to this. I'm trying to do some in-place sorting of a numpy 3D array along a given axis. I don't want simple sorting though, I want to resort the array according to my own index. For example

a = np.random.rand((3,3,3))

假设我想根据旧数组的以下索引求出最后一个维度:

and let's say I want to resort the last dimension according to the following indices of the old array:

new_order = [1,2,0]

我希望能够说:

a[:,:,new_order] = a

,但是这种行为不符合预期.有建议吗?

but this does not behave as expected. Suggestions?

推荐答案

np.ndarray.sort是唯一声称已就位的方法,它没有给您太多控制权.

np.ndarray.sort is the only sort that claims to be inplace, and it does not give you much control.

将订单索引放在正确的位置上是可行的-但可能会产生不可预测的结果.显然,它正在执行某种顺序分配,左侧的较早分配会影响右侧的值.

Placing the order index on the right works - but can give unpredictable results. Evidently it is doing some sort of sequential assignment, and an earlier assignment on the left can affect values on the right.

In [719]: a=np.arange(12).reshape(3,4) In [720]: a[:,[0,1,3,2]]=a In [721]: a Out[721]: array([[ 0, 1, 2, 2], [ 4, 5, 6, 6], [ 8, 9, 10, 10]])

可预测地进行这种分配需要某种缓冲.

To do this sort of assignment predictably requires some sort of buffering.

In [728]: a[:,[0,1,3,2]]=a.copy() In [729]: a Out[729]: array([[ 0, 1, 3, 2], [ 4, 5, 7, 6], [ 8, 9, 11, 10]])

右边的索引可以解决此问题,但这不是就位的.变量a指向一个新对象.

Indexing of the right gets around this, but this is not in-place. The variable a points to a new object.

In [731]: a=a[:,[0,1,3,2]] In [732]: a Out[732]: array([[ 0, 1, 3, 2], [ 4, 5, 7, 6], [ 8, 9, 11, 10]])

但是使用[:]进行分配可以解决此问题:

However assignment with [:] may solve this:

In [738]: a=np.arange(12).reshape(3,4) In [739]: a.__array_interface__ Out[739]: {'data': (181868592, False), # 181... is the id of the data buffer 'descr': [('', '<i4')], 'shape': (3, 4), 'strides': None, 'typestr': '<i4', 'version': 3} In [740]: a[:]=a[:,[0,1,3,2]] In [741]: a.__array_interface__ Out[741]: {'data': (181868592, False), # same data buffer 'descr': [('', '<i4')], 'shape': (3, 4), 'strides': None, 'typestr': '<i4', 'version': 3} In [742]: a Out[742]: array([[ 0, 1, 3, 2], [ 4, 5, 7, 6], [ 8, 9, 11, 10]])

a.data id相同的事实表明这是一个就地操作.但是,最好与其他索引进行测试,以确保它能够满足您的要求.

The fact that the a.data id is the same indicates that this is an inplace action. But it would be good to test this with other indexing to make sure it does what you want.

但是,就地"排序是否必要?如果阵列很大,则可能需要避免内存错误.但是我们必须测试替代方案,看看它们是否有效.

But, is 'inplace' sorting necessary? If the array is very large it might be needed to avoid memory errors. But we'd have to test the alternatives to see if they work.

inplace也很重要.例如

b = a.T # a transpose

对于a[:]=,b的行将重新排序. a和b继续共享相同的data.对于a=,b保持不变. a和b现在已解耦.

With a[:]= the rows of b will be reordered. a and b continue to share the same data. With a=, b is unchanged. a and b are now decoupled.

更多推荐

根据给定索引就地numpy数组排序

本文发布于:2023-11-30 17:34:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!