基础运算1
生成一个数组
import numpy as np
a = np.array([[1,2,3]
,[2,3,4]])
print(a)
[[1 2 3]
[2 3 4]]
输出数组的属性
print('num of dim:',a.ndim) # 输出数组的维度 记得是ndim
num of dim: 2
print('shape:',a.shape) # 输出数组的形状
shape: (2, 3)
print('size:',a.size) # 输出数组的尺寸(大小) 是2*3的 所以是6
size: 6
指定数组的类型
b = np.array([2,3,4],dtype=np.float64) # 指定array的type 注意!!! 用dtype 有int float 等 后面加位数
print(b)
[2. 3. 4.]
特殊的矩阵生成
c = np.zeros((3,4)) # 定义一个全为零矩阵 3行4列
print(c)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
d = np.ones((3,4)) # 定义一个全为1的矩阵 3行4列
print(d)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
e = np.empty((3,4))
print(e)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
f = np.arange(12,24,2).reshape((3,2)) # arange指定从12到24 步长为2 再将其形状改变成3行两列
print(f)
[[12 14]
[16 18]
[20 22]]
g = np.linspace(1,18,6) # 生成线段 6条线段
print(g)
print(g.reshape((2,3)))
[ 1. 4.4 7.8 11.2 14.6 18. ]
[[ 1. 4.4 7.8]
[11.2 14.6 18. ]]
矩阵次方运算
print(a**2)
[[ 1 4 9]
[ 4 9 16]]
三角函数运算
print(np.sin(a))
print(np.tan(a))
print(np.cos(a))
[[ 0.84147098 0.90929743 0.14112001]
[ 0.90929743 0.14112001 -0.7568025 ]]
[[ 1.55740772 -2.18503986 -0.14254654]
[-2.18503986 -0.14254654 1.15782128]]
[[ 0.54030231 -0.41614684 -0.9899925 ]
[-0.41614684 -0.9899925 -0.65364362]]
矩阵的点乘运算 (不清楚的看线代基础)
print(a)
print(f)
np.dot(f,a) # 相当于 a.dot(f)
[[1 2 3]
[2 3 4]]
[[12 14]
[16 18]
[20 22]]
array([[ 40, 66, 92],
[ 52, 86, 120],
[ 64, 106, 148]])
h = np.arange(6).reshape((2,3))
print(h)
a*h # 逐个相乘 只有同形状才行
[[0 1 2]
[3 4 5]]
array([[ 0, 2, 6],
[ 6, 12, 20]])
随机生成&求最大&求最小&求和
j = np.random.random((2,4)) # 随机生成 2行4列 0_1的值
print(j)
print()
print(j.sum()) # 等同于np.sum(a) 求和
print(j.min()) # 等同于np.min(a) 求最小
print(j.max()) # 等同于np.max(a) 求最大
print(j.mean()) # 求平均
print(np.median(j)) # 求中位数 这里只能用np.median()
print()
print('按行求和:',j.sum(axis=1))
print('按列求和:',j.sum(axis=0))
print('从行中找最小值',j.min(axis=1))
print('从行中找最大值',j.max(axis=1))
[[0.95758581 0.54288804 0.64884771 0.64303491]
[0.97419502 0.85767191 0.93865584 0.8430438 ]]
6.405923030504926
0.5428880351727107
0.9741950241948263
0.8007403788131158
0.8503578534746553
按行求和: [2.79235646 3.61356657]
按列求和: [1.93178084 1.40055995 1.58750354 1.4860787 ]
从行中找最小值 [0.54288804 0.8430438 ]
从行中找最大值 [0.95758581 0.97419502]
累加
print(a)
a.cumsum() # 输出是当前位置之前的数所有之和
[[1 2 3]
[2 3 4]]
array([ 1, 3, 6, 8, 11, 15])
累差
np.diff(a) # 当前位置与前一个位置的差值
array([[1, 1],
[1, 1]])
判断
print(a>1)
[[False True True]
[ True True True]]
print(a==1)
[[ True False False]
[False False False]]
print(a<1)
[[False False False]
[False False False]]
基础计算2
寻找最值
A = np.arange(12,24).reshape((3,4))
print(A)
print(A.argmin()) # 找到矩阵中最小值的位置 A.max()是找到值 别搞混了
print(A.argmax()) # 等同于np.argmax(A)
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
0
11
寻找非零数的位置
np.nonzero(A) # 输出的是第行和列 表示0,0位置是非零的 01 02 03 ……
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),
array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
排序
print(j)
np.sort(j) # 每一行都排序
[[0.95758581 0.54288804 0.64884771 0.64303491]
[0.97419502 0.85767191 0.93865584 0.8430438 ]]
array([[0.54288804, 0.64303491, 0.64884771, 0.95758581],
[0.8430438 , 0.85767191, 0.93865584, 0.97419502]])
转置
A.T # 等同于 np.transpose(A)
array([[12, 16, 20],
[13, 17, 21],
[14, 18, 22],
[15, 19, 23]])
clip
np.clip(A,17,22) # 表示小于17的都用17代替 大于22的都用22代替 其余17-22的都保留
array([[17, 17, 17, 17],
[17, 17, 18, 19],
[20, 21, 22, 22]])
numpy的索引
B = np.arange(3,15).reshape((3,4))
print(B)
print(B[0]) # 第一行
print(B[0][2],B[0,2]) # 等价的
print(B[:,0]) # 第一列
print(B[0:1,0]) # 第一列中的第0到1的数 左闭右开
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[3 4 5 6]
5 5
[ 3 7 11]
[3]
遍历
for row in B:
print(row) # 遍历的是每一行 想一下怎么遍历列呢?
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
for column in B.T:
print(column) # 这样打印出来的每一列
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
遍历数组每一个项
C = np.arange(1,5).reshape(2,2)
print(C)
print('是一个迭代器',C.flat) # 返回的是一个迭代对象
print('是一个数组',C.flatten()) # 返回一个数组 将数组整合在一个数组里面
for item in C.flat:
print(item)
print()
for item in C.flatten(): # 这里我们发现是一样的
print(item)
[[1 2]
[3 4]]
是一个迭代器
是一个数组 [1 2 3 4]
1
2
3
4
1
2
3
4
numpy的array合并
D = np.array([1,1,1])
E = np.array([2,2,2])
F = np.vstack((D,E)) # 表示上下合并 另外D E 顺序对结果有影响
print(F)
print(D.shape,E.shape,F.shape)
[[1 1 1]
[2 2 2]]
(3,) (3,) (2, 3)
G = np.hstack((D,E)) # 这个合并是水平合并(左右合并)
print(G,G.shape)
print(D[:,np.newaxis])
print(D[:,np.newaxis].shape) # 给D的列加上一个维度
H = np.hstack((D[:,np.newaxis],E[:,np.newaxis]))
print(H)
[1 1 1 2 2 2] (6,)
[[1]
[1]
[1]]
(3, 1)
[[1 2]
[1 2]
[1 2]]
I = np.concatenate((D[:,np.newaxis],E[:,np.newaxis]),axis=1)
print(I)
J = np.concatenate((D[np.newaxis,:],E[np.newaxis,:]),axis=0)
print(J)
[[1 2]
[1 2]
[1 2]]
[[1 1 1]
[2 2 2]]
numpy的array分割
A1 = np.arange(12).reshape((3,4))
print(A)
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
np.split(A1,3,axis=0) # 将A1分割成3块
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
np.split(A1,2,axis=1) # 将A1分割成2块
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
不等分分割
print(np.array_split(A1,3,axis=1))
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
numpy的copy
copy
a1 = np.arange(4)
print(a1)
a2 = a1
a3 = a2
a1[0] = 11
print(a1,a2,a3) # 全部指向a1
print(a1 is a2,a1 is a3)
[0 1 2 3]
[11 1 2 3] [11 1 2 3] [11 1 2 3]
True True
deep copy
a4 = np.copy(a1)
a1[0] = 99 # a1 改变了 但是 a4没有改变
print(a4)
print(a4 is a1)
[11 1 2 3]
False