【问题标题】:I want to reshape 2D array into 3D array我想将 2D 数组重塑为 3D 数组
【发布时间】:2018-04-27 01:15:57
【问题描述】:

我想将 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: cannot reshape array of size 148 into shape (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],但输出不是我理想的。我的代码有什么问题?

【问题讨论】:

  • 你的二维数组的维度是多少,有多少个元素?排?和列?
  • @Spandy 数组有 2 组 4 个元素,例如 ["100","150","2","4"]。总元素是 2 组 4 个元素的数个。跨度>
  • @Spandy 所以长度的个数不固定。

标签: python numpy


【解决方案1】:

首先,你错过了重塑的意义。假设您的原始数组具有形状(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))

希望有帮助:)

【讨论】:

  • 为了更清楚,你总是可以将未知维度设置为-1。
【解决方案2】:

你不需要循环来重塑你想要的方式,只需使用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 将推断未知维度的值。

【讨论】:

    【解决方案3】:
    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]]]])
    

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 2014-08-05
      • 2015-10-19
      • 2019-09-12
      相关资源
      最近更新 更多