The numpy.asscalar() function converts an array of size 1 to its scalar equivalent.
Syntax
numpy.asscalar(a)
Parameter
a: This parameter represents an input array of size 1.
Return
This function returns a scalar representation of parameter ‘a’. The output data type is the same type returned by the input’s item method.
Example 1
# Python program explaining
# numpy.asscalar() function
import numpy as np
inp_arr = np.array([ 18 ])
print ("Input array: ", inp_arr)
out_scalar = np.asscalar(inp_arr)
print ("Output scalar: ", out_scalar)
Output
Input array: [18]
Output scalar: 18
Example 2
# Python program explaining
# numpy.asscalar() function
import numpy as np
inp_list = [21]
# changing the list to size 1 array
inp_arr = np.array(inp_list)
print ("Input array from Input list : ", inp_arr)
# changing the array to scalar
out_scalar = np.asscalar(inp_arr)
print ("Output scalar: ", out_scalar)
Output
Input array from Input list : [21]
Output scalar: 21