Use numpy.asscalar to convert a numpy array / matrix a scalar value:
>>> a=numpy.array([[[[42]]]])
>>> numpy.asscalar(a)
42
The output data type is the same type returned by the input’s
item
method.
It has built in error-checking if there is more than an single element:
>>> a=numpy.array([1, 2])
>>> numpy.asscalar(a)
gives:
ValueError: can only convert an array of size 1 to a Python scalar
Note: the object passed to asscalar
must respond to item
, so passing a list or tuple won't work.