使用 numpy 对数组进行排序
Sorting array with numpy
我想更改
中列元素的顺序
a = np.asarray(
[[0,1,1,2,2,2,2,3,3,3,4,4,4,4,4,4],
[4,0,3,0,1,2,5,1,2,5,3,4,6,6,7,7],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,1,0,0,0,1,1,0,1,0,1]]
)
基于第 1-3 行的值(从 0 开始)。我的解决方案目前如下所示:
a[:, a.transpose()[:, 1].argsort(axis=0)]
array([[1, 2, 2, 3, 2, 3, 1, 4, 0, 4, 2, 3, 4, 4, 4, 4],
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1]])
很好,除了我还想在搜索中包括第 2-3 行(按字典顺序)。理想情况下,我希望最后一行是 [0, 1, 0, 1, ..., 0, 1]
的结果(也应考虑充满零的第二行,但在本例中它包含相同的值)。
你需要numpy.lexsort,相当于argsort
但是基于多个排序键;给定多个数组,它 returns 对数组进行排序的索引:
Given multiple sorting keys, which can be interpreted as columns in a
spreadsheet, lexsort returns an array of integer indices that
describes the sort order by multiple columns. The last key in the
sequence is used for the primary sort order, the second-to-last key
for the secondary sort order, and so on. The keys argument must be a
sequence of objects that can be converted to arrays of the same shape.
If a 2D array is provided for the keys argument, it’s rows are
interpreted as the sorting keys and sorting is according to the last
row, second last row etc.
a[:, np.lexsort(a[:0:-1])]
#array([[2, 1, 3, 2, 3, 2, 1, 4, 0, 4, 3, 2, 4, 4, 4, 4],
# [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]])
我想更改
中列元素的顺序a = np.asarray(
[[0,1,1,2,2,2,2,3,3,3,4,4,4,4,4,4],
[4,0,3,0,1,2,5,1,2,5,3,4,6,6,7,7],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,1,0,0,0,1,1,0,1,0,1]]
)
基于第 1-3 行的值(从 0 开始)。我的解决方案目前如下所示:
a[:, a.transpose()[:, 1].argsort(axis=0)]
array([[1, 2, 2, 3, 2, 3, 1, 4, 0, 4, 2, 3, 4, 4, 4, 4],
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1]])
很好,除了我还想在搜索中包括第 2-3 行(按字典顺序)。理想情况下,我希望最后一行是 [0, 1, 0, 1, ..., 0, 1]
的结果(也应考虑充满零的第二行,但在本例中它包含相同的值)。
你需要numpy.lexsort,相当于argsort
但是基于多个排序键;给定多个数组,它 returns 对数组进行排序的索引:
Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it’s rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc.
a[:, np.lexsort(a[:0:-1])]
#array([[2, 1, 3, 2, 3, 2, 1, 4, 0, 4, 3, 2, 4, 4, 4, 4],
# [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]])