How to convert a list into a numpy 2D ndarray. For example:
lst = [20, 30, 40, 50, 60]
Expected result:
>> print(arr)
>> array([[20],
[30],
[40],
[50],
[60]])
>> print(arr.shape)
>> (5, 1)
If you need your calculations more effective, use numpy arrays instead of list comprehensions. This is an alternative way using array broadcasting
x = [20, 30, 40, 50, 60]
x = np.array(x) #convert your list to numpy array
result = x[:, None] #use numpy broadcasting
if you still need a list type at the end, you can convert your result efficiently using result.tolist()
None is an alias for np.newaxisYou may use a list comprehension and then convert it to numpy array:
import numpy as np
x = [20, 30, 40, 50, 60]
x_nested = [[item] for item in x]
x_numpy = np.array(x_nested)
[[i] for i in x]ornp.reshape([20, 30, 40, 50, 60], (-1,1))?