Python——scipy.linalg.norm


scipy.linalg 包的 norm() 函式用於返回八種不同的矩陣範數之一或無限數量的向量範數之一。

語法

scipy.linalg.norm(x)

其中,x 是輸入陣列或方陣。

示例 1

讓我們考慮以下示例 −

# Importing the required libraries from scipy
from scipy import linalg
import numpy as np

# Define the input array
x = np.array([7 , 4])
print("Input array:
", x) # Calculate the L2 norm r = linalg.norm(x) # Calculate the L1 norm s = linalg.norm(x, 3) # Display the norm values print("Norm Value of r :", r) print("Norm Value of s :", s)

輸出

上述程式將生成以下輸出 −

Input array:
[7 4]
Norm Value of r : 8.06225774829855
Norm Value of s : 7.410795055420619

示例 2

讓我們再舉一個例子 −

# Importing the required libraries from scipy
from scipy import linalg
import numpy as np

# Define the input array
x = np.array([[ 6, 7, 8], [9, -1, -2]])
print("Input Array :
", x) # Calculate the L2 norm p = linalg.norm(x) # Calculate the L1 norm q = linalg.norm(x, axis=1) # Display the norm values print("Norm Values of P :", p) print("Norm Values of Q :", q)

輸出

它將產生以下輸出 −

Input Array :
[[ 6 7 8]
[ 9 -1 -2]]
Norm Values of P : 15.329709716755891
Norm Values of Q : [12.20655562 9.2736185 ]

更新於:2021-12-22

475 檢視次數

啟動你的 職業生涯

完成該課程,獲得認證

開始學習
Advertisement
© . All rights reserved.