numpy基础之ndarray的数据类型dtype

1 numpy基础之ndarray的数据类型dtype

ndarray的数据类型dtype含有ndarray将一块内存解释为特定数据类型所需的信息。

1.1 ndarray数据类型

数值型dtype的命名规则:类型名+元素长度。

ndarray是一个通用的同构数据多维容器,即同一ndarray对象的所有元必须素是相同的数据类型。

numpy.array()未指定数据类型时,会为新建的ndarray数组推断一个比较合适的数据类型。

NO类型类型代码描述
1int8、uint8i1、u1有符号和无符号的8位(1字节)整数
2int16、uint16i2、u2有符号和无符号的16位(2字节)整数
3int32、uint32i4、u4有符号和无符号的32位(4字节)整数
4int64、uint64i8、u8有符号和无符号的64位(8字节)整数
5float16f2半精度浮点数
6float32f4或f标准单精度浮点数。与C的float兼容
7float64f8或d标准双精度浮点数。与C的double和python的float兼容。
8float128f16或g扩展精度浮点数
9complex64、complex128、complex256c8、c16、c32分别用两个32位、64为、128位浮点数表示的复数
10bool?存储True和False值的布尔类型
11objectOpython对象类型
12string_Sn固定长度的字符串类型(每个字符1个字节)。比如,要创建一个长度为10的字符串,应使用S10
13unicode_Un固定长度的unicode类型(字节数由平台决定)。比如,要创建一个长度为10的unicode,应使用U10

1.2 dtype

用法

import numpy as np
arr=np.array(object)
arr.dtype

描述

ndarray的数据类型存储在dtype属性,通过点号运算获取。

示例

>>> import numpy as np
>>> arr1=np.array([1,2,3])
>>> arr1
array([1, 2, 3])
# 通过 ndarray.dtype 获取ndarray的数据类型
>>> arr1.dtype
dtype('int32')
# array()未指定dtype,同时有浮点数和整数
# array会推断较合适的数据类型 float
>>> arr2=np.array([1.8,2,3])
>>> arr2
array([1.8, 2. , 3. ])
>>> arr2.dtype
dtype('float64')

1.3 astype

用法

ndarray.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)

描述

显式的将数组的dtype转换为指定类型,返回一个新的数组,原数组不变。

(1) 通过np.数据类型指定dtype;

(2) 通过简洁的类型代码指定dtype;

(3) 通过其他数组的数据类型ndarray.dtype指定dtype;

(4) 浮点数转整数类型,小数部分被截断删除;

(5) 元素都为数字的字符串数组,可以转为数值数组。

示例-np.数类型指定dtype,原数组不变

>>> import numpy as np
>>> arr=np.array([1,2,3])
>>> arr
array([1, 2, 3])
>>> arr.dtype
dtype('int32')
# astype() 将数组的dtype转为指定类型
>>> float_arr=arr.astype(np.float64)
>>> float_arr
array([1., 2., 3.])
>>> float_arr.dtype
dtype('float64')
# 原数组不变
>>> arr
array([1, 2, 3])
>>> arr.dtype
dtype('int32')

示例-浮点数转整数

>>> arr=np.array([1.23,2.56,3.89])
>>> arr
array([1.23, 2.56, 3.89])
>>> arr.dtype
dtype('float64')
# 浮点数转为整数,小数被截断删除
>>> int_arr=arr.astype(np.int32)
>>> int_arr
array([1, 2, 3])
>>> int_arr.dtype
dtype('int32')

示例-数字字符串转数字数组

# 元素都为数字字符串
>>> numstr_arr=np.array(['1','2','3'])
>>> numstr_arr
array(['1', '2', '3'], dtype='<U1')
>>> numstr_arr.dtype
dtype('<U1')
# 转换为数值类型
>>> int_arr=numstr_arr.astype(np.int32)
>>> int_arr
array([1, 2, 3])
>>> int_arr.dtype
dtype('int32')
# 元素有非数字字符串
>>> str_arr=np.array(['1','a','3'])
>>> str_arr
array(['1', 'a', '3'], dtype='<U1')
>>> str_arr.dtype
dtype('<U1')
# 不可转换为数值类型,报 ValueError
>>> str_arr.astype(np.int32)
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    str_arr.astype(np.int32)
ValueError: invalid literal for int() with base 10: 'a'

示例-类型代码和其他数组的dtype转换

# 通过简洁的类型代码指定 dtype
>>> arr=np.array([1,2,3],dtype='f4')
>>> arr
array([1., 2., 3.], dtype=float32)
>>> arr.dtype
dtype('float32')
>>> intArr=arr.astype('i2')
>>> intArr
array([1, 2, 3], dtype=int16)
>>> intArr.dtype
dtype('int16')
# 通过 ndarray.dtype 指定 dtype
>>> floatArr=intArr.astype(arr.dtype)
>>> floatArr
array([1., 2., 3.], dtype=float32)
>>> floatArr.dtype
dtype('float32')
  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值