user1315789
user1315789

Reputation: 3659

Reshape this (4,4) numpy ndarray to (2,2,4) ndarray

I have this numpy ndarray nd with shape (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]
]

I would like to reshape it to a ndarray to (2,2,4) and it should look like this.

[
    [
        [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]
    ]
]

I am using python v3.6

Upvotes: 0

Views: 128

Answers (1)

joni
joni

Reputation: 7157

You could use 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))

Upvotes: 2

Related Questions