在 Python 的线性代数中,返回向量沿轴 1 的范数
在 Python Numpy 中,要返回线性代数中矩阵或向量的范数,可以使用 LA.norm() 方法。第一个参数 x 是输入数组。如果 axis 为 None,则 x 必须为 1 维或 2 维,除非 ord 为 None。如果 axis 和 ord 都为 None,则返回 x.ravel 的 2 范数。第二个参数 ord 是范数的阶数。inf 表示 numpy 的 inf 对象。默认为 None。
第三个参数 axis,如果为整数,则指定计算向量范数的 x 轴。如果 axis 为 2 元组,则指定保存 2 维矩阵的轴,并计算这些矩阵的矩阵范数。如果 axis 为 None,则返回向量范数(当 x 为 1 维时)或矩阵范数(当 x 为 2 维时)。默认为 None。
第四个参数 keepdims,如果设置为 True,则对齐进行范数计算的轴将保留在结果中作为大小为 1 的维度。使用此选项,结果将针对原始 x 正确广播。
步骤
首先,导入所需的库 -
import numpy as np from numpy import linalg as LA
创建数组 -
arr = np.array([[ 1, 2, 3], [-1, 1, 4]])
显示数组 -
print("Our Array...\n",arr)检查维度 -
print("\nDimensions of our Array...\n",arr.ndim)获取数据类型 -
print("\nDatatype of our Array object...\n",arr.dtype)获取形状 -
print("\nShape of our Array object...\n",arr.shape)在 Python Numpy 中,要返回线性代数中矩阵或向量的范数,可以使用 LA.norm() 方法 -
print("\nResult...\n",LA.norm(arr, axis = 1))示例
import numpy as np
from numpy import linalg as LA
# Create an array
arr = np.array([[ 1, 2, 3],[-1, 1, 4]])
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# Get the Shape
print("\nShape of our Array object...\n",arr.shape)
# To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy
print("\nResult...\n",LA.norm(arr, axis = 1))输出
Our Array... [[ 1 2 3] [-1 1 4]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 3) Result... [3.74165739 4.24264069]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP