我是一个 Numpy 初学者,如何对 3-D 数组进行排序?有什么文件可以理解吗?

问题描述 投票:0回答:1

我有以下数组。

c = np.array([[(1,2,3,4),(4,5,6,7)],[(3,4,5,6),(4,3,1,9)],[(9,8,0,2),(4,2,7,5)]])
c = array([[[1, 2, 3, 4],
        [4, 5, 6, 7]],

       [[3, 4, 5, 6],
        [4, 3, 1, 9]],

       [[9, 8, 0, 2],
        [4, 2, 7, 5]]])
ca = np.sort(c, axis = 0)
ca = array([[[1, 2, 0, 2],
        [4, 2, 1, 5]],

       [[3, 4, 3, 4],
        [4, 3, 6, 7]],

       [[9, 8, 5, 6],
        [4, 5, 7, 9]]])

我明白我的阵列看起来像这样

(1,2,3,4),(4,5,6,7)

(3,4,5,6),(4,3,1,9)

(9,8,0,2),(4,2,7,5)

如果我进入 axis = 0,我得到每列的列(向下)排序,那应该给我

(1,2,0,2)(4,2,1,5).

但是我是怎么得到其他结果的?

[[3, 4, 3, 4], [4, 3, 6, 7]],

[[9, 8, 5, 6], [4, 5, 7, 9]]])

请帮我理解这个?

python numpy numpy-ndarray
1个回答
0
投票

在轴 2 上排序是最容易看到的 - 尽管您的大部分行已经排序:

In [81]: np.sort(c, axis=2)
Out[81]: 
array([[[1, 2, 3, 4],
        [4, 5, 6, 7]],

       [[3, 4, 5, 6],
        [1, 3, 4, 9]],      # change

       [[0, 2, 8, 9],       # and here
        [2, 4, 5, 7]]])     # and

在每一列中(轴 1,尺寸 2):

In [82]: np.sort(c, axis=1)
Out[82]: 
array([[[1, 2, 3, 4],
        [4, 5, 6, 7]],

       [[3, 3, 1, 6],
        [4, 4, 5, 9]],

       [[4, 2, 0, 2],
        [9, 8, 7, 5]]])

使用轴 0 和尺寸 3 时没有区别,只是很难想象

In [83]: np.sort(c, axis=0)
Out[83]: 
array([[[1, 2, 0, 2],
        [4, 2, 1, 5]],

       [[3, 4, 3, 4],
        [4, 3, 6, 7]],

       [[9, 8, 5, 6],
        [4, 5, 7, 9]]])

c
有一个 (5,3,2) 序列,现在是 (2,3,5);和现在已排序的 (3,5,0) 等

In [85]: np.sort(c, axis=0)[:,1,1]
Out[85]: array([2, 3, 5])

任何 [:,i,j] 选择将被排序。

© www.soinside.com 2019 - 2024. All rights reserved.