網站首頁 編程語言 正文
numpy norm()函數求范數
函數:
norm(x, ord = None, axis = None, keepdims = False)
ord表示求什么類型的范數
舉例說明
import numpy as np x = [1,2,3,4] x1 = np.linalg.norm(x=x, ord=1) x2 = np.linalg.norm(x=x, ord=2) x3 = np.linalg.norm(x=x, ord=np.inf) print(x1) print(x2) print(x3)
運行結果:
axis=0表示對矩陣的每一列求范數,axis=1表示對矩陣的每一行求范數, keeptdims=True表示結果保留二維特性,keepdims=False表示結果不保留二維特性
import numpy as np x = np.array([[0, 1, 2], [3, 4, 5]]) x1 = np.linalg.norm(x=x, ord=1, axis=0, keepdims=True) x2 = np.linalg.norm(x=x, ord=1, axis=1, keepdims=True) x3 = np.linalg.norm(x=x, ord=1, axis=0, keepdims=False) x4 = np.linalg.norm(x=x, ord=1, axis=1, keepdims=False) print(x1) print(x2) print(x3) print(x4)
運行結果:
numpy求解范數(numpy.linalg.norm)以及各階范數詳解
numpy.linalg.norm
語法
numpy.linalg.norm(x,ord=None,axis=None,keepdims=False)
Parameters
x: array_like
Input array. If
axis
is None, x must be 1-D or 2-D, unlessord
is None. If bothaxis
andord
are None, the 2-norm ofx.ravel
will be returned.
X是輸入的array, array的情況必須是以下三種情況之一:
-
axis
未指定,ord
指定。此時x必須是一維或二維數組 -
axis
指定,x
任意 -
axis
未指定,ord
未指定,此時x
任意,返回值為x被展平后的一維向量x.ravel
的二范數。
ord:{non-zero int, inf, -inf, ‘fro’, ‘nuc’}, optional
Order of the norm (see table under Notes). inf means numpy’s inf object. The default is None.
范數的階數,可以不指定。默認為None。inf代表無窮大,-inf為無窮小。
可選的階數見下圖:
axis:{None, int, 2-tuple of ints},optional
If
axis
is an integer, it specifies theaxis
of x along which to compute the vector norms. Ifaxis
is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned. The default is None.
如果axis
是整數,指定了一個維度,在該維度上按照向量進行范數計算。如果是一個二元整數組,指定了兩個維度,在指定的這兩個維度上可以構成矩陣。
對這些矩陣進行計算。如果沒有指定axis
,那么對于一維輸入返回其向量形式的范數計算值,對于二維輸入返回其矩陣形式的范數。默認值為None
keepdims: bool, optional
If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.
如果keepdims=True
,被指定計算范數的維度將在返回結果中保留,其size為1。計算結果會在該維度上進行broadcast
各范數詳析
NOTE: 對于ord<1
的各個范數,結果在嚴格意義不等于數學意義上的范數。但在數值計算層面仍然有效。
默認情況
當不指定ord時,即ord = None
,對于矩陣,計算其Frobenius norm
,對于向量,計算其2-norm
Frobenius范數
ord = 'fro'
其公式為:
F范數只對矩陣存在。其值為對所有元素的絕對值的平方求和后開平方。
Nuclear范數(核范數)
ord = 'nuc'
- 只對矩陣存在,矩陣的核范數等于其所有奇異值的和。
無窮大范數
- 對于矩陣:
max(sum(abs(x), axis=1))
,每一行最終得到一個數,返回最大的數。 - 對于向量:
max(abs(x)
無窮小范數
- 對于矩陣:
min(sum(abs(x),axis=1))
,每一行得到一個數,返回最小的數。 - 對于向量:
min(abs(x))
0 范數
- 對于矩陣:不存在
- 對于向量:
sum(x!=0)
所有非零元素的和
1 范數
- 對于矩陣:
max(sum(abs(x)),axis=0
,每一列得到一個數,返回最大值。 - 對于向量:
sum(abs(x)**ord)**(1./ord)
-1 范數
- 對于矩陣:
min(sum(abs(x)),axis=0
,每一列得到一個數,返回最小值。 - 對于向量:
sum(abs(x)**ord)**(1./ord)
2 范數
- 對于矩陣:最大的奇異值
- 對于向量:
sum(abs(x)**ord)**(1./ord)
-2范數
- 對于矩陣:最小的奇異值
- 對于向量:
sum(abs(x)**ord)**(1./ord)
其余int值對應的范數
- 對于矩陣: Undefined
- 對于向量:
sum(abs(x)**ord)**(1./ord)
總結
原文鏈接:https://blog.csdn.net/cjhxydream/article/details/108192497
相關推薦
- 2022-12-14 C語言程序設計之指針的應用詳解_C 語言
- 2023-02-05 不同的編程語言輸出?“Hello?World”?代碼_其它綜合
- 2023-05-22 Pytorch怎樣保存訓練好的模型_python
- 2023-01-05 Kotlin的空安全處理方式詳解_Android
- 2022-05-06 C#迭代器方法介紹_C#教程
- 2022-07-03 golang defer,func()閉包,panic ,recover,contex
- 2022-08-17 C++詳解如何通過模板實現元素的反序_C 語言
- 2022-03-19 Go?語言的?:=的具體使用_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支