เพื่อหาผลคูณใน (Inner product) ของอาร์เรย์และสเกลาร์ ให้ใช้ numpy.inner() สำหรับอาร์เรย์ 1 มิติเป็น inner product ธรรมดา สำหรับมิติสูงกว่าเป็น sum product บนแก้สุดท้าย
ขั้นตอนการใช้งาน
import numpy as np
สร้างอาร์เรย์ identity 5x5 และสเกลาร์
arr = np.eye(5)
val = 2
print("Array...\n", arr)
print("\nDatatype of Array...\n", arr.dtype)
print("\nDimensions of Array...\n", arr.ndim)
print("\nShape of Array...\n", arr.shape)
คำนวณ Inner product กับสเกลาร์
print("\nResult (Inner Product)...\n", np.inner(arr, val))
โค้ดตัวอย่างสมบูรณ์
import numpy as np
arr = np.eye(5)
val = 2
print("Array...\n", arr)
print("\nDatatype of Array...\n", arr.dtype)
print("\nDimensions of Array...\n", arr.ndim)
print("\nShape of Array...\n", arr.shape)
print("\nResult (Inner Product)...\n", np.inner(arr, val))
ผลลัพธ์ที่คาดหวัง
Array... [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]] Datatype of Array... float64 Dimensions of Array... 2 Shape of Array... (5, 5) Result (Inner Product)... [[2. 0. 0. 0. 0.] [0. 2. 0. 0. 0.] [0. 0. 2. 0. 0.] [0. 0. 0. 2. 0.] [0. 0. 0. 0. 2.]]