ในการรับผลิตภัณฑ์ Inner ของอาร์เรย์และสเกลาร์ ให้ใช้เมธอด numpy.inner() ใน Python ผลคูณภายในทั่วไปของเวกเตอร์สำหรับอาร์เรย์ 1-D ในมิติที่สูงกว่า เป็นผลคูณรวมของแกนสุดท้าย พารามิเตอร์คือ 1 และ b เวกเตอร์สองตัว หาก a และ b ไม่ใช่สเกลาร์ มิติข้อมูลสุดท้ายจะต้องตรงกัน
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น-
import numpy as np
สร้างอาร์เรย์โดยใช้ numpy.eye() เมธอดนี้ส่งคืนอาร์เรย์ 2 มิติโดยมีอาร์เรย์อยู่ในแนวทแยงและค่าศูนย์อยู่ที่อื่น -
arr = np.eye(5)
วาลคือสเกลาร์ −
val = 2
ตรวจสอบประเภทข้อมูล −
print("\nDatatype of Array...\n",arr.dtype)
ตรวจสอบมิติ −
print("\nDimensions of Array...\n",arr.ndim) ตรวจสอบรูปร่าง -
print("\nShape of Array...\n",arr.shape) ในการรับผลิตภัณฑ์ Outer ของอาร์เรย์และสเกลาร์ ให้ใช้เมธอด numpy.outer() ใน Python -
print("\nResult (Outer Product)...\n",np.outer(arr, val)) ในการรับผลิตภัณฑ์ Inner ของอาร์เรย์และสเกลาร์ ให้ใช้วิธี numpy.inner() ใน Python -
print("\nResult (Inner Product)...\n",np.inner(arr, val))
ตัวอย่าง
import numpy as np
# Create an array using numpy.eye(). This method returns a 2-D array with ones on the diagonal and zeros elsewhere.
arr = np.eye(5)
# The val is the scalar
val = 2
# Display the array
print("Array...\n",arr)
# Check the datatype
print("\nDatatype of Array...\n",arr.dtype)
# Check the Dimension
print("\nDimensions of Array...\n",arr.ndim)
# Check the Shape
print("\nShape of Array...\n",arr.shape)
# To get the Inner product of an array and a scalar, use the numpy.inner() method in Python
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.]]