배열과 스칼라의 내적을 얻으려면 Python에서 numpy.inner() 메서드를 사용하십시오. 1차원 배열에 대한 벡터의 보통 내적이며, 더 높은 차원에서 마지막 축에 대한 합입니다. 매개변수는 1과 b, 두 벡터입니다. 및 b가 비 스칼라이면 마지막 차원이 일치해야 합니다.
단계
먼저 필요한 라이브러리를 가져옵니다-
import numpy as np
numpy.eye()를 사용하여 배열을 만듭니다. 이 메서드는 대각선에 1이 있고 다른 곳에 0이 있는 2차원 배열을 반환합니다. -
arr = np.eye(5)
val은 스칼라입니다 -
val = 2
데이터 유형 확인 -
print("\nDatatype of Array...\n",arr.dtype)
치수 확인 -
print("\nDimensions of Array...\n",arr.ndim) 모양 확인 -
print("\nShape of Array...\n",arr.shape) 배열과 스칼라의 외부 곱을 얻으려면 Python에서 numpy.outer() 메서드를 사용하십시오 -
print("\nResult (Outer Product)...\n",np.outer(arr, val)) 배열과 스칼라의 내적을 얻으려면 Python에서 numpy.inner() 메서드를 사용하십시오 -
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.]]