在 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