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 ]
Advertisement
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP