0
matrix = np.array([[10,450],[110,250],[200,500]])

reshaped = matrix.reshape(-1,1,2)

How will the matrix be reshaped? And what is the meaning of (-1,1,2)?

  • Take time to read the docs, https://numpy.org/doc/stable/reference/generated/numpy.reshape.html – hpaulj Jul 28 '21 at 16:21
  • Well, I know what two parameters are used for in np.reshape() function, but as the code and question stated, there are three parameters, why are we giving three parameters and what will it do? – Rishav Mitra Aug 20 '21 at 06:40
  • One number for each dimension in the result. `reshape` can change the number of dimensions, in this case from 2d to 3d. Given the way arrays are stored and manipulated, handling 3d (or even higher) arrays is no different from 2d or 1d. – hpaulj Aug 20 '21 at 07:06

1 Answers1

0

There are 3 arguments in the reshape, so the result will be a 3-dimensional array. The 2nd dimension will have size 1, the 3rd dimension size 2. The -1 for the 1st dimension means that the size of this 1st dimension is calculated on-the-fly, such that all the elements nicely fit.

In this specific case, the result is therefore

array([[[ 10, 450]],
       [[110, 250]],
       [[200, 500]]])
OrOrg
  • 212
  • 2
  • 8