简体   繁体   English

如何将Numpy数组从(x,y,z)重塑为(y,z,x)

[英]How to reshape a Numpy array from (x,y,z) to (y,z,x)

I have an array of dimension (3,120,100) and I want to convert it into an array of dimensions (120,100,3). 我有一个尺寸为(3,120,100)的数组,我想将其转换为尺寸为(120,100,3)的数组。 The array I have is 我有的数组是

arr1 = np.ones((120,100), dtype = int)
arr2 = arr1*2
arr3 = arr1*3
arr = np.stack((arr1,arr2,arr3))
arr

It contains three 120x100 arrays of 1's, 2's, and 3's. 它包含三个120x100的1、2和3数组。 When I use reshape on it, I get 120x100 arrays of 1's, 2's, or 3's. 当我在其上使用reshape时,得到120x100的1、2或3数组。

I want to get an array of 120x100 where each element is [1,2,3] 我想得到一个120x100的数组,其中每个元素是[1,2,3]

If you want a big array containing 1 , 2 and 3 as you describe, user3483203's answer would be the recommendable option. 如果你想包含一个大阵列123是你所描述, user3483203的回答将是值得推荐的选择。 If you have, in general, an array with shape (X, Y, Z) and you want to have it as (Y, Z, X) , you would normally use np.transpose : 通常,如果您有一个形状为(X, Y, Z)的数组,并且希望将其设置为(Y, Z, X) ,通常可以使用np.transpose

import numpy as np

arr = ... # Array with shape (3, 120, 100)
arr_reshaped = np.transpose(arr, (1, 2, 0))
print(arr_reshaped.shape)
# (120, 100, 3)

EDIT: The question title says you want to reshape an array from (X, Y, Z) to (Z, Y, X) , but the text seems to suggest you want to reshape from (X, Y, Z) to (Y, Z, X) . 编辑:问题标题说您想将数组从(X, Y, Z)重塑为(Z, Y, X) ,但文本似乎暗示您想从(X, Y, Z)重塑为(Y, Z, X) I followed the text, but for the one in the title it would simply be np.transpose(arr, (2, 1, 0)) . 我紧跟着文字,但标题中的文字只是np.transpose(arr, (2, 1, 0))

You don't need to create a very large array and reshape. 您无需创建非常大的数组并进行整形。 Since you know what you want each element to be, and the final shape, you can just use numpy.broadcast_to . 由于您知道每个元素的形状和最终形状,因此可以使用numpy.broadcast_to This requires a setup of just creating a shape (3,) array. 这需要仅创建形状(3,)数组的设置。

Setup 设定

arr = np.array([1,2,3])

np.broadcast_to(arr, (120, 100, 3))

array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        ...,
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],

       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        ...,
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]]])

To get a non read-only version of this output, you can call copy() : 要获得此输出的非只读版本,可以调用copy()

out = np.broadcast_to(arr, (120, 100, 3)).copy()

I'll answer this assuming it's part of a larger problem, and this is just example data to demonstrate what you want to do. 我将回答这个问题,假设它是一个更大的问题的一部分,而这仅仅是示例数据,用以演示您想要做的事情。 Otherwise the broadcasting solution works just fine. 否则,广播解决​​方案就可以正常工作。

When you use reshape it doesn't change how numpy interprets the order of individual elements. 当您使用reshape它不会改变numpy解释单个元素顺序的方式。 It simply affects how numpy views the shape. 它仅影响numpy 如何查看形状。 So, if you have elements a, b, c, d in an array on disk that can be interpreted as an array of shape (4,), or shape (2, 2), or shape (1, 4) and so on. 因此,如果磁盘上的数组中的元素a, b, c, d可以解释为形状(4,)或形状(2,2)或形状(1,4)等的数组。

What it seems you're looking for is transpose . 您似乎正在寻找的是transpose This affects allows swapping how numpy interprets the axes. 这会影响交换numpy解释轴的方式。 In your case 就你而言

>>>arr.transpose(2,1,0)
array([[[1, 2, 3],
    [1, 2, 3],
    [1, 2, 3],
    ..., 
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM