一、NumPy数组对象
NumPy中的ndarray是一个多维数组对象,该对象由两部分组成:
- 实际的数据
- 描述这些数据的元数据
NumPy中可以用arange函数快速创建一维数组:
In: import numpy as np
In: a = np.arange(5)
In: a
Out: array([0, 1, 2, 3, 4])
In: a.dtype
Out: dtype('int32') #表示有符号的32位整型
In: f = a.astype(np.float64) #astype可以显式转换其dtype(会创建出一个新数组),若失败引发TypeError
In: f.dtype
Out: dtype('float64')
In: a.shape #返回维度
Out: (5,) #返回一个元祖(tuple)此处只有一维,所以元祖中只有一个元素
数组基本说明:
方法 | 说明 |
---|---|
shape | 返回数组各维度大小 |
dtype | 返回数组数据类型 |
astype | 转换数组的数据类型 |
数据类型
类型 | 类型代码 | 说明 |
---|---|---|
int8、uint8 | i1、u1 | 有符号和无符号的8位(1个字节)整型 |