繁体   English   中英

我想将2D数组重塑为3D数组

[英]I want to reshape 2D array into 3D array

我想将2D数组重塑为3D数组。我编写了代码,

for i in range(len(array)):
    i = np.reshape(i,(2,2,2))
    print(i)

i变量具有偶数长度数组,例如[["100","150","2","4"],["140","120","3","5"]]

[[“1”,”5”,”6”,”2”],[“4”,”2”,”3”,”7”],[“7”,”5”,”6”,”6”],[“9”,”1”,”8”,”3”],[“3”,”4”,”5”,”6”],[“7”,”8”,”9”,”2”],,[“1”,”5”,”2”,”8”],[“6”,”7”,”2”,”1”],[“9”,”3”,”1”,”2”],[“6”,”8”,”3”,”3”]]

长度> =6。当我运行此代码时,ValueError:无法将大小为148的数组重塑为形状(2,2,2)的错误。 我理想的输出是

[[['100', '150'], ['2', '4']], [['140', '120'], ['3', '5']]] or [[[“1”,”5”],[”6”,”2”]],[[“4”,”2”],[”3”,”7”]],[[“7”,”5”],[”6”,”6”]],[[“9”,”1”],[”8”,”3”]],[[“3”,”4”],[”5”,”6”]],[[“7”,”8”],[”9”,”2”]],[[“1”,”5”],[”2”,”8”]],[[“6”,”7”],[”2”,”1”]],[[“9”,”3”],[[”1”,”2”]],[[“6”,”8”],[”3”,”3”]]] 

我将代码y = [[x[:2], x[2:]] for x in i]改写y = [[x[:2], x[2:]] for x in i]但输出不是我的理想代码。我的代码有什么问题?

您无需循环即可重塑所需的方式,只需使用arr.reshape((-1,2,2))

In [3]: x = np.random.randint(low=0, high=10, size=(2,4))

In [4]: x
Out[4]:
array([[1, 1, 2, 5],
       [8, 8, 0, 5]])

In [5]: x.reshape((-1,2,2))
Out[5]:
array([[[1, 1],
        [2, 5]],

       [[8, 8],
        [0, 5]]])

此方法将对您的两个阵列均适用。 作为第一个参数-1意味着numpy将推断未知维的值。

首先,您缺少重塑的含义。 假设您的原点数组的形状为(A,B),并且想要对其进行重塑以使其形状为(M,N,O),则必须确保A * B = M * N *O。显然148!= 2 * 2 * 2,对吗?

在您的情况下,您想将形状(N,4)的数组重塑为形状(N,2,2)的数组。 您可以执行以下操作:

x = np.reshape(y, (-1, 2, 2))

希望这个帮助:)

In [76]: arr = np.arange(24).reshape(3,8)
In [77]: for i in range(len(arr)):
    ...:     print(i)
    ...:     i = np.reshape(i, (2,2,2))
    ...:     print(i)
    ...:     
0
....

AttributeError: 'int' object has no attribute 'reshape'

len(arr)为3,因此range(3)产生值0,1,2。 您不能重塑数字0

还是您要重塑arr[0]arr[1]等?

In [79]: for i in arr:
    ...:     print(i)
    ...:     i = np.reshape(i, (2,2,2))
    ...:     print(i)
    ...:     
[0 1 2 3 4 5 6 7]
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]
[ 8  9 10 11 12 13 14 15]
[[[ 8  9]
  [10 11]]

 [[12 13]
  [14 15]]]
[16 17 18 19 20 21 22 23]
[[[16 17]
  [18 19]]

 [[20 21]
  [22 23]]]

那行得通-有点。 打印看起来还可以,但是arr本身并没有改变:

In [80]: arr
Out[80]: 
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]])

那是因为i是迭代变量。 向其分配新值不会更改原始对象。 如果这令人困惑,则需要复习基本的Python迭代。

或者我们可以迭代范围,并将其用作索引:

In [81]: for i in range(len(arr)):
    ...:     print(i)
    ...:     x = np.reshape(arr[i], (2,2,2))
    ...:     print(x)
    ...:     arr[i] = x
    ...: 
    ...: 
    ...: 
    ...:     
0
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-81-5f0985cb2277> in <module>()
      3     x = np.reshape(arr[i], (2,2,2))
      4     print(x)
----> 5     arr[i] = x
      6 
      7 

ValueError: could not broadcast input array from shape (2,2,2) into shape (8)

整形有效,但是您不能将(2,2,2)数组放回形状为(8,)的插槽中。 元素的数量是正确的,但是形状是不正确的。

换句话说,您无法零碎地重整数组。 您必须重塑整个事情。 (如果arr是列表列表,则这种零星的重塑将起作用。)

In [82]: np.reshape(arr, (3,2,2,2))
Out[82]: 
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]]]])

暂无
暂无

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

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