一:常用矩阵的方法和属性
1.向量转矩阵
print(numpy.arange(15)) a = numpy.arange(15).reshape(3,5) print(a)
打印结果:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Tips:reshpe(3, -1)也可以,-1表示默认。多维如reshape(2, 3,-1)。
2.矩阵的维度
print(a.ndim)
打印结果:2
3.矩阵中数据的类型
print(a.dtype.name)
打印结果:int32
4.矩阵的长度
print(a.size)
打印结果:15
二:矩阵的初始化
1.初始化为0
numpy.zeros((行数, 列数))
注意:此处有两个括号
init1 = numpy.zeros((3, 4)) print(init1)
打印结果:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
注意:此处默认初始化为float
2.初始化为1
init2 = numpy.ones((2,3,4),dtype="int32") print(init2)
打印结果:
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
注意:dtype可以修改初始化后的类型
3.初始化一个等差数列
init3 = numpy.arange(10, 30, 5) print(init3)打印结果:
[10 15 20 25]
4.初始化一个随机矩阵
init4 = numpy.random.random((2,3)) print(init4)
打印结果:
[[ 0.12118478 0.28265194 0.7184926 ]
[ 0.29421611 0.20473122 0.49643855]]
4.在某个区间中等距取值
numpy.linspace(起始值,结束值,间隔)
from numpy import pi init5 = numpy.linspace(0, 2*pi, 10) print(init5)
打印结果:
[ 0. 0.6981317 1.3962634 2.0943951 2.7925268 3.49065854.1887902 4.88692191 5.58505361 6.28318531]
三:矩阵的计算
1.三角函数
a = numpy.sin(numpy.linspace(0, 2*pi, 5)) print(a)打印结果:
[ 0.00000000e+00 1.00000000e+00 1.22464680e-16 -1.00000000e+00
-2.44929360e-16]
不明此处sin(pi)的值为什么不是0
2.简单计算
a = numpy.array([10, 20, 30, 40]) b = numpy.arange(4) print(a - b) print(a + b) print(a * b) print(b / a) print(b ** 2) #乘方 print(a < 35)
打印结果:
[10 19 28 37]
[10 21 32 43]
[ 0 20 60 120]
[ 0. 0.05 0.06666667 0.075 ]
[0 1 4 9]
[ True True True False]
3.矩阵乘法
A = numpy.array([ [1, 1], [0, 1] ]) B = numpy.array([ [2, 0], [3, 4] ]) print(A) print("#####") print(B) print("#####") print(A*B) #对应位置相乘 print("#####") print(A.dot(B)) #矩阵乘法 print("#####") print(numpy.dot(A,B)) #矩阵乘法 print("#####")
打印结果:
[[1 1]
[0 1]]
#####
[[2 0]
[3 4]]
#####
[[2 0]
[0 4]]
#####
[[5 4]
[3 4]]
#####
[[5 4]
[3 4]]
#####
4.平方和开方
a = numpy.arange(3) print(numpy.exp(a)) print(numpy.sqrt(a))
打印结果:
[ 1. 2.71828183 7.3890561 ]
[ 0. 1. 1.41421356]
5.矩阵和向量的转换
a = numpy.floor(10 * numpy.random.random((3, 4))) #向下取整 print(a) print("######") print(a.ravel()) #多维矩阵转向量 a.shape = (6, 2) print(a) print("######") print(a.T) #转置矩阵
打印结果:
[[ 6. 5. 2. 7.]
[ 4. 4. 8. 9.]
[ 6. 9. 7. 0.]]
######
[ 6. 5. 2. 7. 4. 4. 8. 9. 6. 9. 7. 0.]
[[ 6. 5.]
[ 2. 7.]
[ 4. 4.]
[ 8. 9.]
[ 6. 9.]
[ 7. 0.]]
######
[[ 6. 2. 4. 8. 6. 7.]
[ 5. 7. 4. 9. 9. 0.]]
6.矩阵的拼接
a = numpy.floor(10 * numpy.random.random((2, 2))) b = numpy.floor(10 * numpy.random.random((2, 2))) print(a) print("######") print(b) print("######") print(numpy.hstack((a, b))) print("######") print(numpy.vstack((a, b)))
打印结果:
[[ 3. 0.]
[ 2. 7.]]
######
[[ 8. 4.]
[ 7. 4.]]
######
[[ 3. 0. 8. 4.]
[ 2. 7. 7. 4.]]
######
[[ 3. 0.]
[ 2. 7.]
[ 8. 4.]
[ 7. 4.]]
7.矩阵的切割
numpy.hsplit(要切割的矩阵,切割成的份数)
a = numpy.floor(10 * numpy.random.random((2, 12))) print(a) print("#####") print(numpy.hsplit(a, 3)) print("#####") print(numpy.hsplit(a, (3, 4))) #(3, 4)该元组表示在第三个和第四个空隙切割 a = numpy.floor(10 * numpy.random.random((12, 2))) print("#####") print(a) print("#####") print(numpy.vsplit(a, 3))
打印结果:
[[ 9. 7. 6. 1. 4. 2. 4. 9. 1. 2. 2. 0.][ 6. 8. 5. 0. 7. 8. 9. 3. 2. 6. 5. 2.]]
#####
[array([[ 9., 7., 6., 1.],
[ 6., 8., 5., 0.]]), array([[ 4., 2., 4., 9.],
[ 7., 8., 9., 3.]]), array([[ 1., 2., 2., 0.],
[ 2., 6., 5., 2.]])]
#####
[array([[ 9., 7., 6.],
[ 6., 8., 5.]]), array([[ 1.],
[ 0.]]), array([[ 4., 2., 4., 9., 1., 2., 2., 0.],
[ 7., 8., 9., 3., 2., 6., 5., 2.]])]
#####
[[ 2. 8.]
[ 7. 7.]
[ 3. 3.]
[ 5. 2.]
[ 5. 6.]
[ 4. 4.]
[ 5. 4.]
[ 3. 1.]
[ 2. 2.]
[ 4. 6.]
[ 8. 1.]
[ 1. 7.]]
#####
[array([[ 2., 8.],
[ 7., 7.],
[ 3., 3.],
[ 5., 2.]]), array([[ 5., 6.],
[ 4., 4.],
[ 5., 4.],
[ 3., 1.]]), array([[ 2., 2.],
[ 4., 6.],
[ 8., 1.],
[ 1., 7.]])]