# minimum dimension
a = np.array([1,2,3,4,5], ndmin=2) # [[1,2,3,4,5]]
# dtype
a = np.array([1,2,3], dtype=complex) # [1.+0.j, 2.+0.j, 3.+0.j]
# shape, size, dim
shape = a.shape # [m, n]
size = a.size # m*n
dim = a.ndim # 2
# itemsize, 数据类型大小
y = np.array([1,2,3], dtype=np.float64)
c = y.itemsize # 8
# np.empty, 随机初始化
x = np.empty([3, 2], dtype=int) # 三行两列
# np.zeros / np.ones, 默认为浮点型
x = np.zeros(5) # [0. 0. 0. 0. 0.]
y = np.zeros((5, ), dtype=np.int) # [0 0 0 0 0]
z = np.zeros((2,2), dtype=np.int) # [[(0, 0) (0, 0)]
[(0, 0) (0, 0)]]
# np.random.randn, 服从 X~N(0,1) 的正态分布随机数组
a = np.random.randn(2,3) # array([[ 0.50203463, 1.48955265, -0.66236422],
[ 0.44311407, 0.11144459, -0.13326862]])
# np.random.randint(min, max, (row, col)), [min, max] 内随机整数
a = random.randint(100, 200, (3,3)) # array([[100, 154, 172],
[149, 165, 184],
[140, 140, 142]])
# np.asarray, 转化为np.ndarray
x = [1,2,3]
y = np.asarray(x)
# np.arrange(start, stop, step, dtype) 从数值范围创建数组
x = np.arrange(5) # [0 1 2 3 4]
y = np.arange(10,20,2) # [10 12 14 16 18]
# np.linspace(start, stop, num=50) 一维等差数列数组
a = np.linspace(1,10,10) # [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
b = np.linspace(1,1,10) # [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
c = np.linspace(1,4,4).reshape([2,2]) # [[1., 2.]
[3., 4.]]
# np.logspace(start, stop, num=50, base=10) 等比数列数组
# 默认底数是 10
a = np.logspace(1.0, 2.0, num = 10) # 10^1, 10^2
>>> [ 10. 12.91549665 16.68100537 21.5443469 27.82559402
35.93813664 46.41588834 59.94842503 77.42636827 100. ]
b = np.logspace(0,9,10,base=2) # [ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]
# 切片
a = np.arange(10)
b = a[2:7:2] # 从索引 2 开始到索引 7 停止,间隔为 2
>>> [2 4 6]
a = np.array([[1,2,3],[3,4,5]])
b = a[1:] # [3,4,5]
c = a[..., 1] # [2,4] 第2列元素
d = a[1, ...] # [3,4,5] 第2行元素
# 获取数组中(0,0),(1,1)和(2,0)位置处的元素
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[[0,1,2], [0,1,0]]
>>> [1 4 5]
# 布尔索引
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[x > 3] # [4,5,6]
a = np.array([np.nan, 1,2,np.nan,3,4,5])
b = a[~np.isnan(a)] # [1,2,3,4,5]
# 点乘
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = a * b # [ 10 40 90 160]
# 加减
a = np.array([[ 0, 0, 0],
[ 10,10,10]])
b = np.array([1,2,3])
c = a + b # [[ 1 2 3]
[ 11 12 13]]
# 广播
b = np.array([1,2,3])
bb = np.tile(b, (4, 1))
>>> [[ 1 2 3]
[ 1 2 3]
[ 1 2 3]
[ 1 2 3]]
# 迭代数组
a = np.arange(6).reshape(2,3)
for x in np.nditer(a):
print (x, end=", " )
>>> 0, 1, 2, 3, 4, 5,
for x in np.nditer(a, order='F') # Fortran order,即是列序优先;
for x in np.nditer(a.T, order='C') # C order,即是行序优先;
# np.flat 数组元素迭代器
a = np.arange(9).reshape(3,3)
for element in a.flat:
print (element) # 0 1 2 3 4 5 6 7 8
b = a.flatten() # [0 1 2 3 4 5 6 7] 展开的数组,返回一份数组拷贝
# np.ravel() 展平数组元素
a = np.arange(8).reshape(2,4)
b = a.ravel() # [0 1 2 3 4 5 6 7]
# 翻转数组
b = np.transpose(a)
b = a.T
# np.swapaxes 函数用于交换数组的两个轴
b = np.swapaxes(a, ax1, ax2)
# np.broadcast 用于模仿广播的对象,它返回一个对象,该对象封装了将一个数组广播到另一个数组的结果
x = np.array([[1], [2], [3]])
y = np.array([4, 5, 6])
b = np.broadcast(x, y) # 对 y 广播 x
r,c = b.iters
print (next(r), next(c)) # 1 4
print (next(r), next(c)) # 1 5
print (b.shape) # (3, 3)
# np.expand_dims 插入新的轴来扩展数组形状
x = np.array(([1,2],[3,4]))
y = np.expand_dims(x, axis = 0) # [[[1 2]
[3 4]]]
# np.squeeze 删除一维的条目
z = np.squeeze(y) # [[1 2]
[3 4]]
# np.concatenate 沿指定轴连接相同形状的两个或多个数组
a = np.array([[1,2],[3,4]]) # [[1 2] [3 4]]
b = np.array([[5,6],[7,8]]) # [[5 6] [7 8]]
np.concatenate((a,b),axis = 0) # [[1 2] [3 4] [5 6] [7 8]]
np.concatenate((a,b),axis = 1) # [[1 2 5 6] [3 4 7 8]]
# np.stack 函数用于沿新轴连接数组序列
np.stack((a,b),0) # [[[1 2] [3 4]] [[5 6] [7 8]]]
np.stack((a,b),1) # [[[1 2] [5 6]] [[3 4] [7 8]]]
# np.hstack 通过水平堆叠来生成数组
np.hstack((a,b)) # [[1 2 5 6] [3 4 7 8]]
# np.vstack 通过垂直堆叠来生成数组
np.vstack((a,b)) # [[1 2] [3 4] [5 6] [7 8]]
# np.split
b = np.split(a,3) # 将数组分为三个大小相等的子数组
c = np.split(a,[4,7]) # 将数组在一维数组中表明的位置分割
# np.hsplit / np.vsplit 水平 / 垂直分割数组
b = np.vsplit(a,2)
b = np.hsplit(a,1)
# np.resize
a = np.array([[1,2,3],[4,5,6]])
b = np.resize(a, (3,2)) # [[1 2] [3 4] [5 6]]
c = np.resize(a,(3,3)) # [[1 2 3] [4 5 6] [1 2 3]]
# np.append
d = np.append(a, [7,8,9]) # [[1 2 3] [4 5 6] [7 8 9]]
e = np.append(a, [[5,5,5],[7,8,9]],axis = 1) # [[1 2 3 5 5 5] [4 5 6 7 8 9]]
# np.insert 函数在给定索引之前,沿给定轴在输入数组中插入值
# np.insert(arr, obj, values, axis)
a = np.array([[1,2],[3,4],[5,6]]) # [[1,2],[3,4],[5,6]]
b = np.insert(a,3,[11,12]) # 未传递axis参数在插入之前输入数组会被展开 [1 2 3 11 12 4 5 6]
c = np.insert(a,1,[11],axis = 0) # [[1 2] [11 11] [3 4] [5 6]]
d = np.insert(a,1,11,axis = 1) # [[1 11 2] [3 11 4] [5 11 6]]
# np.delete
a = np.arange(12).reshape(3,4)
b = np.delete(a,5) # [ 0 1 2 3 4 6 7 8 9 10 11]
c = np.delete(a,1,axis = 1) # [[ 0 2 3] [ 4 6 7] [ 8 10 11]]
# np.unique 去除数组中的重复元素
a = np.array([5,2,6,2,7,5,6,8,2,9])
u = np.unique(a) # [2 5 6 7 8 9]
# 位运算 "&"、 "~"、 "|" 和 "^" bitwise_and / bitwise_or / invert
a,b = 13,17
print (bin(a), bin(b)) # 0b1101 0b10001
np.bitwise_and(a, b) # 1
# 字符串函数
a = np.char.add(['hello'],[' xyz']) # ['hello xyz']
b = np.char.multiply('Runoob ',3) # Runoob Runoob Runoob
c = np.char.center('Runoob', 20,fillchar = '*') # *******Runoob*******
d = np.char.capitalize('runoob') # Runoob 第一个字母转换为大写
e = np.char.title('i like runoob') # I Like Runoob
f = np.char.lower('RUNOOB') # runoob
g = np.char.upper('runoob') # RUNOOB
h = np.char.split ('i like runoob?') # ['i', 'like', 'runoob?'] 分隔符默认为空格
i = np.char.split ('www.runoob.com', sep = '.') # ['www', 'runoob', 'com']
j = np.char.splitlines('i\nlike runoob?') # ['i', 'like runoob?']
# 数学函数
np.sin()、np.cos()、np.tan()
numpy.degrees() # 函数将弧度转换为角度
np.around(a,decimals) # 四舍五入
np.floor() # 返回数字的下舍整数
np.ceil() # 返回数字的上入整数
np.reciprocal() # 函数返回参数逐元素的倒数
a = np.array([10,100,1000])
b = np.array([1,2,3])
np.power(a,b) # [ 10 10000 1000000000]
np.mod(a,b) # 余数
# 统计函数
np.amin / np.amax # 计算数组沿指定轴的最小/大值
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
b = np.amin(a,1) # [3 3 2] , 1为行
c = np.amin(a,0) # [2 4 3] , 0为列
np.ptp() # 计算数组中元素最大值与最小值的差(最大值 - 最小值)
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
b = np.ptp(a) # 7
c = np.ptp(a, axis = 1) # [4 5 7]
np.median() # 函数用于计算数组 a 中元素的中位数(中值)
np.mean() # 函数返回数组中元素的算术平均值。 如果提供了轴,则沿其计算
np.average() # 加权平均值
np.std() # 标准差, std = sqrt(mean((x - x.mean())**2))
np.var() # 方差
# np.sort(a, axis, kind, order) 排序, axis=0 按列排序,axis=1 按行排序
a = np.array([[3,7],[9,1]])
b = np.sort(a, axis = 0) # [[3 1] [9 7]]
# np.argsort() 函数返回的是数组值从小到大的索引值
x = np.array([3, 1, 2])
y = np.argsort(x) # [1 2 0]
z = x[y] # 1 2 3
# np.argmax() 和 np.argmin() 分别沿给定轴返回最大和最小元素的索引
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
b = np.argmax(a, axis = 0) # [1 2 0]
# np.where() 函数返回输入数组中满足给定条件的元素的索引
x = np.arange(9.).reshape(3, 3)
y = np.where(x > 3) # (array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2])), 共5个元素
z = x[y] # [4. 5. 6. 7. 8.]
# 矩阵
np.dot(a, b)
np.inner(np.array([1,2,3]),np.array([0,1,0])) # 向量内积,等价于 1*0+2*1+3*0
np.matmul # 对于一维数组则通过在其维度上附加 1 来将其提升为矩阵,并在乘法之后被去除
# 对于二维数组就是矩阵乘法
# 任一参数的维数大于2,则将其视为存在于最后两个索引的矩阵的栈,并进行相应广播
np.linalg.det() # 计算输入矩阵的行列式
np.linalg.solve() # 给出了矩阵形式的线性方程的解
np.linalg.inv() # 计算矩阵的乘法逆矩阵
# IO
np.save() / np.load() # 将数组保存 / 加载到以 .npy 为扩展名的文件中
np.save('outfile.npy',a)
b = np.load('outfile.npy')
np.savetxt('out.txt',a, delimiter=",") # 将数组保存 / 加载到以 .txt 为扩展名的文件中
b = np.loadtxt('out.txt')