Skip to content Skip to sidebar Skip to footer

How To Create A Numpy Array Of Objects Without First Constructing A List?

arr = np.array([Myclass(np.random.random(100)) for _ in range(10000)]) Is there a way to save time in this statement by creating a numpy array of objects directly (avoiding the li

Solution 1:

Numpy needs to know the length of the array in advance because it must allocate enough memory in a block.

You can start with an empty array of appropriate type using np.empty(10_000, object). (Beware that for most data types empty arrays may contain garbage data, it's usually safer to start with np.zeros() unless you really need the performance, but dtype object does get properly initialized to Nones.)

You can then apply any callable you like (like a class) over all the values using np.vectorize. It's faster to use the included vectorized functions when you can instead of converting them, since vectorize basically has to call it for each element in a for loop. But sometimes you can't.

In the case of random numbers, you can create an array sample of any shape you like using np.random.rand(). It would still have to be converted to a new array of dtype object when you apply your class to it though. I'm not sure if that's any faster than creating the samples in each __init__ (or whatever callable). You'd have to profile it.

Post a Comment for "How To Create A Numpy Array Of Objects Without First Constructing A List?"