尋找 Python 中標量值的最小資料型別


numpy.min_scalar() 方法查詢最小資料型別。第一個引數是需要查詢最小資料型別的值。對於標量,返回大小最小、標量型別最小的資料型別,它可以儲存自己的值。對於非標量陣列,返回向量的 dtype 而不進行修改。浮點數不會降為整數,複雜值不會降為浮點數。

步驟

首先,匯入必需的庫 −

import numpy as np

numpy.min_scalar() 方法查詢最小資料型別 −

print("Using the min_scalar() method in Numpy\n")
print("Result...",np.min_scalar_type(55))
print("Result...",np.min_scalar_type(38.9))
print("Result...",np.min_scalar_type(-78))
print("Result...",np.min_scalar_type(479))
print("Result...",np.min_scalar_type(2e100))
print("Result...",np.min_scalar_type(-45.8))
print("Result...",np.min_scalar_type(6.5e100))

示例

# For scalar, returns the data type with the smallest size and smallest scalar kind which can hold its value.
# For non-scalar array, returns the vector’s dtype unmodified.
# Floating point values are not demoted to integers, and complex values are not demoted to floats.

import numpy as np

# The numpy.min_scalar() method finds the minimal data type.
# The 1st parameter is the value whose minimal data type is to be found.
print("Using the min_scalar() method in Numpy\n")
print("Result...",np.min_scalar_type(55))
print("Result...",np.min_scalar_type(38.9))
print("Result...",np.min_scalar_type(-78))
print("Result...",np.min_scalar_type(479))
print("Result...",np.min_scalar_type(2e100))
print("Result...",np.min_scalar_type(-45.8))
print("Result...",np.min_scalar_type(6.5e100))

輸出

Using the min_scalar() method in Numpy

Result... uint8
Result... float16
Result... int8
Result... uint16
Result... float64
Result... float16
Result... float64

更新於: 28-2-2022

229 瀏覽

開啟您的 職業

完成課程並獲取認證

開始
廣告