cerebrou
cerebrou

Reputation: 5540

Creating an empty multidimensional array

In Python when using np.empty(), for example np.empty((3,1)) we get an array that is of size (3,1) but, in reality, it is not empty and it contains very small values (e.g., 1.7*(10^315)). Is possible to create an array that is really empty/have no values but have given dimensions/shape?

Upvotes: 2

Views: 6416

Answers (3)

Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1621

I suggest to use np.nan. like shown below,

yourdata = np.empty((3,1)) * np.nan

(Or)

you can use np.zeros((3,1)). but it will fill all the values as zero. It is not intuitively well. I feel like using np.nan is best in practice.

Its all upto you and depends on your requirement.

Upvotes: 1

raphael
raphael

Reputation: 2960

I'd suggest using np.full_like to choose the fill-value directly...

x = np.full_like((3, 1), None, dtype=object)

... of course the dtype you chose kind of defines what you mean by "empty"

Upvotes: 2

Hari Menon
Hari Menon

Reputation: 35405

I am guessing that by empty, you mean an array filled with zeros.

Use np.zeros() to create an array with zeros. np.empty() just allocates the array, so the numbers in there are garbage. It is provided as a way to even reduce the cost of setting the values to zero. But it is generally safer to use np.zeros().

Upvotes: 1

Related Questions