np.linalg.norm
时间: 2023-11-02 18:37:41 浏览: 97
np.linalg.norm is a function from the NumPy library in Python that computes the Euclidean norm (magnitude or length) of a vector or matrix. It can be used to calculate the distance between two points in n-dimensional space.
The syntax for np.linalg.norm is:
np.linalg.norm(x, ord=None, axis=None, keepdims=False)
where:
- x: the input vector or matrix
- ord: the order of the norm to be computed (default is 2, i.e., Euclidean norm)
- axis: the axis along which to compute the norm (default is None, i.e., flattened array)
- keepdims: whether to keep the dimensions of the input array (default is False)
Example usage:
import numpy as np
# compute the Euclidean norm of a vector
v = np.array([1, 2, 3])
norm_v = np.linalg.norm(v)
print(norm_v) # Output: 3.7416573867739413
# compute the Euclidean norm of a matrix along rows
A = np.array([[1, 2, 3], [4, 5, 6]])
norm_A = np.linalg.norm(A, axis=1)
print(norm_A) # Output: [3.74165739 8.77496439]
阅读全文