矩阵操作
基本操作
- 向量
- 类型
- 取值
- 判断
- 矩阵
- 维度
- 求值
- 比较
- 特定赋值
- 类型转换
- 求最值
import numpy as np
'向量'
v = np.array([1, 2, 3, 4])
print(v)
# [1 2 3 4]
'类型:会自动转化为同一类型'
print(v.dtype)
# int32
'求维度'
print(v.shape)
# (4,)
'向量取值'
print(v[1:3])
# [2 3]
'向量判断'
print(v == 2)
# [False True False False]
t = (v == 2)
print(v[t])
# [2]
'矩阵'
m = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(m)
# [[1 2 3]
# [4 5 6]]
print(m.shape)
# (2, 3)
'取值:index从0开始'
print(m[1, 0])
# 4
'矩阵求值:冒号表示所有行即占位符,以逗号分割表示行列'
print(m[:, 1])
# [2 5]
print(m[:1, 0:1])
# [[1]]
'矩阵判断'
print(m == 3)
# [[False False True]
# [False False False]]
t = (m[:, 2] == 3)
print(m[t, :])
# [[1 2 3]]
'向量比较'
v = np.array([5, 10, 15, 20])
t = (v == 10) & (v == 5)
print(t)
# [False False False False]
v = np.array([5, 10, 15, 20])
t = (v == 10) | (v == 5)
print(t)
# [ True True False False]
'特定赋值'
v[t] = 66
print(v)
# [66 66 15 20]
'矩阵特定赋值'
m = np.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
t = m[:, 1] == 25
print(t)
# [False True False]
m[t, 1] = -1
print(m)
# [[ 5 10 15]
# [20 -1 30]
# [35 40 45]]
'向量的类型转换'
v = np.array(['1', '2', '3'])
print(v.dtype)
# <U1
print(v)
# ['1' '2' '3']
v = v.astype(float)
print(v.dtype)
# float64
print(v)
# [1. 2. 3.]
'向量求最值'
v = np.array([1, 2, 3, 4])
print(v.max(), v.min())
# 4 1
'矩阵求和:axis=0表示按列求和, axis=1表示按行求和'
m = np.array([
[1, 2, 3],
[3, 4, 5],
[5, 6, 7]
])
print(m.sum(axis=1))
# [ 30 75 120]
矩阵操作
- 生成序列
- 序列转矩阵
- 维数
- 元素个数
- 零矩阵
- 一矩阵
- 随机矩阵
- 范围矩阵
- 矩阵加减
- 矩阵乘方
- 矩阵比较
- 矩阵内积
- 矩阵外积
import numpy as np
print(np.arange(15))
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
a = np.arange(15).reshape(3, 5)
print(a)
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
print(a.shape)
# (3, 5)
print(a.dtype.name)
# int32
'求维数'
print(a.ndim)
# 2
'求元素个数'
print(a.size)
# 15
print(np.zeros((3, 4)))
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
print(np.ones((2, 3, 4), dtype=np.int32))
# [[[1 1 1 1]
# [1 1 1 1]
# [1 1 1 1]]
#
# [[1 1 1 1]
# [1 1 1 1]
# [1 1 1 1]]]
print(np.arange(10, 30, 5))
# [10 15 20 25]
print(np.random.random((2, 3)))
# [[0.44358543 0.52329237 0.62330638]
# [0.4742094 0.17395022 0.36088141]]
from numpy import pi
'从0开始到2*pi结束,一共10个数'
print(np.linspace(0, 2*pi, 10))
# [0. 0.6981317 1.3962634 2.0943951 2.7925268 3.4906585
# 4.1887902 4.88692191 5.58505361 6.28318531]
'矩阵运算'
a = np.array([20, 30, 40, 50])
b = np.arange(4)
print(a)
# [20 30 40 50]
print(b)
# [0 1 2 3]
c = a - b
print(c)
# [20 29 38 47]
c = c - 1
print(c)
# [19 28 37 46]
print(b ** 2)
# [0 1 4 9]
print(a < 35)
# [ True True False False]
a = np.array([
[1, 1],
[0, 1]
])
b = np.array([
[2, 0],
[3, 4]
])
'矩阵内积:对应位置相乘'
print(a * b)
# [[2 0]
# [0 4]]
'矩阵外积:矩阵乘法'
print(a.dot(b)) # 或print(np.dot(a, b))
# [[5 4]
# [3 4]]
常用函数
- e次方
- 矩阵转序列
- 矩阵转置
- 拼接矩阵
- 切分矩阵
- 复制矩阵
- 求矩阵下标
- 矩阵整体变换
- 矩阵排序
import numpy as np
b = np.arange(3)
print(b)
# [0 1 2]
print(np.exp(b))
# [1. 2.71828183 7.3890561 ]
print(np.sqrt(b))
# [0. 1. 1.41421356]
a = np.floor(10 * np.random.random((3, 4)))
print(a)
# [[3. 4. 5. 9.]
# [3. 5. 1. 8.]
# [0. 4. 3. 7.]]
print(a.ravel())
# [3. 4. 5. 9. 3. 5. 1. 8. 0. 4. 3. 7.]
a.shape = (6, 2)
print(a)
# [[3. 4.]
# [5. 9.]
# [3. 5.]
# [1. 8.]
# [0. 4.]
# [3. 7.]]
print(a.T)
# [[3. 5. 3. 1. 0. 3.]
# [4. 9. 5. 8. 4. 7.]]
a = np.floor(10 * np.random.random((2, 2)))
b = np.floor(10 * np.random.random((2, 2)))
print(a)
# [[2. 0.]
# [9. 6.]]
print(b)
# [[1. 5.]
# [1. 8.]]
'拼接矩阵:vstack是垂直拼接,hstack是水平拼接'
print(np.vstack((a, b)))
# [[2. 0.]
# [9. 6.]
# [1. 5.]
# [1. 8.]]
a = np.floor(10 * np.random.random((2, 12)))
print(a)
# [[3. 0. 4. 5. 5. 3. 7. 8. 2. 6. 9. 0.]
# [2. 8. 2. 7. 7. 8. 6. 1. 1. 1. 9. 8.]]
'平均切分,同上分水平和垂直'
print(np.hsplit(a, 3))
# [array([[3., 0., 4., 5.],
# [2., 8., 2., 7.]]), array([[5., 3., 7., 8.],
# [7., 8., 6., 1.]]), array([[2., 6., 9., 0.],
# [1., 1., 9., 8.]])]
'指定位置切分,同上分水平和垂直'
print(np.hsplit(a, (3, 4)))
# [array([[3., 0., 4.],
# [2., 8., 2.]]), array([[5.],
# [7.]]), array([[5., 3., 7., 8., 2., 6., 9., 0.],
# [7., 8., 6., 1., 1., 1., 9., 8.]])]
'复制操作1:深复制,相当于同一对象'
a = np.arange(12)
b = a
print(b is a)
# True
b. shape = (3, 4)
print(a.shape)
# (3, 4)
print(id(a))
# 2007509792608
print(id(b))
# 2007509792608
'复制操作2:浅复制,不同对象,但是操作的值是统一的'
c = a.view()
print(c is a)
# False
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
c[0, 0] = 123
print(a)
# [[123 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(id(a))
# 2827766237024
print(id(b))
# 2827766237024
'复制操作3:直接复制,不同对象,互不关联'
d = a.copy()
print(d is a)
# False
print(a)
# [[123 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
d[0, 0] = 999
print(d)
# [[999 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(a)
# [[123 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(id(a))
# 2260966013008
print(id(d))
# 2260816938688
data = np.sin(np.arange(20)).reshape(5, 4)
print(data)
# [[ 0. 0.84147098 0.90929743 0.14112001]
# [-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
# [ 0.98935825 0.41211849 -0.54402111 -0.99999021]
# [-0.53657292 0.42016704 0.99060736 0.65028784]
# [-0.28790332 -0.96139749 -0.75098725 0.14987721]]
'找到每列最大值的下标,axis=0表示按列'
ind = data.argmax(axis=0)
print(ind)
# [2 0 3 1]
print(range(data.shape[1]))
# range(0, 4)
data_max = data[ind, range(data.shape[1])]
print(data_max)
# [0.98935825 0.84147098 0.99060736 0.6569866 ]
a = np.arange(0, 40, 10)
print(a)
# [ 0 10 20 30]
'对矩阵整体变换'
b = np.tile(a, (3, 5))
print(b)
# [[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
# [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
# [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
a = np.array([
[4, 3, 5],
[1, 2, 1]
])
print(a)
# [[4 3 5]
# [1 2 1]]
b = np.sort(a, axis=1)
print(b)
# [[3 4 5]
# [1 1 2]]
a.sort(axis=1)
print(a)
# [[3 4 5]
# [1 1 2]]
a = np.array([4, 3, 1, 2])
'得出升序后下标'
j = np.argsort(a)
print(j)
# [2 3 1 0]
print(a[j])
# [1 2 3 4]
IO操作
四种存取方式
import numpy as np
'采用二进制存取单个矩阵数组'
a = np.array([1, 2, 3, 4, 5])
'保存到 outfile.npy 文件上'
np.save('outfile.npy', a)
'读取文件'
b = np.load('outfile.npy')
print(b)
# [1 2 3 4 5]
'采用二进制存取多个矩阵数组'
a = np.array([
[1, 2, 3],
[4, 5, 6]
])
b = np.arange(0, 1.0, 0.1)
c = np.sin(b)
np.savez("runoob.npz", a=a, b=b, sin_array=c)
r = np.load("runoob.npz")
'查看各个数组名称'
print(r.files)
# ['a', 'b', 'sin_array']
'数组 a'
print(r["a"])
# [[1 2 3]
# [4 5 6]]
'数组 b'
print(r["b"])
# [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
'数组 c'
print(r["sin_array"])
# [0. 0.09983342 0.19866933 0.29552021 0.38941834 0.47942554
# 0.56464247 0.64421769 0.71735609 0.78332691]
'采用文本存取矩阵数组'
a = np.array([1, 2, 3, 4, 5])
np.savetxt('out.txt', a)
b = np.loadtxt('out.txt')
print(b)
# [1. 2. 3. 4. 5.]
'以二进制的制定格式存取矩阵数组'
a = np.arange(0, 10, 0.5).reshape(4, -1)
'改为保存为整数,以逗号分隔'
np.savetxt("out.txt", a, fmt="%d", delimiter=",")
'load 时也要指定为逗号分隔'
b = np.loadtxt("out.txt", delimiter=",")
print(b)