當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.asscalar()用法及代碼示例

當我們要將大小為1的數組轉換為其標量等效項時,將使用numpy.asscalar()函數。

用法: numpy.asscalar(arr)

參數:
arr :[ndarray]大小為1的輸入數組。


Return :arr的標量表示。輸出數據類型與輸入的item方法返回的類型相同。

代碼1:工作

# Python program explaining 
# numpy.asscalar() function 
  
import numpy as geek 
# creating a 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) 

輸出:

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


代碼2:

# Python program explaining 
# numpy.asscalar() function 
import numpy as geek 
  
in_list = [2 ] 
  
# changing the list to size 1 array 
arr = geek.array(in_list)  
  
print ("Input  array from list:", arr) 
  
# changing the array to scalar   
scalar = geek.asscalar(arr) 
  
print ("output scalar from input list:", scalar) 

輸出:

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


相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 numpy.asscalar() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。