3

In order to store many 3D points coordinates tuples in a numpy.ndarray I initialize an empty numpy array before entering a loop for each of some features.

To do so, I currently do this before entering the loop:

import numpy as np
pointsarrray = np.empty((1,3))

but this results in an array which is all but empty:

array([[  5.30498948e-315,   0.00000000e+000,   7.81250000e-003]])

When filling pointsarray in my loop after, I do this:

pointsarray = np.vstack((pointsarray, [np.array(myPoint)]))

(it also works with np.append)

and I finally need to delete the first line of the array after exiting the loop because this first line always contains the values from the initialization step!

It's not a big deal but I wonder if there is a cleaner way to achieve a really empty array, I mean; with nothing inside it (it shows 1 row yet, I can not figure out why) but at the right dimensions?

5
  • 3
    What's your definition of really empty array? Commented Jun 7, 2017 at 20:09
  • An empty array has zero dimensions. An array with non-zero dimensions is not empty. Commented Jun 7, 2017 at 20:10
  • Are you aware of the underlying basics? E.g. uninitialized C-arrays? Allocating memory and pre-setting memory are two different things. And empty just means, you are not initializing it with values (so any garbage from memory will live there until overwritten). Commented Jun 7, 2017 at 20:13
  • <at>Divakar : My definition in this case would have been an array with nothing in it, i.e. no 1st row as my example shown. <at>sascha : No I'm not really aware of these memory underlying basics but it's alway good to have some clues; thanks. Commented Jun 7, 2017 at 20:20
  • Well, you asked for a row and see stuff from memory (because of no init). Psidom's answer shows what you maybe did try to do. Commented Jun 7, 2017 at 20:21

2 Answers 2

9

You need the shape to be (0, 3) so you have the correct number of columns to stack but have actually no data inside:

import numpy as np
pointsarrray = np.empty((0,3))

pointsarrray
# array([], shape=(0, 3), dtype=float64)
Sign up to request clarification or add additional context in comments.

2 Comments

What's the most annoying here is not the simplicity and efficiency of the answer but rather the fact I didn't think of it by myself (I've tried so many more complicated stuff...) anyway; big thanks
Is there a numpy function returning a shapeless (hence without initialized coefficients as there are no coeffficient if the shape is not specified) array ? (Like a default constructor doing really nothing.)
2

Maybe it's the name that is confusing you but numpy.emptys documentation explains what is happening:

Return a new array of given shape and type, without initializing entries.

So it just doesn't initialize the entries (sometimes they are random sometimes they are zero) but empty refers only to the value of the entries not to the shape of the array.


However it's really bad for performance to create an array by appending or stacking. Just collect them in a list and create the array afterwards:

pointsarray = [np.array(myPoint)]

# ...

pointsarray = np.array(pointsarray)  # or np.stack(pointsarray, axis=...) whatever you need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.