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.
Syntax:
numpy.asscalar(a)
Parameter:
Name | Description | Required / Optional |
---|---|---|
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]))
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:
Python - NumPy Code Editor:
Previous: asarray_chkfinite()
Next: require()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/numpy/manipulation/asscalar.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics