Change language

numpy.asscalar () in Python

|

numpy.asscalar() is used when we want to convert an array of size 1 to its scalar equivalent.

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 # 1: Work

# Python program explaining
# numpy.asscalar () function

  

import numpy as geek

# create an array of size 1

in_arr = geek.array ([ 8 ])

 

print ( "Input array:" , in_arr)

 

  

out_scalar = geek .asscalar (in_arr)

print ( "output scalar from input array:" , out_scalar) 

Output:

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

Code # 2:

# Python program explaining
# numpy. asscalar () function

import numpy as geek

 

in_list = [ 2 ]

 
# change the list to size 1 array

arr = geek.array (in_list ) 

 

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

 
# changing the array to a scalar

scalar = geek.asscalar (arr)

 

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

Output:

 Input ar ray from list: [2] output scalar from input list: 2 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method