Để xác định kiểu chung theo quy tắc cưỡng chế tiêu chuẩn, hãy sử dụng phương thức numpy.find_common_type () trong Python numpy. Đối số thứ nhất là danh sách các đối tượng có thể chuyển đổi dtype hoặc dtype đại diện cho các mảng. Đối số thứ 2 là Danh sách các đối tượng có thể chuyển đổi kiểu dtype hoặc kiểu đại diện cho các đối tượng vô hướng.
Phương thức find_common_type () trả về kiểu dữ liệu phổ biến, là kiểu tối đa của kiểu_mảng bỏ qua kiểu_bảng vô hướng, trừ khi kiểu_mạng vô hướng thuộc loại khác (dtype.kind). Nếu loại không được hiểu, thì Không trả lại.
Các bước
Đầu tiên, hãy nhập thư viện được yêu cầu -
import numpy as np
Sử dụng phương thức find_common_type () trong Numpy. Xác định kiểu chung theo quy tắc chuẩn -
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([], [np.int64, np.float32, complex]))
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([np.float32], [complex]))
print("Result...",np.find_common_type([np.float64], [complex]))
print("Result...",np.find_common_type(['f4', 'i4'], ['c8']))
print("Result...",np.find_common_type([np.int64], [complex]))
print("Result...",np.find_common_type([np.int64], [np.float64])) Ví dụ
import numpy as np
# To determine common type following standard coercion rules, use the numpy.find_common_type() method in Python numpy
# The 1st argument is a list of dtypes or dtype convertible objects representing arrays.
# The 2nd argument is A list of dtypes or dtype convertible objects representing scalars.
print("Using the find_common_type() method in Numpy\n")
# Determine common type following standard coercion rules
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([], [np.int64, np.float32, complex]))
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([np.float32], [complex]))
print("Result...",np.find_common_type([np.float64], [complex]))
print("Result...",np.find_common_type(['f4', 'i4'], ['c8']))
print("Result...",np.find_common_type([np.int64], [complex]))
print("Result...",np.find_common_type([np.int64], [np.float64])) Đầu ra
Using the find_common_type() method in Numpy Result... float32 Result... complex128 Result... float32 Result... complex128 Result... complex128 Result... complex128 Result... complex128 Result... float64