0

I was creating a numpy.empty([0]) array and accidentally typed numpy.empty([]).

This created an numpy array with the string representation

array(0.)

with size

>>> numpy.empty([]).size
1

and shape

>>> numpy.empty([]).shape
()

Question: what is the nature of this object?

I couldn't deduce it from the documentation of numpy.empty. In particular, what I find most confusing is the 0. that appears in the string representation, which seems to be a float(0.0). If it is somehow representing an element, this is not accessible. I was trying to access it as numpy.empty([])[0].

The object is different in nature from the one created by numpy.empty([0]), which has size

>>> numpy.empty([0]).size
0

and shape

>>> numpy.empty([0]).shape
(0,)
0

2 Answers 2

2

That's a 0-dimensional array. The first argument to numpy.empty indicates how long you want each dimension to be, and passing a length-0 list means you don't want any dimensions.

There is exactly one element in any 0-dimensional array, accessible by indexing it with a tuple of 0 indices:

arr[()]

The element happened to be 0.0 this time, but that's not a guarantee, since you used numpy.empty.

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

1 Comment

I guess that they put an element in it due to $\prod_{i\in D}d_i$ being $1$ if $D$ is the empty set.
1
  • np.empty([10, 10, 10]) creates a 3-dimensional array, with size 10x10x10 or 103
  • np.empty([10, 10]) creates a 2-dimensional array, with size 10x10 or 102
  • np.empty([10]) creates a 1-dimensional array, with size 10 or 101
  • np.empty([]) creates a 0-dimensional array, with size 1 or 100

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.