numpy.result_type() วิธีการส่งกลับประเภทที่เป็นผลลัพธ์จากการใช้กฎ NumPy typepromotion กับอาร์กิวเมนต์ พารามิเตอร์ที่ 1 เป็นตัวถูกดำเนินการของการดำเนินการบางอย่างที่ต้องการประเภทผลลัพธ์ ประเภทการเลื่อนตำแหน่งใน NumPy ทำงานคล้ายกับกฎในภาษาเช่น C ++ โดยมีความแตกต่างเล็กน้อย เมื่อใช้ทั้งสเกลาร์และอาร์เรย์ ประเภทของอาร์เรย์จะมีความสำคัญและคำนึงถึงมูลค่าที่แท้จริงของสเกลาร์ด้วย
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
numpy.result_type() วิธีการส่งกลับประเภทที่เกิดจากการใช้กฎ NumPy typepromotion กับอาร์กิวเมนต์ -
print("Using the result_type() method in Numpy\n")
print("Result...",np.result_type(2, np.arange(4,dtype='i1')))
print("Result...",np.result_type(5, 8))
print("Result...",np.result_type('i4', 'c8'))
print("Result...",np.result_type(3.8, 8))
print("Result...",np.result_type(5, 20.7))
print("Result...",np.result_type(-8, 20.7))
print("Result...",np.result_type(10.0, -4)) ตัวอย่าง
import numpy as np
# The numpy.result_type() method returns the type that results from applying the NumPy type promotion rules to the arguments.
# The 1st parameter is the operands of some operation whose result type is needed.
print("Using the result_type() method in Numpy\n")
print("Result...",np.result_type(2, np.arange(4,dtype='i1')))
print("Result...",np.result_type(5, 8))
print("Result...",np.result_type('i4', 'c8'))
print("Result...",np.result_type(3.8, 8))
print("Result...",np.result_type(5, 20.7))
print("Result...",np.result_type(-8, 20.7))
print("Result...",np.result_type(10.0, -4)) ผลลัพธ์
Using the result_type() method in Numpy Result... int8 Result... int64 Result... complex128 Result... float64 Result... float64 Result... float64 Result... float64