np.eye(10)*10
# 10阶方阵,当对角线值为1时为对角矩阵
np.eye(5)
array([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
a = np.arange(10)
print(a)
print(a.shape)
# 行向量
a = np.arange(10).reshape(10,1)
print(a)
print(a.shape)
# 列向量
[0 1 2 3 4 5 6 7 8 9] (10,)
[[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]] (10, 1)
ar1 = np.arange(12).reshape(3,4)
ar2 = np.arange(10,22).reshape(3,4)
ar3 = np.ones((3,4))
ar4 = np.ones((3,5))
矩阵加法
print(ar1+ar2)
print(ar1+ar2+ar3)
#print(ar1+ar4)
# shape需要相同
# 数与矩阵相乘
ar1 * 10
# 数组与矩阵相乘
print(ar1*ar2)
#print(ar1*ar4)
print('------')
# 数组相乘 → numpy里面两个shape相同的数组可以直接相乘,对应位置的值的乘积为结果
# 如果shape不同,则报错
a1 = np.array([2,3,4])
b1 = np.array([5,6,7]).reshape(3,1) # 转换为列向量
c1 = np.dot(a1,b1)
print(a1.shape,b1.shape,c1.shape)
print(c1,type(c1))
a2 = np.array([
[1,2,3],
[2,3,4]
])
b2 = np.array([
[4,4],
[5,5],
[6,6]
])
c2 = np.dot(a2,b2)
print(a2.shape,b2.shape,c2.shape)
print(c2)
# 矩阵乘法,需要保证第一个矩阵的列数(column)和第二个矩阵的行数(row)相同
# 设 A = (aij) 是一个m×s 矩阵, B = (bij)是一个s×n矩阵,那么规定矩阵A与矩阵B的乘积是一个m×n的矩阵
# 矩阵相乘结果仍为矩阵
# numpy中用.dop()来计算矩阵乘法
# 矩阵乘法:A*B 与 B*A
a3 = np.array([
[-2,4],
[1,-2]
])
b3 = np.array([
[2,4],
[-3,-6]
])
print(np.dot(a3,b3))
print(np.dot(b3,a3))
# 矩阵的转置
A = np.array([
[2,0,-1],
[1,3,2]
])
B = np.array([
[1,7,-1],
[4,2,3],
[2,0,1]
])
np.dot(A,B).T
逆矩阵
# 创建A矩阵
A = np.array([
[1,2,3],
[2,2,1],
[3,4,3]
])
print(A)
print(np.linalg.det(A))
# numpy求逆矩阵B → np.linalg.inv()
B = np.linalg.inv(A)
print(B)
print(np.linalg.det(B))
# A*B = E,单位矩阵
E = np.dot(A,B)
print(E)
print(np.linalg.det(E))
#np.eye(3)
# 伴随矩阵
A_bs = B*np.linalg.det(A)
print(A_bs)
print(np.linalg.det(A_bs))