Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
The inner product (also known as dot product) of two arrays is a fundamental operation in linear algebra. In NumPy, this can be calculated with the dot()
function or the @
operator (from Python 3.5 onwards).
The inner product of two arrays in NumPy refers to the sum of products of their corresponding elements if the arrays are 1D. For higher dimensions, it equates to matrix multiplication.
If you haven't installed NumPy, you can do so with:
pip install numpy
Start by importing NumPy:
import numpy as np
dot()
function:For 1D arrays, the inner product is the sum of products of the corresponding entries:
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) result = np.dot(a, b) print(result) # Output: 32 (1*4 + 2*5 + 3*6)
@
operator:Python 3.5 introduced the @
operator as a convenient infix operator for matrix multiplication:
result = a @ b print(result) # Output: 32
For 2D arrays, the inner product becomes matrix multiplication:
A = np.array([[1, 2], [3, 4]]) B = np.array([[2, 0], [1, 3]]) result = np.dot(A, B) print(result) # Output: # [[ 4 6] # [10 12]]
Using the @
operator:
result = A @ B print(result) # Output: # [[ 4 6] # [10 12]]
Note: Ensure that the number of columns in the first matrix (A) matches the number of rows in the second matrix (B) for matrix multiplication to be valid.
The dot()
function can also handle arrays with more than two dimensions. The product is defined by removing the last dimension of the first array and the second-to-last of the second array, and the sums are computed over these removed dimensions.
numpy.inner()
: For 1D arrays, it is the same as dot product. For higher-dimensional arrays, it sums products over the last dimension.numpy.matmul()
: It is another function to perform matrix multiplication, equivalent to the @
operator.Calculating the inner product or dot product in NumPy is straightforward using the dot()
function or the @
operator. The function is versatile, supporting both 1D and multi-dimensional arrays. Remember to ensure that the arrays' shapes are compatible for the specific operation you're trying to perform.
Description: Computing the inner product of arrays in NumPy is often done using the numpy.inner
function.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Compute inner product inner_product_result = np.inner(array1, array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Inner Product:") print(inner_product_result)
Description: Calculating the dot product in NumPy is commonly achieved using the numpy.dot
function.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Calculate dot product dot_product_result = np.dot(array1, array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Dot Product:") print(dot_product_result)
Description: The numpy.dot
function in Python NumPy is versatile and can be used for various types of dot products.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Dot product using numpy.dot dot_product_result = np.dot(array1, array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Dot Product:") print(dot_product_result)
Description: In NumPy, the terms "inner product" and "dot product" are often used interchangeably. The functions numpy.inner
and numpy.dot
can both be used to calculate these products.
Description: NumPy array multiplication, followed by summing the elements, can be used to calculate the inner product.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Calculate inner product using array multiplication inner_product_result = np.sum(array1 * array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Inner Product:") print(inner_product_result)
Description: Using dedicated functions like numpy.inner
or numpy.dot
is an efficient way to compute the inner product in NumPy.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Efficiently compute inner product inner_product_result = np.inner(array1, array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Inner Product:") print(inner_product_result)
Description: Matrix multiplication in NumPy is often performed using the numpy.matmul
function or the @
operator.
Code:
import numpy as np # Create two 2D NumPy arrays (matrices) matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # Matrix multiplication matrix_product_result = np.matmul(matrix1, matrix2) print("Matrix 1:") print(matrix1) print("Matrix 2:") print(matrix2) print("Matrix Product:") print(matrix_product_result)
Description: NumPy's vectorized operations allow for efficient and concise inner product calculations.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Vectorized inner product calculation inner_product_result = np.sum(array1 * array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Inner Product (Vectorized):") print(inner_product_result)
Description: The numpy.einsum
function allows for advanced inner product operations using Einstein summation notation.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Advanced inner product using einsum inner_product_result = np.einsum('i,i->', array1, array2) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Inner Product (Advanced):") print(inner_product_result)