[英]Rearrange rows of a given numpy 2D array given a list with the permutations
There's an incredibly simple way of permuting the columns of a 2d array with numpy like this:有一种非常简单的方法可以像这样用 numpy 置换 2d 数组的列:
array1 = np.array([[11, 22, 33, 44, 55],
[66, 77, 88, 99, 100]])
print("Original array:")
print(array1)
permutation = [1,3,0,4,2]
result = array1[:, permutation]
print("New array:")
print(result)
This outputs:这输出:
Original array:
[[ 11 22 33 44 55]
[ 66 77 88 99 100]]
New array:
[[ 22 44 11 55 33]
[ 77 99 66 100 88]]
Visual representation (from w3resource.com)视觉表示(来自 w3resource.com)
Is there a way to acomplish the same thing as elegantly but for the rows instead?有没有办法优雅地完成同样的事情,但是对于行呢?
As @Marat mentioned in the comments, you can do the same by similar advanced indexing that you described for columns:正如@Marat 在评论中提到的,您可以通过为列描述的类似高级索引来执行相同的操作:
array1 = np.array([[11, 22, 33, 44, 55],
[66, 77, 88, 99, 100]])
permutation = [1,0]
array1[permutation]
#[[ 66 77 88 99 100]
# [ 11 22 33 44 55]]
When you advance index numpy array, the default is calling rows.当您推进索引 numpy 数组时,默认是调用行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.