np.linalg

np.linalg.inv

np.linalg.inv(a)

计算一个矩阵的逆

Compute the (multiplicative) inverse of a matrix.

Given a square matrix a, return the matrix ainv satisfying dot(a, ainv) = dot(ainv, a) = eye(a.shape[0]).

import numpy as np
from numpy.linalg import inv
a = np.array([[1., 2.], [3., 4.]])
ainv = inv(a)
np.allclose(np.dot(a, ainv), np.eye(2))
np.allclose(np.dot(ainv, a), np.eye(2))
'''
[[-2.   1. ]
 [ 1.5 -0.5]]
True
True
'''


# 可以同时求多个方阵的逆
x1 = np.array([[1., 2.], [3., 4.]])
x2 = np.array([[1, 3], [3, 5]])
a = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]])
print(inv(a))
print(inv(x1))
print(inv(x2))
'''
[[[-2.    1.  ]
  [ 1.5  -0.5 ]]

 [[-1.25  0.75]
  [ 0.75 -0.25]]]


[[-2.   1. ]
 [ 1.5 -0.5]]
[[-1.25  0.75]
 [ 0.75 -0.25]]
'''

np.linalg.norm

np.linalg.norm

计算矩阵或者向量的逆

Matrix or vector norm.

This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.

import numpy as np
import numpy.linalg as LA

a = np.arange(9)
print(a)
print(LA.norm(a, np.inf))  # 无穷范数
print(LA.norm(a, -np.inf))
print(LA.norm(a, 1))  # 1范数
print(LA.norm(a, 2))
'''
[0 1 2 3 4 5 6 7 8]
8.0
0.0
36.0
14.2828568570857
'''
ordnorm for matricesnorm for vectors
NoneFrobenius norm2-norm
‘fro’Frobenius norm
‘nuc’nuclear norm
infmax(sum(abs(x), axis=1))max(abs(x))
-infmin(sum(abs(x), axis=1))min(abs(x))
0sum(x != 0)
1max(sum(abs(x), axis=0))as below
-1min(sum(abs(x), axis=0))as below
22-norm (largest sing. value)as below
-2smallest singular valueas below
othersum(abs(x)ord)(1./ord)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值