ในการรับผลิตภัณฑ์ Outer ของสองอาร์เรย์ ให้ใช้เมธอด numpy.outer() ใน Python พารามิเตอร์ที่ 1 a คือเวกเตอร์อินพุตแรก อินพุตจะถูกทำให้แบนหากไม่ใช่ 1 มิติอยู่แล้ว พารามิเตอร์ตัวที่ 2 b คือเวกเตอร์อินพุตที่สอง อินพุตจะถูกทำให้แบนหากไม่ใช่ 1 มิติอยู่แล้ว พารามิเตอร์ตัวที่ 3 คือตำแหน่งที่เก็บผลลัพธ์
จากเวกเตอร์สองเวกเตอร์ a =[a0, a1, ..., aM] และ b =[b0, b1, ..., bN] ผลคูณภายนอก [1] คือ −
[[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . [ ... . [aM*b0 aM*bN ]]
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น-
import numpy as np
การสร้างอาร์เรย์หนึ่งมิติจำนวนสองอันโดยใช้เมธอด array() -
arr1 = np.array([5, 10, 15]) arr2 = np.array([20, 25, 30])
แสดงอาร์เรย์ -
print("Array1...\n",arr1)
print("\nArray2...\n",arr2) ตรวจสอบขนาดของอาร์เรย์ทั้งสอง -
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim) ตรวจสอบรูปร่างของอาร์เรย์ทั้งสอง -
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape) ในการรับผลิตภัณฑ์ Outer ของสองอาร์เรย์ ให้ใช้วิธี numpy.outer() ใน Python -
print("\nResult (Outer Product)...\n",np.outer(arr1, arr2)) ตัวอย่าง
import numpy as np
# Creating two numpy One-Dimensional array using the array() method
arr1 = np.array([5, 10, 15])
arr2 = np.array([20, 25, 30])
# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)
# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)
# To get the Outer product of two arrays, use the numpy.outer() method in Python
print("\nResult (Outer Product)...\n",np.outer(arr1, arr2)) ผลลัพธ์
Array1... [ 5 10 15] Array2... [20 25 30] Dimensions of Array1... 1 Dimensions of Array2... 1 Shape of Array1... (3,) Shape of Array2... (3,) Result (Outer Product)... [[100 125 150] [200 250 300] [300 375 450]]