Numpy学习——数组类型

更多的数据类型

转换(casting)

numpy会自动转换高精度数据类型:

>>> np.array([1, 2, 3]) + 1.5
array([2.5, 3.5, 4.5])

赋值不会改变原有数据类型:

>>> a = np.array([1, 2, 3])
>>> a.dtype
dtype('int32')
>>> a[0] = 1.9
>>> a
array([1, 2, 3])

凑整:

>>> a = np.array([1.2, 1.5, 1.6, 2.5, 3.5, 4.5])
>>> b = np.around(a) #四舍五入
>>> b
array([1., 2., 2., 2., 4., 4.])
>>> c = np.around(a).astype(int)
>>> c
array([1, 2, 2, 2, 4, 4])

不同数据类型的大小

有符号整数:

类型位数
int88 bits
int1616 bits
int3232 bits
int6464 bits
>>> np.array([1], dtype=int).dtype
dtype('int32')
>>> np.iinfo(np.int32).max, 2**31 - 1
(2147483647, 2147483647)

无符号整数:

类型位数
uint88 bits
uint1616 bits
uint3232 bits
uint6464 bits
>>> np.iinfo(np.uint32).max, 2**32 - 1
(4294967295, 4294967295)

浮点数类型:

类型位数
float1616 bits
float3232 bits
float6464 bits
float9696 bits
float128128 bits
>>> np.finfo(np.float32).eps
1.1920929e-07
>>> np.finfo(np.float64).eps
2.220446049250313e-16
>>> np.float32(1e-8) + np.float32(1) == 1
True
>>> np.float64(1e-8) + np.float64(1) == 1
False

复合浮点数(complex):

类型位数
complex64two 32-bit floats
complex128two 64-bit floats
complex192two 96-bit floats
complex256two 128-bit floats

结构体类型

变量类型
sensor_code(4-character string)
position(float)
value(float)
>>> samples = np.zeros((6,), dtype=[('sensor_code', 'S4'), ('position', float), ('value', float)])
>>> samples.ndim
1
>>> samples.shape
(6,)
>>> samples.dtype.names
('sensor_code', 'position', 'value')
>>> samples[:] = [('ALFA',   1, 0.37), ('BETA', 1, 0.11), ('TAU', 1,   0.13),('ALFA', 1.5, 0.37), ('ALFA', 3, 0.11), ('TAU', 1.2, 0.13)]
>>> samples
array([(b'ALFA', 1. , 0.37), (b'BETA', 1. , 0.11), (b'TAU', 1. , 0.13),
       (b'ALFA', 1.5, 0.37), (b'ALFA', 3. , 0.11), (b'TAU', 1.2, 0.13)],
      dtype=[('sensor_code', 'S4'), ('position', '<f8'), ('value', '<f8')])

通过索引名称访问某个字段:

>>> samples['sensor_code']
array([b'ALFA', b'BETA', b'TAU', b'ALFA', b'ALFA', b'TAU'], dtype='|S4')
>>> samples['value']
array([0.37, 0.11, 0.13, 0.37, 0.11, 0.13])
>>> samples[0]
(b'ALFA', 1., 0.37)
>>> samples[0]['sensor_code'] = 'TAU'
>>> samples[0]
(b'TAU', 1., 0.37)

同时访问多个字段:

>>> samples[['position', 'value']]
array([(1. , 0.37), (1. , 0.11), (1. , 0.13), (1.5, 0.37), (3. , 0.11),
       (1.2, 0.13)],
      dtype={'names':['position','value'], 'formats':['<f8','<f8'], 'offsets':[4,12], 'itemsize':20})

花式索引:

>>> samples[samples['sensor_code'] == b'ALFA']
array([(b'ALFA', 1.5, 0.37), (b'ALFA', 3. , 0.11)],
      dtype=[('sensor_code', 'S4'), ('position', '<f8'), ('value', '<f8')])

注意:还有其他一些构造结构体数组的语法。

处理丢失的数据

对于浮点类型我们可以使用NaN,但是mask适用于所有数据类型:

>>> x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1])
>>> x
masked_array(data=[1, --, 3, --],
             mask=[False,  True, False,  True],
       fill_value=999999)
>>> y = np.ma.array([1, 2, 3, 4], mask=[0, 1, 1, 1])
>>> x + y
masked_array(data=[2, --, --, --],
             mask=[False,  True,  True,  True],
       fill_value=999999)

用于函数处理:

>>> np.ma.sqrt([1, -1, 2, -2])
masked_array(data=[1.0, --, 1.4142135623730951, --],
             mask=[False,  True, False,  True],
       fill_value=1e+20)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值