numpy的基础操作
1. numpy的基础操作
1.1 数据格式
dtype:data type
数据格式: float, uint8, float32
shape
ndim:维度
1.2 创建不同数据格式的数组
列表转直接数组
import numpy as np
x = np.array([1, 2, 3])
print(x)
print(x.dtype)
输出
[1 2 3]
int32
列表转指定数据格式的数组
y = np.uint8([1, 1, 1])
print(y)
print(y.dtype)
输出
[1 1 1]
uint8
z = np.float32(x)
print(z) # [1. 2. 3.]
print(z.dtype) # float32
输出
[1. 2. 3.]
float32
1.3 numpy的几个函数
np.arange()
start, end, step
start:t默认为0
step:默认为1
nprange = np.arange(10)
print(nprange)
print(nprange.dtype)
# [0 1 2 3 4 5 6 7 8 9]
# int32
nprange2 = np.arange(2, 10, 1.5) # start end step
print(nprange2)
print(nprange2.dtype)
# [2. 3.5 5. 6.5 8. 9.5]
# float64
np.linspace()
start, end, num
start:t默认为0
step:默认为1
nprange3 = np.linspace(0, 10, 21) # start end num
print(nprange3)
print(nprange3.dtype)
# [ 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5 10. ]
# float64
np.zeros()
创建0矩阵
zero_array = np.zeros((2, 4), dtype=np.uint8)
print(zero_array)
print(zero_array.dtype)
# [[0 0 0 0]
# [0 0 0 0]]
# uint8
np.ones()
创建1矩阵
one_array = np.ones((3, 3), dtype=np.float32)
print(one_array)
print(one_array.dtype)
# [[1. 1. 1.]
# [1. 1. 1.]
# [1. 1. 1.]]
# float32
np.identity() 创建单位矩阵
创建单位矩阵
# 创建单位矩阵
unit_narray = np.identity(4) # 秩
print(unit_narray.dtype)
print(unit_narray)
# float64
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]]
np.eye() 创建单位矩阵
创建单位矩阵
unit_narray2 = np.eye(3) # 秩
print(unit_narray2.dtype)
print(unit_narray2)
# float64
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
np.random.randint()
start, end, shape, dtype
random_narray = np.random.randint(0, 256, (3, 3), dtype=np.uint8) # start end shape dtype
print(random_narray.dtype)
print(random_narray)
# uint8
# [[185 20 160]
# [169 216 57]
# [100 138 106]]
1.4 矩阵变换
A.reshape()
重新设置矩阵的形状
A = np.ones((3,3), dtype=np.uint8)
print(A)
print(A.reshape((1,9)))
print(A.reshape((1,-1)))
# [[1 1 1]
# [1 1 1]
# [1 1 1]]
# [[1 1 1 1 1 1 1 1 1]]
# [[1 1 1 1 1 1 1 1 1]]
A.flatten() 将矩阵拉平为一维
print(A.flatten())
# [1 1 1 1 1 1 1 1 1]
A.ravel() 将矩阵拉平为一维
print(A.ravel())
# [1 1 1 1 1 1 1 1 1]
A.T 转置
B = np.random.randint(0, 30, (3,3), dtype=np.uint8)
print(B)
print(B.T)
# [[17 8 26]
# [ 4 0 5]
# [21 17 19]]
# [[17 4 21]
# [ 8 0 17]
# [26 5 19]]
A.transpose() 转置
B = np.random.randint(0, 30, (3,3), dtype=np.uint8)
print(B)
print(B.transpose())
# [[17 8 26]
# [ 4 0 5]
# [21 17 19]]
# [[17 4 21]
# [ 8 0 17]
# [26 5 19]]
np.hstack() 水平堆叠矩阵
水平堆叠矩阵
print(np.hstack([A, B, A, B]))
# [[ 1 1 1 17 8 26 1 1 1 17 8 26]
# [ 1 1 1 4 0 5 1 1 1 4 0 5]
# [ 1 1 1 21 17 19 1 1 1 21 17 19]]
np.vstack() 竖直堆叠矩阵
print(np.vstack([A, B, A, B]))
# [[ 1 1 1]
# [ 1 1 1]
# [ 1 1 1]
# [17 8 26]
# [ 4 0 5]
# [21 17 19]
# [ 1 1 1]
# [ 1 1 1]
# [ 1 1 1]
# [17 8 26]
# [ 4 0 5]
# [21 17 19]]
1.5 numpy统计矩阵特征
print(B.mean()) # 针对整个矩阵而言
print(np.mean(B))
print(B.max()) # 针对整个矩阵而言
print(np.max(B))
print(B.min()) # 针对整个矩阵而言
print(np.min(B))
print(B.max(axis=0)) # 列方向
print(B.max(axis=1)) # 行方向
# 13.0
# 13.0
# 26
# 26
# 0
# 0
# [21 17 26]
# [26 5 21]
1.6 矩阵数学运算
矩阵相加
对应位置相加
print(np.hstack([A, B])) # 方便查看数据
print(A + B)
# [[ 1 1 1 17 8 26]
# [ 1 1 1 4 0 5]
# [ 1 1 1 21 17 19]]
# [[18 9 27]
# [ 5 1 6]
# [22 18 20]]
矩阵相减
对应位置相减
print(np.hstack([A, B]))
print(B - A)
# [[ 1 1 1 17 8 26]
# [ 1 1 1 4 0 5]
# [ 1 1 1 21 17 19]]
# [[ 16 7 25]
# [ 3 255 4]
# [ 20 16 18]]
矩阵相乘
对应位置相乘
print(np.hstack([A, B]))
print(A * B)
# [[ 1 1 1 20 28 23]
# [ 1 1 1 2 25 2]
# [ 1 1 1 2 10 9]]
# [[20 28 23]
# [ 2 25 2]
# [ 2 10 9]]
矩阵相除
对应位置相除
print(np.hstack([A, B]))
print(B / A)
# [[ 1 1 1 21 8 5]
# [ 1 1 1 0 11 26]
# [ 1 1 1 6 7 26]]
# [[21. 8. 5.]
# [ 0. 11. 26.]
# [ 6. 7. 26.]]
np.power() 幂函数
各元素做幂运算
print(B)
print(np.power(B, 2)) # 平方
print(np.power(B, 0.5)) # 开方
print(np.sqrt(B)) # 开方
# [[ 1 26 6]
# [27 28 22]
# [ 8 0 12]]
#
# [[ 1 676 36]
# [729 784 484]
# [ 64 0 144]]
#
# [[1. 5.09901951 2.44948974]
# [5.19615242 5.29150262 4.69041576]
# [2.82842712 0. 3.46410162]]
#
# [[1. 5.09901951 2.44948974]
# [5.19615242 5.29150262 4.69041576]
# [2.82842712 0. 3.46410162]]
np.log() 对数函数
各元素做对数运算
print(np.log(B + 1)) # +1是为了避免真数为0, 默认底数为e
print(np.log2(B + 1)) # +1是为了避免真数为0, 默认底数为e
print(np.log10(B + 1)) # +1是为了避免真数为0, 默认底数为e
# [[0.69314718 3.29583687 1.94591015]
# [3.33220451 3.36729583 3.13549422]
# [2.19722458 0. 2.56494936]]
#
# [[1. 4.7548875 2.80735492]
# [4.80735492 4.857981 4.52356196]
# [3.169925 0. 3.70043972]]
#
# [[0.30103 1.43136376 0.84509804]
# [1.44715803 1.462398 1.36172784]
# [0.95424251 0. 1.11394335]]
点乘和叉乘
print(A)
print(x)
print(A * x) # numpy的广播机制
print(x * A) # numpy的广播机制
print(A @ x)
print(A.dot(x))
print(np.hstack([A, B]))
print(A.dot(B))
# A
# [[1 1 1]
# [1 1 1]
# [1 1 1]]
# x
# [1 2 3]
# A * x
# [[1 2 3]
# [1 2 3]
# [1 2 3]]
# x * A
# [[1 2 3]
# [1 2 3]
# [1 2 3]]
# A @ x
# [6 6 6]
# A.dot(x)
# [6 6 6]
# np.hstack([A, B])
# [[ 1 1 1 1 26 6]
# [ 1 1 1 27 28 22]
# [ 1 1 1 8 0 12]]
# A.dot(B)
# [[36 54 40]
# [36 54 40]
# [36 54 40]]
1.7 矩阵元素值获取、修改
获取
print(B)
print(B[0, :]) # 第0行,所有列
print(B[:,1]) # 所有行,第1列
print(B[1:3,1:3])
print(B[0,0])
# B
# [[11 23 29]
# [15 17 5]
# [ 3 19 17]]
# B[0, :]
# [11 23 29]
# B[:,1]
# [23 17 19]
# B[1:3,1:3]
# [[17 5]
# [19 17]]
# B[0,0]
# 11
修改
print(B)
B[1,1] = 666
print(B)
B[0, :] = np.array([3,3,3])
print(B)
# [[29 10 1]
# [20 5 28]
# [ 5 1 2]]
# [[ 29 10 1]
# [ 20 666 28]
# [ 5 1 2]]
# [[ 3 3 3]
# [ 20 666 28]
# [ 5 1 2]]
完整代码
# 学习网址:
# https://www.bilibili.com/video/BV1YA411K7pp?p=5&spm_id_from=pageDriver
import numpy as np
x = np.array([1, 2, 3])
print(x) # [1 2 3]
print(x.dtype) # int32
y = np.uint8([1, 1, 1])
print(y) # [1 1 1]
print(y.dtype) # uint8
z = np.float32(x)
print(z) # [1. 2. 3.]
print(z.dtype) # float32
"""np的几个函数"""
# 1. range linspace
nprange = np.arange(10)
print(nprange) # [0 1 2 3 4 5 6 7 8 9]
print(nprange.dtype) # int32
nprange2 = np.arange(2, 10, 1.5) # start end step
print(nprange2) # [2. 3.5 5. 6.5 8. 9.5]
print(nprange2.dtype) # float64
nprange3 = np.linspace(0, 10, 21) # start end num
print(nprange3)
print(nprange3.dtype)
# 2. np.zeros() np.ones() np.eye() np.identity np.random.randint()
# 创建0矩阵
zero_array = np.zeros((2, 4), dtype=np.uint8)
print(zero_array.dtype) # uint8
print(zero_array) # [[0 0 0 0][0 0 0 0]]
# 创建1矩阵
one_array = np.ones((3, 3), dtype=np.float32)
print(one_array) # [[1 1 1][1 1 1][1 1 1]]
print(one_array.dtype) # float32
# 创建单位矩阵
unit_narray = np.identity(4) # 秩
print(unit_narray.dtype) # float64
print(unit_narray) # [[1. 0. 0. 0.][0. 1. 0. 0.][0. 0. 1. 0.][0. 0. 0. 1.]]
unit_narray2 = np.eye(3) # 秩
print(unit_narray2.dtype)
print(unit_narray2)
# 创建随机数矩阵
random_narray = np.random.randint(0, 256, (3, 3), dtype=np.uint8) # start end shape dtype
print(random_narray.dtype)
print(random_narray)
"""4. 矩阵转换"""
# A.reshape() A.flatten() A.ravlel() A.T A.transpose() np.hstack([A, B])
# A.reshape()
A = np.ones((3, 3), dtype=np.uint8)
print(A)
# [[1 1 1]
# [1 1 1]
# [1 1 1]]
print(A.reshape((1, 9))) # [[1 1 1 1 1 1 1 1 1]]
print(A.reshape((1, -1))) # [[1 1 1 1 1 1 1 1 1]]
# A.flatten() 拉平为一维
print(A.flatten()) # [1 1 1 1 1 1 1 1 1]
# A.ravlel()
print(A.ravel()) # [1 1 1 1 1 1 1 1 1]
# 转置 A.T A.transpose()
B = np.random.randint(0, 30, (3, 3), dtype=np.uint32)
print(B)
print(B.T)
print(B.transpose())
# np.hstack([A, B, C, ...]) # 水平堆叠
print(np.hstack([A, B, A, B]))
# np.vstack([A, B, C, ...]) # 竖直堆叠
print(np.vstack([A, B, A, B]))
"""5. 矩阵统计方法 最大值、最小值、均值、"""
print(B.mean()) # 针对整个矩阵而言
print(np.mean(B))
print(B.max()) # 针对整个矩阵而言
print(np.max(B))
print(B.min()) # 针对整个矩阵而言
print(np.min(B))
print(B.max(axis=0)) # 列方向
print(B.max(axis=1)) # 行方向
"""6. 数学运算"""
# 矩阵相加 --> 对应位置相加
print(np.hstack([A, B]))
print(A + B)
# 矩阵相减 --> 对应位置相减
print(np.hstack([A, B]))
print(B - A)
# 矩阵叉乘 --> 对应位置相乘
print(np.hstack([A, B]))
print(A * B)
# 矩阵相除 --> 对应位置相除
print(np.hstack([A, B]))
print(B / A)
# 幂函数 --> 对每个位置的数值进行幂运算
print(B)
print(np.power(B, 2)) # 平方
print(np.power(B, 0.5)) # 开方
print(np.sqrt(B)) # 开方
# log函数
print(np.log(B + 1)) # +1是为了避免真数为0, 默认底数为e
print(np.log2(B + 1)) # +1是为了避免真数为0, 默认底数为e
print(np.log10(B + 1)) # +1是为了避免真数为0, 默认底数为e
print(A)
print(x)
print(A * x) # numpy的广播机制
print(x * A) # numpy的广播机制
print(A @ x)
print(A.dot(x))
print(np.hstack([A, B]))
print(A.dot(B))
# 矩阵元素获取
print(B)
print(B[0, :]) # 第0行,所有列
print(B[:,1]) # 所有行,第1列
print(B[1:3,1:3])
print(B[0,0])
# 修改矩阵中的元素值
print(B)
B[1,1] = 666
print(B)
B[0, :] = np.array([3,3,3])
print(B)