numpy中的norm()函數(shù)求范數(shù)實例
numpy norm()函數(shù)求范數(shù)
函數(shù):
norm(x, ord = None, axis = None, keepdims = False)
ord表示求什么類型的范數(shù)
舉例說明
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)
運行結(jié)果:
axis=0表示對矩陣的每一列求范數(shù),axis=1表示對矩陣的每一行求范數(shù), keeptdims=True表示結(jié)果保留二維特性,keepdims=False表示結(jié)果不保留二維特性
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)
運行結(jié)果:
numpy求解范數(shù)(numpy.linalg.norm)以及各階范數(shù)詳解
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必須是一維或二維數(shù)組axis
指定,x
任意axis
未指定,ord
未指定,此時x
任意,返回值為x被展平后的一維向量x.ravel
的二范數(shù)。
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.
范數(shù)的階數(shù),可以不指定。默認為None。inf代表無窮大,-inf為無窮小。
可選的階數(shù)見下圖:
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
是整數(shù),指定了一個維度,在該維度上按照向量進行范數(shù)計算。如果是一個二元整數(shù)組,指定了兩個維度,在指定的這兩個維度上可以構(gòu)成矩陣。
對這些矩陣進行計算。如果沒有指定axis
,那么對于一維輸入返回其向量形式的范數(shù)計算值,對于二維輸入返回其矩陣形式的范數(shù)。默認值為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
,被指定計算范數(shù)的維度將在返回結(jié)果中保留,其size為1。計算結(jié)果會在該維度上進行broadcast
各范數(shù)詳析
NOTE: 對于ord<1
的各個范數(shù),結(jié)果在嚴格意義不等于數(shù)學意義上的范數(shù)。但在數(shù)值計算層面仍然有效。
默認情況
當不指定ord時,即ord = None
,對于矩陣,計算其Frobenius norm
,對于向量,計算其2-norm
Frobenius范數(shù)
ord = 'fro'
其公式為:
F范數(shù)只對矩陣存在。其值為對所有元素的絕對值的平方求和后開平方。
Nuclear范數(shù)(核范數(shù))
ord = 'nuc'
- 只對矩陣存在,矩陣的核范數(shù)等于其所有奇異值的和。
無窮大范數(shù)
- 對于矩陣:
max(sum(abs(x), axis=1))
,每一行最終得到一個數(shù),返回最大的數(shù)。 - 對于向量:
max(abs(x)
無窮小范數(shù)
- 對于矩陣:
min(sum(abs(x),axis=1))
,每一行得到一個數(shù),返回最小的數(shù)。 - 對于向量:
min(abs(x))
0 范數(shù)
- 對于矩陣:不存在
- 對于向量:
sum(x!=0)
所有非零元素的和
1 范數(shù)
- 對于矩陣:
max(sum(abs(x)),axis=0
,每一列得到一個數(shù),返回最大值。 - 對于向量:
sum(abs(x)**ord)**(1./ord)
-1 范數(shù)
- 對于矩陣:
min(sum(abs(x)),axis=0
,每一列得到一個數(shù),返回最小值。 - 對于向量:
sum(abs(x)**ord)**(1./ord)
2 范數(shù)
- 對于矩陣:最大的奇異值
- 對于向量:
sum(abs(x)**ord)**(1./ord)
-2范數(shù)
- 對于矩陣:最小的奇異值
- 對于向量:
sum(abs(x)**ord)**(1./ord)
其余int值對應的范數(shù)
- 對于矩陣: Undefined
- 對于向量:
sum(abs(x)**ord)**(1./ord)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
class類在python中獲取金融數(shù)據(jù)的實例方法
在本篇文章里小編給大家整理了關(guān)于class類怎樣在python中獲取金融數(shù)據(jù)的相關(guān)內(nèi)容,有需要的朋友們可以學習下。2020-12-12ansible動態(tài)Inventory主機清單配置遇到的坑
這篇文章主要介紹了ansible動態(tài)Inventory主機清單配置遇到的坑,需要的朋友可以參考下2020-01-01