ndarray数据类型

dtype(数据类型)是一个特殊的对象,它含有ndarray将一块内存解释为特定数据类型所需的信息

1 In [18]: sim1 = np.array([1,2,3],dtype=np.float64)
2 
3 In [19]: sim2 = np.array([1,2,3],dtype=np.float32)
4 
5 In [20]: sim1.dtype
6 Out[20]: dtype('float64')
7 
8 In [21]: sim2.dtype
9 Out[21]: dtype('float32')

dtype是Numpy强大和灵活的原因之一。数值型dtype的命名方式相同:一个类型名(如float或int),后面跟一个用于表示各元素位长的数字。标准的双精度浮点值(既Python中的float)需要占用8字节(既64位)。因此,该类型在Numpy中就记作float64。

Numpy的数据类型

类型类型代码说明
int8、uint8i1、u1有符号和无符号的8位(1个字节)整数
int16、uint16i2、u2有符号和无符号的16位(2个字节)整数
int32、uint32i4、u4有符号和无符号的32位(4个字节)整数
int64、unint64i8、u8有符号和无符号的64位(8个字节)整数
float16  f2半精度浮点数
float32f4或f标准的单精度浮点数
float64f8或d标准的双精度浮点数
float128f16或g扩展精度浮点数
complex64、complex128、complex256c8、c16、c32分别用两个32位、64位或128位浮点数表示的复数
bool  ? 存储True和False值的布尔类型
objectO Python对象类型
string_S固定长度的字符串长度(每个字符1个字节)
unicode_U固定长度的unicode长度(每个字符1个字节)

可以通过ndarray的astype方法显式地转换其dtype。整数转换浮点数。

注意:调用astype无论如何都会创建出一个新的数组(原始数据的一份拷贝),即使新dtype跟老dtype相同也是如此。

1 In [22]: sim = np.array([1,2,3,4,5])
2 
3 In [23]: sim.dtype
4 Out[23]: dtype('int64')
5 
6 In [24]: float_sim = sim.astype(np.float64)
7 
8 In [25]: float_sim.dtype
9 Out[25]: dtype('float64')

浮点数转换成整数,小数点部分将会被截断。

1 In [26]: sim = np.array([3.7,-1.6,4.7,-2.3,9.0])
2 
3 In [27]: sim
4 Out[27]: array([ 3.7, -1.6,  4.7, -2.3,  9. ])
5 
6 In [28]: sim.astype(np.int32)
7 Out[28]: array([ 3, -1,  4, -2,  9], dtype=int32)

字符串全是数字,可以用astype将其转换为数值形式。

1 In [31]: number_strings = np.array(['1.26','-8','-4.6'],dtype=np.string_)
2 
3 In [32]: number_strings.astype(np.float64)
4 Out[32]: array([ 1.26, -8.  , -4.6 ])

还可以用简洁的代码来表示dtype。

1 In [33]: empty_uint32 = np.empty(8,dtype='u4')
2 
3 In [34]: empty_uint32
4 Out[34]: 
5 array([         0, 1072693248, 1717986918, 1073899110,          0,
6        1074790400,          0, 1075052544], dtype=uint32)

 

转载于:https://www.cnblogs.com/yu-1104/p/7867849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值