03_NumPy的数据类型

NumPy的数据类型

  • ndarray 对象的 dtype 属性中描述了该对象的数据类型信息,返回一个 dtype 对象
  • 调用 astype() 会创建一个新对象,不会改变原对象
  • 所有的数据类型如下表所示:
数据类型 类型代码 说明
int8、uint8 i1、u1 有符号和无符号的8位(1个字节)整形
int16、uint16 i2、u2 有符号和无符号的16位(2个字节)整形
int32、uint32 i4、u4 有符号和无符号的32位(4个字节)整形
int64、uint64 i8、u8 有符号和无符号的64位(8个字节)整形
float16 f2 半精度浮点数
float32 f4或f 标准的单精度浮点数
float64 f8或d 标准的双精度浮点数
float128 f16或g 扩展精度浮点数
bool ? 存储布尔类型(存储为一个字节)
object O Python对象类型
string_ S 固定长度的字符串类型(每个字符1个字节)
unicode_ U 固定长度的unicode类型(字节数由平台决定)
complex64、complex128、complex256 c8、c16、c32 分别用两个32位、64位或128位浮点数表示的复数

注意int_int32 的简写,float_float64 的简写,complex_complex128 的简写

import numpy as np

int_arr = np.arange(6).reshape(2, 3)
int_arr
>>> array([[0, 1, 2],
           [3, 4, 5]])

可以通过 dtype 属性获得类型信息

int_arr.dtype
>>> dtype('int64')

创建指定numpy类型的标量

var_float16 = np.float16([12, 121])
var_float16
>>> array([ 12., 121.], dtype=float16)

var_float16.dtype
>>> dtype('float16')

可以通过 astype() 方法转换数据类型

float_arr = int_arr.astype(np.float32) # 'f'等效于np.float32
float_arr
>>> array([[0., 1., 2.],
           [3., 4., 5.]], dtype=float32)

# 如果将浮点数转换成整数,小数部分会被直接删除
float_arr2 = np.array([1.2, 2.6, 3.5, 4.9, 5.0])
float_arr2
>>> array([1.2, 2.6, 3.5, 4.9, 5. ])

float_arr2.astype('int')
>>> array([1, 2, 3, 4, 5])

# 字符串也可以转为数字类型
str_arr = np.array(['2.001', '1.25', '88', '-236.123', '00'], dtype=np.string_)
str_arr
>>> array([b'2.001', b'1.25', b'88', b'-236.123', b'00'], dtype='|S8')

str_arr.astype('float')
>>> array([   2.001,    1.25 ,   88.   , -236.123,    0.   ])

调用 dtypemro 方法即可查看其所有的父类

np.int32.mro()
>>> [numpy.int32,
     numpy.signedinteger,
     numpy.integer,
     numpy.number,
     numpy.generic,
     object]

可以使用issubdtype()判断类型

np.issubdtype(float_arr.dtype, np.floating)
>>> True

np.issubdtype(np.int32, np.floating)
>>> False

自定义类型

my_dtype = np.dtype([('name',  'S10'), ('age',  int), ('city', 'S10')]) # 按关键字排序
my_dtype
>>> dtype([('name', 'S10'), ('age', '<i8'), ('city', 'S10')])

temp_arr = [('zhangsan', 20, 'BJ'), ('lisi', 22, 'CD'), ('wangwu', 21, 'SH')]
my_arr = np.array(temp_arr, dtype=my_dtype)
my_arr
>>> array([(b'zhangsan', 20, b'BJ'), (b'lisi', 22, b'CD'),
           (b'wangwu', 21, b'SH')],
          dtype=[('name', 'S10'), ('age', '<i8'), ('city', 'S10')])

my_arr[1]
>>> (b'lisi', 22, b'CD')

my_arr['name']
>>> array([b'zhangsan', b'lisi', b'wangwu'], dtype='|S10')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章