I guess you'd better create an empty 2D array and fill it later...
import numpy as np
N = how_many()
L = how_long()
complete = np.empty((N, L), dtype=float)
for i in range(N):
complete(i, :) = extract_feature(i)
In place of dtype=float you can use a different numeric type, as required by your problem.
E.g.,
In [14]: a = np.empty((2,4), dtype=float)
In [15]: for i in (0, 1):
...: a[i,:] = np.ones(4)*i
...:
In [16]: a
Out[16]:
array([[0., 0., 0., 0.],
[1., 1., 1., 1.]])
Addendum
Comment on efficiency If one knows beforehand the dimensions of an array they are going to construct row-by-row, the approach sketched above is better, because it avoids constantly
alllocating new memory, copying the temporary result and the new row to the new memory and deallocating the memory used to hold the previous temporary result.
Alternative using concatenation If, on the other hand, one doesn't know beforehand how many rows will be produced during the creation of the array, or if one insists on using a sub-optimal solution, they can use np.vstack, encapsulating the productions of new rows in a generator
import numpy as np
def features_factory(stuff):
while true:
feature = new_feature(stuff)
if feature:
yield feature
else:
return
complete = np.vstack(features_factory(todays_stuff))
E.g.,
In [1]: import numpy as np
In [2]: np.random.seed((2018+7+8)) # today's stuff... ;)
In [3]: def features_factory(stuff):
...: n = 0
...: while True:
...: if n<stuff:
...: yield np.ones(5)*n
...: n = n+1
...: else:
...: return
In [4]: complete = np.vstack(features_factory(np.random.randint(5,11)))
In [5]: complete
Out[5]:
array([[0., 0., 0., 0., 0.],
[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5.],
[6., 6., 6., 6., 6.],
[7., 7., 7., 7., 7.]])
In [6]:
(86,), and i have 128 of those, So i would like to loop through these array's and get shape of(128, 86), hope im clear here