沿特定轴将另一个数组排序为一个numpy数组 [英] Sort a numpy array by another array, along a particular axis
问题描述
类似于此答案,我有一对3D numpy数组a
和b
,我想按a
的值对b
的条目进行排序.与此答案不同,我想进行排序仅沿数组的一个轴.
Similar to this answer, I have a pair of 3D numpy arrays, a
and b
, and I want to sort the entries of b
by the values of a
. Unlike this answer, I want to sort only along one axis of the arrays.
我对numpy.argsort()
文档的幼稚阅读:
Returns
-------
index_array : ndarray, int
Array of indices that sort `a` along the specified axis.
In other words, ``a[index_array]`` yields a sorted `a`.
让我相信我可以使用以下代码进行排序:
led me to believe that I could do my sort with the following code:
import numpy
a = numpy.zeros((3, 3, 3))
a += numpy.array((1, 3, 2)).reshape((3, 1, 1))
print "a"
print a
"""
[[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 3. 3. 3.]
[ 3. 3. 3.]
[ 3. 3. 3.]]
[[ 2. 2. 2.]
[ 2. 2. 2.]
[ 2. 2. 2.]]]
"""
b = numpy.arange(3*3*3).reshape((3, 3, 3))
print "b"
print b
"""
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
"""
print "a, sorted"
print numpy.sort(a, axis=0)
"""
[[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 2. 2. 2.]
[ 2. 2. 2.]
[ 2. 2. 2.]]
[[ 3. 3. 3.]
[ 3. 3. 3.]
[ 3. 3. 3.]]]
"""
##This isnt' working how I'd like
sort_indices = numpy.argsort(a, axis=0)
c = b[sort_indices]
"""
Desired output:
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[18 19 20]
[21 22 23]
[24 25 26]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]]
"""
print "Desired shape of b[sort_indices]: (3, 3, 3)."
print "Actual shape of b[sort_indices]:"
print c.shape
"""
(3, 3, 3, 3, 3)
"""
什么是正确的方法?
推荐答案
您仍然必须为其他两个维度提供索引,以使其正常工作.
You still have to supply indices for the other two dimensions for this to work correctly.
>>> a = numpy.zeros((3, 3, 3))
>>> a += numpy.array((1, 3, 2)).reshape((3, 1, 1))
>>> b = numpy.arange(3*3*3).reshape((3, 3, 3))
>>> sort_indices = numpy.argsort(a, axis=0)
>>> static_indices = numpy.indices((3, 3, 3))
>>> b[sort_indices, static_indices[1], static_indices[2]]
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]])
numpy.indices
计算当通过其他两个轴(或n-1轴,其中n =轴的总数)展平"时,数组每个轴的索引.换句话说,这(对冗长的帖子表示歉意):
numpy.indices
calculates the indices of each axis of the array when "flattened" through the other two axes (or n - 1 axes where n = total number of axes). In other words, this (apologies for the long post):
>>> static_indices
array([[[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]],
[[2, 2, 2],
[2, 2, 2],
[2, 2, 2]]],
[[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]],
[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]],
[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]]],
[[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]]])
这些是每个轴的标识索引;当用于索引b时,它们将重新创建b.
These are the identity indices for each axis; when used to index b, they recreate b.
>>> b[static_indices[0], static_indices[1], static_indices[2]]
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
作为numpy.indices
的替代方法,您可以使用uncbu建议的numpy.ogrid
.由于ogrid
生成的对象较小,因此,出于一致性考虑,我将创建所有三个轴,但是请注意unutbu的注释,以通过仅生成两个轴来实现此目的.
As an alternative to numpy.indices
, you could use numpy.ogrid
, as unutbu suggests. Since the object generated by ogrid
is smaller, I'll create all three axes, just for consistency sake, but note unutbu's comment for a way to do this by generating only two.
>>> static_indices = numpy.ogrid[0:a.shape[0], 0:a.shape[1], 0:a.shape[2]]
>>> a[sort_indices, static_indices[1], static_indices[2]]
array([[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]],
[[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]],
[[ 3., 3., 3.],
[ 3., 3., 3.],
[ 3., 3., 3.]]])
这篇关于沿特定轴将另一个数组排序为一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!