1
import numpy as np
np.array([], ndmin=2)

The resulting array has shape (1, 0). But why not (0, 0), if the input array has length 0? Is there a better way to create an array of shape (0, 0) than explicitely call np.zeros((0, 0))?

2
  • There's nothing preventing that array from having a shape of (0, 0), verify for yourself by assigning that shape to it, or using reshape. The behavior of the constructor is to append 1s until the number of dimensions has been satisfied. Commented Nov 1, 2019 at 15:33
  • Why do you prefer (0,0) over (1,0)? Commented Nov 1, 2019 at 15:59

1 Answer 1

2

From the documentation for numpy.array:

ndmin : int, optional -- Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement

You're giving it a shape (0,) array-like, so it prepends a 1 to make the shape size 2.

np.zeros((0, 0)) is fine.

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

3 Comments

"Ones will be pre-pended to the shape as needed to meet this requirement" - why is it even needed?
It doesn't treat the [] case special. np.array( np.ones(n), ndmin=2) becomes (1,n) shape regardless whether n is 0 or 10. Consistency like that is nice.
@steam_engine [] is a shape (0,) array-like, and you tell it that your requirement is a shape (x,y) array-like, so it appends a 1 before the 0 to give you an array with shape (1,0).

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.