import numpy as np
if __name__ == '__main__':
# 矩阵的创建
A = np.array([[1, 2], [3, 4]])
print(A)
# 矩阵的属性, m*n
print(A.shape)
# 矩阵的转置
print(A.T)
# 获取矩阵的元素
print(A[1, 1])
print(A[0])
print(A[:, 0]) # 从头到尾的值
print(A[1, :]) # 得到矩阵的第二行
# 矩阵的基本运算
B = np.array([[5, 6], [7, 8]])
print(A + B)
print(A - B)
print(10 * A)
print(A * 10)
print(A * B)
print(A.dot(B))
# 向量和矩阵的加法
p = np.array([10, 100])
print(A + p)
print(A + 1)
# 矩阵和向量的乘法
print(A.dot(p))
# 返回结果
[[1 2]
[3 4]]
(2, 2)
[[1 3]
[2 4]]
4
[1 2]
[1 3]
[3 4]
[[ 6 8]
[10 12]]
[[-4 -4]
[-4 -4]]
[[10 20]
[30 40]]
[[10 20]
[30 40]]
[[ 5 12]
[21 32]]
[[19 22]
[43 50]]
[[ 11 102]
[ 13 104]]
[[2 3]
[4 5]]
[210 430]
网友评论