numpy.asscalar() – The NumPy asscalar Python Function

Introduction to NumPy asscalar Function

When we wish to convert an array of size 1 to its scalar equivalent, we utilize the numpy.asscalar() function.

Syntax:

numpy.asscalar(arr)

Parameters:

  • arr : [ndarray] Input array of size 1.

Return: Scalar representation of arr. The output data type is the same type returned by the input’s item method.

Code Examples of NumPy asscalar

Example 01:

# welcome to softhunt.net
# Python program explaining
# numpy.asscalar() function

import numpy as np
# creating a array of size 1
in_arr = np.array([ 5 ])

print ("Input array : ", in_arr)

	
out_scalar = np.asscalar(in_arr)
print ("output scalar from input array : ", out_scalar)

Output:

Input array :  [5]
output scalar from input array :  5

Example 02:

# welcome to softhunt.net
# Python program explaining
# numpy.asscalar() function
import numpy as np

in_list = [34]

# changing the list to size 1 array
arr = np.array(in_list)

print ("Input array from list : ", arr)

# changing the array to scalar
scalar = np.asscalar(arr)

print ("output scalar from input list : ", scalar)

Output:

Input array from list :  [34]
output scalar from input list :  34

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What is the NumPy module in Python?

Numerical Python (NumPy) is a library that consists of multidimensional array objects and a set of functions for manipulating them. NumPy allows you to conduct mathematical and logical operations on arrays.

NumPy is a Python scripting language. ‘Numerical Python’ is what it stands for. It is a library that contains multidimensional array objects as well as a collection of array processing routines.

Is NumPy an API?

The C-API in NumPy allows users to expand the system. And gain access to the array object for usage in other procedures. Reading the source code is the best method to properly understand the C-API. However, if you’re not used to working with (C) source code. This might be difficult at first. Be confident that with experience, the process will become simpler. And you may be amazed at how easy C-code may be to understand. Even if you don’t think you’ll be able to write C code from scratch. It’s a lot easier to comprehend and alter existing code than it is to write it from scratch.

Conclusion

That’s all for this article. If you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.asarray_chkfinite()

Leave a Comment