I want to convert this list
li = [3, 2 , 1 , 4]
to the following NumPy ndarray
[[3], [2], [1], [4]]
I tried with np.asarray() but it is not converting it into an ndarray. Is there any way to mention the axis? 
You can use numpy.expand_dims to create an array with an extra axis,
>>> np.expand_dims(li, -1)
array([[3],
       [2],
       [1],
       [4]])
or if you prefer, add the axis after array creation.
np.array(li)[..., np.newaxis]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With