The problem:
I have a numpy array
tf = numpy.full((500, 4, 1, 2), [500, 1])
tf :
array([[[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]]],
...,
[[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]]]])
tf.shape :
(500, 4, 1, 2)
Consider the first group:
tf[0][0] this is : array([[ 500., 1.]])
I need to be able to append (in place) additional values say [[100, 0.33], [1, 0.34], [15, 0.33]] so that the end result looks like (this operation is carried out for each of the elements):
tf :
array([[[[ 500., 1.], [100., 0.33], [1., 0.34], [15., 0.33]],
[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]]],
...,
[[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]],
[[ 500., 1.]]]])
I tried numpy.concatenate((tf[0][0], [[100, 0.33]]), axis = 0) this returns a new appended ndarray, but I cannot assign it back to tf[0][0] as it fails with the following error.
ValueError: could not broadcast input array from shape (2,2) into shape (1,2)
Is there any other way to achieve what I want using numpy?
==========================================================
Inefficient list way of doing it:
# initialization
tf = [[[]] for i in xrange(500)]
for i in xrange(500):
tf[i] = [[] for a in xrange(4)]
for j in xrange(4):
tf[i][j].append([500, 1.0])
# usage: (for any 0 < i < 500; 0 < j < 4 )
tf[i][j].append([100, 0.33])
but this is inefficient (considering I need to do this over a million times)
numpy.ndarrayswhich are true, fixed-size multidimensional arrays. The efficient way to do this is with a list.this operation is carried out for each of the elements- So it seems it won't be a ragged array, but a regular one.ndarrayyour only option is to use some sort ofdtype=objectarray, which will essentially be a bad list. In what sense of "inefficient" do you mean "inefficient list way"?