หากต้องการคืนค่านอร์มของเมทริกซ์หรือเวกเตอร์ในพีชคณิตเชิงเส้น ให้ใช้เมธอด LA.norm() ใน PythonNumpy พารามิเตอร์ที่ 1 x คืออาร์เรย์อินพุต ถ้าแกนคือ None x ต้องเป็น 1-D หรือ 2-D เว้นแต่ ord isNone ถ้าทั้ง axis และ ord เป็น None ค่า 2 ของ x.ravel จะถูกส่งคืน
พารามิเตอร์ตัวที่ 2 ord คือลำดับของบรรทัดฐาน inf หมายถึงวัตถุ inf ของ numpy ค่าเริ่มต้นคือไม่มี ค่า "nuc" ที่ตั้งค่าเป็นพารามิเตอร์คือบรรทัดฐานของนิวเคลียร์ ทั้ง Frobenius และกฎเกณฑ์นิวเคลียร์กำหนดไว้สำหรับเมทริกซ์เท่านั้น
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np from numpy import linalg as LA
สร้างอาร์เรย์ -
arr = np.array([[ -4, -3, -2], [-1, 0, 1], [2, 3, 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) หากต้องการคืนค่านอร์มของเมทริกซ์หรือเวกเตอร์ในพีชคณิตเชิงเส้น ให้ใช้วิธี LA.norm() -
print("\nResult...\n",LA.norm(arr, 'nuc')) ตัวอย่าง
import numpy as np
from numpy import linalg as LA
# Create an array
arr = np.array([[ -4, -3, -2],[-1, 0, 1],[2, 3, 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, 'nuc')) ผลลัพธ์
Our Array... [[-4 -3 -2] [-1 0 1] [ 2 3 4]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (3, 3) Result... 9.797958971132713