Python Numpy将3D列表重塑为2D数组

时间:2019-04-23 11:57:09

标签: python numpy reshape flatten

我希望转换

edit:下面的代码是列表的3D列表

[
    [ 
        [
            [1,2,3,],
            [4,5,6,],
        ],
        [
            [7,8,9,],
            [10,11,12,], 
        ],
    ],
    [ 
        [
            [A,B,C,],
            [D,E,F,], 
        ],
        [
            [G,H,I,],
            [J,K,L,], 
        ],
    ],
]

进入

[

[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
[A,B,C],
[D,E,F],
[G,H,I],
[J,K,L]

]

我尝试了numpy.flatten但没有成功     https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

1 个答案:

答案 0 :(得分:2)

重塑是您的工具。

这是一个自包含的示例:

import numpy as np
a = np.array([
    [[[1,2,3] , [4,5,6]],
     [[7,8,9] , [10,11,12]]],
    [[[13,14,15] , [16,17,18]],
     [[19,20,21] , [22,23,24]]]
    ])

a.shape
>>> (2, 2, 2, 3)

a.reshape(8,3)
>>> array([[ 1,  2,  3],
>>>        [ 4,  5,  6],
>>>        [ 7,  8,  9],
>>>        [10, 11, 12],
>>>        [13, 14, 15],
>>>        [16, 17, 18],
>>>        [19, 20, 21],
>>>        [22, 23, 24]])