Create dynamic 2D numpy array on the fly
I'm having a hard time creating a numpy
2D array on the fly.
So basically I have a loop for something like this.
for ele in huge_list_of_lists:
instance = np.array(ele) # creates a 1D numpy array of this list
# and now I want to append it to a numpy array
# so basically converting list of lists to array of arrays?
# i have checked the manual.. and np.append() methods
that doesnt work as for np.append() it needs two arguments to append it together
Any hints?
source to share
Create a 2D array in front and fill in the lines while looping:
my_array = numpy.empty((len(huge_list_of_lists), row_length))
for i, x in enumerate(huge_list_of_lists):
my_array[i] = create_row(x)
where create_row()
returns a list or 1D array of NumPy length row_length
.
Depending on what it does create_row()
, there may be even better approaches that avoid the Python loop altogether.
source to share
Just pass a list of lists to numpy.array
, keep in mind that the arrays are numpy ndarrays
, so the concept to list of lists does not convert to arrays of arrays, this translates to a 2d array.
>>> import numpy as np
>>> a = [[1., 2., 3.], [4., 5., 6.]]
>>> b = np.array(a)
>>> b
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
>>> b.shape
(2, 3)
Also ndarrays are nd-indexed, so [1][1]
it becomes [1, 1]
in numpy:
>>> a[1][1]
5.0
>>> b[1, 1]
5.0
Am I misunderstanding your question?
You defiantly don't want to use it numpy.append
for something like this. Be aware that numpy.append has an O (n) runtime, so if you call it n times, once for each row of your array, you end up with an O (n ^ 2) algorithm. If you need to create an array before you know what all the content will be, but you know the final size, your best bet is to create an array with numpy.zeros(shape, dtype)
and fill it later. Like Sven's answer.
source to share
import numpy as np
ss = np.ndarray(shape=(3,3), dtype=int);
array([[ 0, 139911262763080, 139911320845424],
[ 10771584, 10771584, 139911271110728],
[139911320994680, 139911206874808, 80]]) #random
Function numpy.ndarray achieves this. numpy.ndarray
source to share