ndarray基础 | 机器学习 |《python学习之路》| python 技术论坛-金年会app官方网
numpy中最重要的对象—ndarray:
ndarray对象指的是用于存放同类型元素的多维数据,它是一个多维容器,n代表着它的维度
创建ndarray对象
- 通过array方法创建
- 参数说明
- 必选参数
- object 数组或嵌套的数列
- 可选参数
- dtype 数组元素的数据类型
- copy 对象是否需要复制
- order 创建数组的样式,c为行方向,f为列方向,a为任意方向(默认)
- subok 默认返回一个与基类类型一致的数组
- ndmin 指定生成数组的最小维度
- 必选参数
- demo
>>> from numpy as np >>> x = np.array([[1, 2, 3], [4, 5, 6]],dtype=np.int32,copy=true,order=none,subok=false,ndmin=0) >>> type(x) # 查看x类型 <type 'numpy.ndarray'> >>> x.shape # 查看ndarray对象的维度 (2, 3) >>> x.dtype # 查看x里的数据类型 dtype('int32')
- 参数说明
- 通过zeros/ones方法创建(创建指定大小的数组,数组元素以 0/1 来填充,)
- 参数说明
- 必要参数
- shape 数组形状
- 可选参数
- dtype 数据类型
- order ‘c’ 用于 c 的行数组,或者 ‘f’ 用于 fortran 的列数组
- 必要参数
- demo
>>> np.zeros(5) array([0., 0., 0., 0., 0.]) >>> np.zeros((3,3)) array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
- 参数说明
- 通过empty方法创建(创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:)
- 参数说明
- 必要参数
- shape 数组形状
- 可选参数
- dtype 数据类型
- 有”c”和”f”两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序
- 必要参数
- demo
>>> x = np.empty((3,2),dtype=int) >>> x array([[0, 0], [0, 0], [0, 0]]) >>> y = np.empty([3,2],dtype=int) >>> y array([[0, 0], [0, 0], [0, 0]])
- 参数说明
- 其他方法:
- 通过full方法创建(创建一个填充给定值的n * n数组)
- demo
>>> np.full([3,3],3) array([[3, 3, 3], [3, 3, 3], [3, 3, 3]])
- 通过eye方法创建(创建一个对角线是1,其余是0的多维数组)
- demo
>>> np.eye(3) array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) >>> np.eye(1) array([[1.]])
- 通过linspace方法创建(创建一个在指定的时间间隔内返回均匀间隔的数字的数组)
- demo
>>> np.linspace(0,8.8,num=5) array([0. , 2.2, 4.4, 6.6, 8.8])
- 通过random方法创建(创建一个填充0到1之间随机值的数组)
- demo
>>> np.random.random([3,3]) array([[0.17647511, 0.79086009, 0.26275058], [0.83484953, 0.6386956 , 0.53928901], [0.26020885, 0.58836421, 0.39308341]])
numpy支持的数据类型(ndarray对象支持的数据类型)
名称 | 描述 |
---|---|
bool_ | 布尔型数据类型(true 或者 false) |
int_ | 默认的整数类型(类似于 c 语言中的 long,int32 或 int64) |
intc | 与 c 的 int 类型一样,一般是 int32 或 int 64 |
intp | 用于索引的整数类型(类似于 c 的 ssize_t,一般情况下仍然是 int32 或 int64) |
int8 | 字节(-128 to 127) |
int16 | 整数(-32768 to 32767) |
int32 | 整数(-2147483648 to 2147483647) |
int64 | 整数(-9223372036854775808 to 9223372036854775807) |
uint8 | 无符号整数(0 to 255) |
uint16 | 无符号整数(0 to 65535) |
uint32 | 无符号整数(0 to 4294967295) |
uint64 | 无符号整数(0 to 18446744073709551615) |
float_ | float64 类型的简写 |
float16 | 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位 |
float32 | 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位 |
float64 | 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位 |
complex_ | complex128 类型的简写,即 128 位复数 |
complex64 | 复数,表示双 32 位浮点数(实数部分和虚数部分) |
complex128 | 复数,表示双 64 位浮点数(实数部分和虚数部分) |
ndarray对象的属性
属性 | 说明 |
---|---|
ndarray.ndim | 数组的轴(维度)的个数。在python世界中,维度的数量被称为rank。 |
ndarray.shape | 数组的维度。这是一个整数的元组,表示每个维度中数组的大小。对于有n行和m列的矩阵,shape将是(n,m)。因此,shape元组的长度就是rank或维度的个数 ndim。 |
ndarray.size | 数组元素的总数。这等于shape的元素的乘积。 |
ndarray.dtype | 一个描述数组中元素类型的对象。可以使用标准的python类型创建或指定dtype。另外numpy提供它自己的类型。例如numpy.int32、numpy.int16和numpy.float64。 |
ndarray.itemsize | 数组中每个元素的字节大小。例如,元素为 float64 类型的数组的 itemsize 为8(=64/8),而 complex32 类型的数组的 itemsize 为4(=32/8)。它等于 ndarray.dtype.itemsize 。 |
ndarray.flags | ndarray 对象的内存信息 |
ndarray.real | ndarray元素的实部 |
ndarray.imag | ndarray 元素的虚部 |
ndarray.data | 该缓冲区包含数组的实际元素。通常,我们不需要使用此属性,因为我们将使用索引访问数组中的元素。 |
demo
>>> a = np.full((3,3),3)
>>> a
array([[3, 3, 3],
[3, 3, 3],
[3, 3, 3]])
>>> a.ndim
2
>>> a.shape
(3, 3)
>>> a.size
9
>>> a.dtype
dtype('int32')
>>> a.itemsize
4
>>> a.flags
c_contiguous : true
f_contiguous : false
owndata : true
writeable : true
aligned : true
writebackifcopy : false
updateifcopy : false
>>> a.real
array([[3, 3, 3],
[3, 3, 3],
[3, 3, 3]])
>>> a.imag
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
>>> a.data
<memory at 0x0fe0e990>