0

I want to append a numpy array to a empty numpy array but it's not working.

reconstructed = numpy.empty((4096,))
to_append = reconstruct(p, e_faces, weights, mu, i)

# to_append=array([129.47776809, 129.30775937, 128.90932868, ..., 103.64777681, 104.99912816, 105.93984307]) It's shape is (4096,)

numpy.append(reconstructed, to_append, axis=0)
#Axis is not working anyway.

Plz help me. I want to put that long array in the empty one. The result is just empty.

6
  • Don't try to do this. empty and append are not list clones. Commented Jan 9, 2021 at 16:06
  • What do you mean list clones? Commented Jan 9, 2021 at 16:13
  • can you pass the numbers of to_append list or give an example case? Commented Jan 9, 2021 at 16:23
  • Have you read the documentation for either? Commented Jan 9, 2021 at 16:36
  • # to_append=array([129.47776809, 129.30775937, 128.90932868, ..., 103.64777681, 104.99912816, 105.93984307]) It's shape is (4096,) I already wrote it in. Are you asking for something else? Commented Jan 9, 2021 at 16:37

1 Answer 1

1

Look at what empty produces:

In [140]: x = np.empty((5,))
In [141]: x
Out[141]: array([0.  , 0.25, 0.5 , 0.75, 1.  ])

append makes a new array; it does not change x

In [142]: np.append(x, [1,2,3,4,5], axis=0)
Out[142]: array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.  , 2.  , 3.  , 4.  , 5.  ])
In [143]: x
Out[143]: array([0.  , 0.25, 0.5 , 0.75, 1.  ])

we have to assign it to a new variable:

In [144]: y = np.append(x, [1,2,3,4,5], axis=0)
In [145]: y
Out[145]: array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.  , 2.  , 3.  , 4.  , 5.  ])

Look at that y - those random values that were in x are also in y!

Contrast that with list

In [146]: alist = []
In [147]: alist
Out[147]: []
In [148]: alist.append([1,2,3,4,5])
In [149]: alist
Out[149]: [[1, 2, 3, 4, 5]]

The results are very different. Don't use this as a model for creating arrays.

If you need to build an array row by row, use the list append to collect the rows in one list, and then make the array from that.

In [150]: z = np.array(alist)
In [151]: z
Out[151]: array([[1, 2, 3, 4, 5]])
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Thank you so much. Got it not to build an empty array to append!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.