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


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

numpy.result_type()函數返回通過將NumPy類型提升規則應用於參數而產生的類型。

NumPy類型提升示例:
假設計算3 * arr(其中arr是32位浮點數的數組),直觀地應該得出32位浮點數的輸出。如果3是32位整數,則NumPy規則表明它不能無損地轉換為32位浮點數,因此結果類型應為64位浮點數。

用法: numpy.result_type(arrays_and_dtypes)

參數:
arrays_and_dtypes:[數組和dtypes的列表]需要結果類型的某些操作的操作數。
返回:[dtype]返回結果類型。

代碼1:



# Python program explaining 
# numpy.result_type() function 
          
# importing numpy as geek  
import numpy as geek  
      
gfg = geek.result_type('f4', 'i8') 
    
print (gfg)

輸出:

float64


代碼2:

# Python program explaining 
# numpy.result_type() function 
          
# importing numpy as geek  
import numpy as geek  
      
gfg = geek.result_type(3, geek.arange(7, dtype = 'i1')) 
    
print (gfg)

輸出:

int8

相關用法


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