Python
 Computer >> コンピューター >  >> プログラミング >> Python

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

  1. Pythonで正の無限大に最も近い値を見つける方法は?

    無限大には具体的な表現はありませんが、無限大に最も近い数値は、引数として「inf」を使用したfloat()関数の戻り値として表されます >>> a=float('inf') >>> a inf

  2. Pythonでファイルのmimeタイプを見つける方法は?

    Pythonには、Pythonでファイルのmimeタイプを推測するために使用できるmimetypesというモジュールがあります。ただし、ファイルのmimeタイプを知るための信頼できる方法ではありません。 例 >>> import mimetypes >>> print(mimetypes.MimeTypes().guess_type('my_file.txt')[0]) text/plain python-magicと呼ばれる非標準モジュールを使用して、ファイルのmimetypeを推測することもできます。 例 >>>