0

I want to reshape

[1,2,3,4]

into:

[[1],[2],[3],[4]]

Is there a simple numpy method to do this?

mm_
  • 1,566
  • 2
  • 18
  • 37

2 Answers2

3

Use

reshaped_arr = arr.reshape(4,1)

Testing it out:

import numpy as np

arr = np.arange(4)
reshaped_arr = arr.reshape(4,1)
print (reshaped_arr)

Output:

[[0]
 [1]
 [2]
 [3]]
fountainhead
  • 3,584
  • 1
  • 8
  • 17
2

There are a few ways. You can use the reshape method:

a = np.array([1, 2, 3, 4])
a.reshape(len(a), -1)

Or the reshape free function:

np.reshape(a, (len(a), -1))

Or np.newaxis:

a[:, np.newaxis]

All of these give the same output:

array([[1],
       [2],
       [3],
       [4]])
gmds
  • 19,325
  • 4
  • 32
  • 58