专业编程基础技术教程

网站首页 > 基础教程 正文

numpy基础之ndarray的数据类型dtype

ccvgpt 2024-10-12 13:29:54 基础教程 8 ℃

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

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

1.1 ndarray数据类型

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

numpy基础之ndarray的数据类型dtype

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

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

NO

类型

类型代码

描述

1

int8、uint8

i1、u1

有符号和无符号的8位(1字节)整数

2

int16、uint16

i2、u2

有符号和无符号的16位(2字节)整数

3

int32、uint32

i4、u4

有符号和无符号的32位(4字节)整数

4

int64、uint64

i8、u8

有符号和无符号的64位(8字节)整数

5

float16

f2

半精度浮点数

6

float32

f4或f

标准单精度浮点数。与C的float兼容

7

float64

f8或d

标准双精度浮点数。与C的double和python的float兼容。

8

float128

f16或g

扩展精度浮点数

9

complex64、complex128、complex256

c8、c16、c32

分别用两个32位、64为、128位浮点数表示的复数

10

bool

?

存储True和False值的布尔类型

11

object

O

python对象类型

12

string_

Sn

固定长度的字符串类型(每个字符1个字节)。比如,要创建一个长度为10的字符串,应使用S10

13

unicode_

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')

2 END

本文首发微信公众号:梯阅线条

更多内容参考python知识分享或软件测试开发目录。

最近发表
标签列表