NumPy是Python语言的一个扩充程序库,支持高级大量的维度数组与矩阵运算,底层是C语言实现的,由于针对数组运算提供大量的数学函数库,计算速度比较快,Numpy内部解除了Python的PIL(全局解释器锁),运算效率极好,是大量机器学习框架的基础类库!
numpy 的属性
(1)ndim:矩阵维度属性 (2)shape:行数和列数的值
import numpy as np
a = np.array([[1,3,4],[3,6,9],[22,1,1]])
print(a) # 打印a矩阵
print(a.ndim) # a的维度
print(a.shape) # a的行和列维度的值
print(a.shape[0]) # shape[0]行这个维度
print(a.shape[1]) # shape[1]列这个维度
[[ 1 3 4]
[ 3 6 9]
[22 1 1]]
2
(3, 3)
3
3
(3)size:返回元素个数
print(a.size)
9
(4)dtype: 数组元素的类型 float32,uint8,int32等等
b = np.array([[1,2,3,4]], dtype=float)
print(b)
[[1. 2. 3. 4.]]
(5) itemsize : 每个元素占字节数
c = np.array([1,2,3,4])
print(c.itemsize)
4
(6) nbytes 所有元素占的字节
c = np.array([1,2.,3,4])
print(c)
print(c.nbytes) # float: 8字节
[1. 2. 3. 4.]
32