บทนำ
การคืนค่า Norm ของเมทริกซ์หรือเวกเตอร์ในพีชคณิตเชิงเส้น ใช้เมธอด LA.norm() ใน NumPy พารามิเตอร์ axis=1 ระบุการคำนวณ vector norms ตามแกน 1 (แถว)
ขั้นตอนการทำงาน
1. นำเข้าไลบรารี
import numpy as np from numpy import linalg as LA
2. สร้างอาร์เรย์
arr = np.array([[1, 2, 3], [-1, 1, 4]])
3. แสดงอาร์เรย์
print("Our Array...\n", arr)
4. ตรวจสอบมิติ
print("\nDimensions of our Array...\n", arr.ndim)
5. ตรวจสอบชนิดข้อมูล
print("\nDatatype of our Array object...\n", arr.dtype)
6. ตรวจสอบรูปร่าง
print("\nShape of our Array object...\n", arr.shape)
7. คำนวณ Norm บน axis=1
print("\nResult...\n", LA.norm(arr, axis=1))
ตัวอย่างโค้ดสมบูรณ์
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)
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]