What is the numpy.asscalar() function in Python?

Overview

The asscaler() function in Python is used to return a scalar equivalent of a size 1 array.

The asscalar() function has been deprecated since the introduction of version 1.16 of NumPy and the numpy.ndarray.item() function has become the alternate function to it. Preceding versions of the NumPy 1.16 version support the ascalar() function, while succeeding versions of it do not.

Syntax

numpy.asscalar(a)
Syntax for the numpy.asscalar() function in Python

Parameter value

The asscaler() function takes a single parameter value, a, which represents the input array of size 1.

Return value

The asscaler() function returns a scalar representation of the input array arr1.

Example

import numpy as np
# creating an input arrray of size 1
arr1 = np.array([25])
# converting to scalar
print(np.asscalar(arr1))

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an input array of size 1 named arr1.
  • Line 7: We obtain and print the scalar equivalent of the input array, arr1, using the asscalar() function.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources