0

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

user1315789
  • 3,069
  • 6
  • 18
  • 41
  • What have you tried ? Simple reshape would do the job. – Krishna Aug 19 '18 at 11:40
  • Yes, I felt stupid. I got confused by the answer here and thought it's going to be complicated. https://stackoverflow.com/questions/14476415/reshape-an-array-in-numpy – user1315789 Aug 19 '18 at 12:20

1 Answers1

2

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))
joni
  • 6,840
  • 2
  • 13
  • 20