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】データ型と型の変換をする。

Posted at

#データ型と型変換
#Pythonのlist と Numpy array は異なる点がある。
#それは、Numpy array は 配列内に同じデータ型した入れられないことである。

data = np. array([1, "2", 3, 4, 5])
data

#上記の dtype='<U21')は、Unicode の文字を表している。
#つまり、ひとつ文字があるだけで、全体が文字型として認識されてしまう。

#整数で統一したい場合は、dtype を指定する。
data = np. array([1, 2, 3, 4, 5], dtype=np.int64)
data

data.dtype #下記はdtype の確認

#型がint64になっていることがわかる。
#また、下記のように、dataのタイプ指定をnp.int64 に指定することも可能
data_int = data.astype(np.int64)
data_int

data_int.dtype #data型がint64になっているか確認する

np.iinfo(np.int8) #8bit がどれだけ表示できるか? メモリの節約につながるエラー防止。
#よって今回の1から5までは、int64 ではなく、int8 で十分。

メモリの消費量を減らすことで、エラー防止になる。

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?