29

How do I create a 0 x 0 (i.e. ndim= 2, shape= (0,0)) float numpy.ndarray?

2 Answers 2

43
>>> import numpy as np
>>> a = np.empty( shape=(0, 0) )
>>> a
    array([], shape=(0, 0), dtype=float64)

>>> a.shape
    (0, 0)
>>> a.size
    0

The array above is initialized as a 2D array--i.e., two size parameters passed for shape.

Second, the call to empty is not strictly necessary--i.e., an array having 0 size could (i believe) be initialized using other array-creation methods in NumPy, e.g., NP.zeros, Np.ones, etc.

I just chose empty because it gives the smallest array (memory-wise).

Sign up to request clarification or add additional context in comments.

2 Comments

You mention it could also be done with NP.array, NP.zeros, NP.ones etc. I see how to do it with Np.zeros and NP.ones, but not how to do it with NP.array.
so for instance, NP.array([]); though the shape of this array is (0,); your Q specifies a 2D array. In my answer i just wanted to make the point that other array constructors would do the job as well, not just empty. I'll edit to remove 'array', given you need a 2D array.
1

All of the functions that return an array given shape/size can create a 0x0 array (in fact, of any dimension).

np.full((0,0), 0.0)
np.random.rand(0,0)
np.random.randint(0, size=(0,0))
np.random.choice(0, size=(0,0))
# etc. 

To create the same with np.array, the shape attribute of an empty array could be modified.

a = np.array([])
a.shape += (0,)
a  # array([], shape=(0, 0), dtype=float64)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.