将此(4,4)numpy ndarray重塑为(2,2,4)ndarray

时间:2018-08-19 11:32:55

标签: python-3.x numpy numpy-ndarray

我有这个形状为nd的numpy ndarray (4,4)

[
    [1.07 1.16 1.00 1.11]
    [1.13 1.19 1.11 1.17]
    [1.17 1.17 1.13 1.16]
    [1.14 1.16 1.03 1.04]
]

我想将其重塑成一个(2,2,4)的ndarray,它应该看起来像这样。

[
    [
        [1.07 1.16 1.00 1.11]
        [1.13 1.19 1.11 1.17]
    ]
    [
        [1.17 1.17 1.13 1.16]
        [1.14 1.16 1.03 1.04]
    ]
]

我正在使用python v3.6

1 个答案:

答案 0 :(得分:2)

您可以使用reshape

import numpy as np
nd = np.array([
    [1.07, 1.16, 1.00, 1.11],
    [1.13, 1.19, 1.11, 1.17],
    [1.17, 1.17, 1.13, 1.16],
    [1.14, 1.16, 1.03, 1.04]])

nd_new = np.reshape(nd, (2,2,4))