0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】NumPyのデータ型

Posted at

整数のデフォルトはint64で、小数のデフォルトはfloat64である。

ただし、OS、Python、NumPyのバージョンなど、環境によって変わることがあります。

dtype属性でデータ型を確認できる。

dtype属性は以下のように使います。

arr = np.array([1])
arr.dtype
▶︎ dtype('int64')

属性のため、「dtype(arr)」ではなく「arr.dtype」と記述します。

なお、要素の型ではなくオブジェクトの型を確認するtype関数は、関数のため「type(arr) ▶︎ numpy.ndarray」と記述します。type関数はNumPyの関数ではなく、Python標準の関数です。

astypeメソッドで以下のように記述して、データ型を変更できます。

arr = np.array([1])
arr.astype(np.float64)
▶︎ array([1.])

int32やint64の数字部分は、確保するメモリ量を表している。

正しい説明です。

int64などの数字部分は確保するメモリ量を表しており、以下の範囲の数値を要素に格納できます。

int16 → 2バイト(-32768〜32767)

int32 → 4バイト(-2147483648〜2147483647)

int64 → 8バイト(-9223372036854775808〜9223372036854775807)

Numpyの主なデータ型は以下があります。

bool(論理型)

int16、int32、int64(整数)

float16、float32、float64(浮動小数点数)

datetime64、timedelta64(日時)

datetime64は、「date = np.array('2015-07-04', dtype=np.datetime64)」のように記述して作成します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?