numpy矩阵的使用

numpy中矩阵和数组的对比

数组可以实现矩阵的所有功能,但是矩阵在实现一些功能的时候操作更加简便,比如矩阵的乘法直接使用A*B而不是使用函数,但是数组可以更加灵活的处理各种数据,而且可以表示高维数组,速度更快

numpy.matrix

创建矩阵

matrix ( 字符串/列表/元组/数组 )
mat ( 字符串/列表/元组/数组 )

a= np.matrix('1 2 3;4 5 6')
b = np.mat([
    [1,2,3],
    [4,5,6]
])
print(a)
print(type(a))
print(b)
print(type(b))

numpy矩阵的使用_第1张图片

c = np.array([
    [1,2,3],
    [4,5,6]
])
d = np.mat(c)
print(d)
print(type(c))
print(type(d))

矩阵对象的属性

属性 说明
.dim 矩阵的维数
.shape 矩阵的形状
.size 矩阵的元素个数
.dtype 元素的数据类型
print(d)
print(d.ndim)
print(d.shape)
print(d.size)
print(d.dtype)

矩阵运算

矩阵的乘法

numpy矩阵的使用_第2张图片

m1 = np.mat([
    [0,1],
    [2,3]
])

m2 = np.mat([
    [1,1],
    [2,0]
])

print(m1*m2)

矩阵转置

矩阵转置:.T

求逆矩阵

矩阵求逆: .I

m = np.mat([
    [0,1],
    [2,3]
])

print(m.T)
print(m.I)

你可能感兴趣的:(python)