展平3-D numpy数组

dok*_*ndr 2 python arrays numpy

如何扁平化:

b = np.array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[1,1,1],[2,2,2],[3,3,3]]
])
Run Code Online (Sandbox Code Playgroud)

变成:

c = np.array([
    [1,2,3,4,5,6,7,8,9],
    [1,1,1,2,2,2,3,3,3]
])
Run Code Online (Sandbox Code Playgroud)

这些工作无足轻重:

c = np.apply_along_axis(np.ndarray.flatten, 0, b)
c = np.apply_along_axis(np.ndarray.flatten, 0, b)
Run Code Online (Sandbox Code Playgroud)

只是返回相同的数组。

最好将其放平。

Mir*_*ber 5

这将完成工作:

c=b.reshape(len(b),-1)
Run Code Online (Sandbox Code Playgroud)

然后c

array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
       [1, 1, 1, 2, 2, 2, 3, 3, 3]])
Run Code Online (Sandbox Code Playgroud)