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.
1 Answer 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]])
emptyandappendare not list clones.