當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python dask.array.linalg.norm用法及代碼示例

用法:

dask.array.linalg.norm(x, ord=None, axis=None, keepdims=False)

矩陣或向量範數。

此文檔字符串是從 numpy.linalg.norm 複製的。

可能存在與 Dask 版本的一些不一致之處。

根據ord 參數的值,此函數能夠返回八種不同的矩陣範數之一,或無限數量的向量範數之一(如下所述)。

參數

xarray_like

輸入數組。如果axis 為無,則x 必須為一維或二維,除非ord 為無。如果 axisord 都為 None,則將返回 x.ravel 的 2-norm。

ord{非零整數,inf,-inf,‘fro’, ‘nuc’},可選

規範的順序(參見 Notes 下的表格)。 inf 表示 numpy 的 inf 對象。默認值為無。

axis{無,整數,整數的 2 元組},可選。

如果axis 是整數,它指定x 的軸,沿該軸計算向量範數。如果axis 是一個 2 元組,它指定保存二維矩陣的軸,並計算這些矩陣的矩陣範數。如果axis 為無,則返回向量範數(當x 為一維時)或矩陣範數(當x 為二維時)。默認值為無。

keepdims布爾型,可選

如果將其設置為 True,則規範化的軸將作為尺寸為 1 的尺寸留在結果中。使用此選項,結果將針對原始 x 正確廣播。

返回

n浮點數或 ndarray

矩陣或向量的範數。

注意

對於 ord < 1 的值,嚴格來說,結果不是數學上的 ‘norm’,但它對於各種數值目的可能仍然有用。

可以計算以下規範:

ord

矩陣的範數

向量的範數

None

弗羅貝尼烏斯範數

2範數

‘fro’

弗羅貝尼烏斯範數

-

‘nuc’

核規範

-

inf

最大值(總和(絕對值(x),軸 = 1))

最大值(絕對值(x))

-inf

最小值(總和(絕對值(x),軸 = 1))

最小(絕對值(x))

0

-

總和(x!= 0)

1

最大值(總和(絕對值(x),軸 = 0))

如下

-1

最小值(總和(絕對值(x),軸 = 0))

如下

2

2-範數(最大單值)

如下

-2

最小奇異值

如下

other

-

總和(abs(x)**ord)**(1./ord)

Frobenius 範數由 [1] 給出:

\(||A||_F = [\sum_{i,j} abs(a_{i,j})^2]^{1/2}\)

核範數是奇異值的總和。

Frobenius 和核範數階都隻為矩陣定義,並在 x.ndim != 2 時引發 ValueError 。

參考

1

G. H. Golub 和 C. F. Van Loan,矩陣計算,馬裏蘭州巴爾的摩,約翰霍普金斯大學出版社,1985 年,第 頁。 15

例子

>>> from numpy import linalg as LA  
>>> a = np.arange(9) - 4  
>>> a  
array([-4, -3, -2, ...,  2,  3,  4])
>>> b = a.reshape((3, 3))  
>>> b  
array([[-4, -3, -2],
       [-1,  0,  1],
       [ 2,  3,  4]])
>>> LA.norm(a)  
7.745966692414834
>>> LA.norm(b)  
7.745966692414834
>>> LA.norm(b, 'fro')  
7.745966692414834
>>> LA.norm(a, np.inf)  
4.0
>>> LA.norm(b, np.inf)  
9.0
>>> LA.norm(a, -np.inf)  
0.0
>>> LA.norm(b, -np.inf)  
2.0
>>> LA.norm(a, 1)  
20.0
>>> LA.norm(b, 1)  
7.0
>>> LA.norm(a, -1)  
-4.6566128774142013e-010
>>> LA.norm(b, -1)  
6.0
>>> LA.norm(a, 2)  
7.745966692414834
>>> LA.norm(b, 2)  
7.3484692283495345
>>> LA.norm(a, -2)  
0.0
>>> LA.norm(b, -2)  
1.8570331885190563e-016 # may vary
>>> LA.norm(a, 3)  
5.8480354764257312 # may vary
>>> LA.norm(a, -3)  
0.0

使用 axis 參數計算向量範數:

>>> c = np.array([[ 1, 2, 3],  
...               [-1, 1, 4]])
>>> LA.norm(c, axis=0)  
array([ 1.41421356,  2.23606798,  5.        ])
>>> LA.norm(c, axis=1)  
array([ 3.74165739,  4.24264069])
>>> LA.norm(c, ord=1, axis=1)  
array([ 6.,  6.])

使用 axis 參數計算矩陣範數:

>>> m = np.arange(8).reshape(2,2,2)  
>>> LA.norm(m, axis=(1,2))  
array([  3.74165739,  11.22497216])
>>> LA.norm(m[0, :, :]), LA.norm(m[1, :, :])  
(3.7416573867739413, 11.224972160321824)

相關用法


注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask.array.linalg.norm。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。