pythonarraysnumpytypeerrorindex-error

How can I get the value stored in a 0-dimensional Numpy array?


I have a numpy array like this

a = np.array(1)

How can I get the 1 value back from this array?

a[0] does not work: it results in IndexError: 0-d arrays can't be indexed.

Similarly, a(0) gives an error that says TypeError: 'numpy.ndarray' object is not callable.


Solution

  • What you create with

    a = np.array(1)
    

    is a zero-dimensional array, and these cannot be indexed. You also don't need to index it -- you can use a directly as if it were a scalar value. If you really need the value in a different type, say float, you can explicitly convert it with float(a). If you need it in the base type of the array, you can use a.item() or a[()].

    Note that the zero-dimensional array is mutable. If you change the value of the single entry in the array, this will be visible via all references to the array you stored. Use a.item() if you want to store an immutable value.

    If you want a one-dimensional array with a single element instead, use

    a = np.array([1])
    

    You can access the single element with a[0] now.