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