I am pretty new at python and numpy, so sorry if the question is kind of obvious. Looking on the internet I could not find the right answer. I need to create a 2 dimensions (a.ndim -->2) array of unknown size in python. Is it possible? I have found a way for 1 dimension passing through a list but no luck with 2 dimension.
example
for i in range(0,Nsens):
count=0
for l in range (0,my_data.shape[0]):
if my_data['Node_ID'][l]==sensors_name[i]:
temp[count,i]=my_data['Temperature'][l]
count=count+1
else:
count=count
Where temp is the array I need to initialize.
This shows a fairly high-performance (although slower than initializing to exact size) way to fill in an array of unknown size in numpy:
data = numpy.zeros( (1, 1) )
N = 0
while True:
row = ...
if not row: break
# assume every row has shape (K,)
K = row.shape[0]
if (N >= data.shape[0]):
# over-expand: any ratio around 1.5-2 should produce good behavior
data.resize( (N*2, K) )
if (K >= data.shape[1]):
# no need to over-expand: presumably less common
data.resize( (N, K+1) )
# add row to data
data[N, 0:K] = row
# slice to size of actual data
data = data[:N, :]
Adapting to your case:
if count > temp.shape[0]:
temp.resize( (max( temp.shape[0]*2, count+1 ), temp.shape[1]) )
if i > temp.shape[1]:
temp.resize( (temp.shape[0], max(temp.shape[1]*2, i+1)) )
# now safe to use temp[count, i]
You may also want to keep track of the actual data sizes (max count, max i) and trim the array later.
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