Tham số axis trong LA.norm() cho phép tính chuẩn theo trục cụ thể. Với ma trận 2D, axis=0 tính chuẩn theo cột, axis=1 tính theo hàng.
Ví dụ (trùng với bài trước, nhưng giải thích axis)
import numpy as np
from numpy import linalg as LA
arr = np.array([[-4, -3, -2],
[-1, 0, 1],
[ 2, 3, 4]])
print("Ma trận:", arr)
# Chuẩn inf mặc định (toàn ma trận)
print("Chuẩn inf (toàn ma trận):", LA.norm(arr, np.inf))
# Chuẩn inf theo axis=0 (theo cột)
print("Chuẩn inf axis=0:", LA.norm(arr, np.inf, axis=0))
# Chuẩn inf theo axis=1 (theo hàng)
print("Chuẩn inf axis=1:", LA.norm(arr, np.inf, axis=1))
Kết quả
Ma trận: [[-4 -3 -2]
[-1 0 1]
[ 2 3 4]]
Chuẩn inf (toàn ma trận): 9.0
Chuẩn inf axis=0: [4. 3. 4.] # max tuyệt đối mỗi cột
Chuẩn inf axis=1: [4. 1. 4.] # max tuyệt đối mỗi hàng
Tham số axis linh hoạt cho phép tính chuẩn vector con theo chiều mong muốn.