numpy.nancumprod() tính tích lũy (cumulative product) bỏ qua NaN (coi như 1). Tham số dtype cho phép ép kiểu kết quả.
Ví dụ với dtype=int
import numpy as np
# Mảng 2D có NaN
arr = np.array([[5, 10, 15],
[20, np.nan, 30]])
print("Mảng:", arr)
print("Số chiều:", arr.ndim)
print("Kiểu dữ liệu:", arr.dtype)
# Tích lũy theo axis=1, ép kiểu int
result = np.nancumprod(arr, axis=1, dtype=int)
print("Tích lũy (axis=1, dtype=int):", result)
Kết quả
Mảng: [[ 5. 10. 15.]
[20. nan 30.]]
Số chiều: 2
Kiểu dữ liệu: float64
Tích lũy (axis=1, dtype=int): [[ 5 50 750]
[ 20 20 600]]
Hàng 1: 5, 5×10=50, 50×15=750. Hàng 2: 20, 20×1(NaN)=20, 20×30=600. Kết quả ép kiểu int.