หากต้องการคืนค่านอร์มของเมทริกซ์หรือเวกเตอร์ในพีชคณิตเชิงเส้น ให้ใช้เมธอด LA.norm() ใน Python Numpy พารามิเตอร์ที่ 1 x คืออาร์เรย์อินพุต ถ้าแกนคือไม่มี x ต้องเป็น 1-D หรือ 2-D เว้นแต่ ord จะเป็น None ถ้าทั้ง axis และ ord เป็น None ค่า 2 norm ของ x.ravel จะถูกส่งกลับ พารามิเตอร์ตัวที่ 2 ord คือลำดับของบรรทัดฐาน inf หมายถึงวัตถุ inf ของ numpy ค่าเริ่มต้นคือไม่มี
แกนพารามิเตอร์ที่ 3 หากเป็นจำนวนเต็ม จะระบุแกนของ x ซึ่งจะคำนวณบรรทัดฐานของเวกเตอร์ ถ้าแกนเป็นทูเพิล 2 แกน จะระบุแกนที่มีเมทริกซ์ 2 มิติ และคำนวณบรรทัดฐานเมทริกซ์ของเมทริกซ์เหล่านี้ หากแกนเป็น None จะส่งคืนบรรทัดฐานเวกเตอร์ (เมื่อ x คือ 1-D) หรือบรรทัดฐานของเมทริกซ์ (เมื่อ x คือ 2-D) ค่าเริ่มต้นคือไม่มี
พารามิเตอร์ตัวที่ 4 keepdims หากตั้งค่าเป็น True แกนที่ถูกทำให้เป็นบรรทัดฐานจะเหลืออยู่ในผลลัพธ์เป็นมิติที่มีขนาดหนึ่ง ด้วยตัวเลือกนี้ ผลลัพธ์จะออกอากาศอย่างถูกต้องเทียบกับ 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)
หากต้องการคืนค่านอร์มของเมทริกซ์หรือเวกเตอร์ในพีชคณิตเชิงเส้น ให้ใช้วิธี LA.norm() ใน Python Numpy -
print("\nResult...\n",LA.norm(arr)) ตัวอย่าง
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)) ผลลัพธ์
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... 5.656854249492381