NumPy: numpy.asscalar() function

numpy.asscalar() function

The numpy.asscalar() function is used to convert an array of size 1 to its scalar equivalent.

This function is useful when working with NumPy arrays that are guaranteed to have only one element, such as the output of certain NumPy functions or operations.


NumPy manipulation: asscalar() function


Name Description Required /
a Input array of size 1. Required

Return value:

out : scalar - Scalar representation of a. The output data type is the same type returned by the input’s item method.

Example: Converting a 1-element array to a scalar using numpy.asscalar()

>>> import numpy as np
>>> np.asscalar(np.array([48]))

The above code converts a 1-element numpy array to a scalar value. In this case, the code creates a 1-element numpy array containing the value 48 and then uses the numpy.asscalar() function to convert it to a scalar with the same value.

Pictorial Presentation:

NumPy manipulation: asscalar() function

Python - NumPy Code Editor:

Previous: asarray_chkfinite()
Next: require()

Follow us on Facebook and Twitter for latest update.