Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Dot Product



What is the Dot Product?

The dot product, also known as the scalar product, is a mathematical operation that takes two equal-length sequences of numbers (usually vectors) and returns a single number.

In the context of matrices, the dot product is used to perform matrix multiplication, which is a fundamental operation in many areas of mathematics, physics, and engineering.

The dot product of two vectors a and b is defined as −

a . b = a1b1 + a2b2 + ... + anbn

Where, ai and bi are the components of vectors a and b respectively, and n is the number of dimensions.

Matrix Multiplication Using Dot Product

In matrix multiplication, the dot product is used to multiply the rows of the first matrix by the columns of the second matrix. This produces a new matrix where each element is the dot product of the corresponding row and column vectors.

Consider two matrices A and B

A = [[a11, a12],
         [a21, a22]]

B = [[b11, b12],
         [b21, b22]]

The product C = A . B is −

C = [[a11b11 + a12b21, a11b12 + a12b22],
         [a21b11 + a22b21, a21b12 + a22b22]]

Using NumPy for Dot Product

NumPy provides a convenient way to perform dot products using the dot() function. This function can be used for both vector dot products and matrix multiplication.

Example

In the following example, the dot product is calculated as (1 * 4) + (2 * 5) + (3 * 6) = 32 −

import numpy as np

# Define two vectors
vector_1 = np.array([1, 2, 3])
vector_2 = np.array([4, 5, 6])

# Compute dot product
dot_product = np.dot(vector_1, vector_2)
print(dot_product)

Following is the output obtained −

32

Matrix Dot Product

To compute the dot product of two matrices, we use the same dot() function.

Example

In this example, the dot product of the two matrices is computed as −

[[1*5 + 2*7, 1*6 + 2*8],
 [3*5 + 4*7, 3*6 + 4*8]]
import numpy as np

# Define two matrices
matrix_1 = np.array([[1, 2], [3, 4]])
matrix_2 = np.array([[5, 6], [7, 8]])

# Compute dot product
matrix_product = np.dot(matrix_1, matrix_2)
print(matrix_product)

Following is the output obtained −

[[19 22]
 [43 50]]

Dot Product with Higher Dimensional Arrays

NumPy's dot() function can also handle higher-dimensional arrays. In this case, the function computes the dot product over the last axis of the first array and the second-to-last axis of the second array.

Example

In this example, the dot product is computed for each pair of sub-arrays, resulting in a new 3-dimensional array −

import numpy as np

# Define two 3-dimensional arrays
array_1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
array_2 = np.array([[[1, 0], [0, 1]], [[1, 1], [1, 0]]])

# Compute dot product
array_product = np.dot(array_1, array_2)
print(array_product)

Following is the output obtained −

[[[[ 1  2]
   [ 3  1]]

  [[ 3  4]
   [ 7  3]]]


 [[[ 5  6]
   [11  5]]

  [[ 7  8]
   [15  7]]]]

Using the @ Operator for Dot Product

In Python 3.5 and later, the @ operator can be used as an alternative to the dot() function for matrix multiplication. This makes the code more readable and concise.

Example

The result of the following example is the same as using the dot() function, but the syntax is more cleaner −

import numpy as np

# Define two matrices
matrix_1 = np.array([[1, 2], [3, 4]])
matrix_2 = np.array([[5, 6], [7, 8]])

# Using @ operator for matrix multiplication
matrix_product = matrix_1 @ matrix_2
print(matrix_product)

Following is the output obtained −

[[19 22]
 [43 50]]

Applications of Dot Product

The dot product is a fundamental operation with a lot of applications in various fields −

  • Machine Learning: Dot products are used in calculating the similarity between vectors, which is crucial in algorithms like support vector machines and neural networks.
  • Physics: Dot products are used to compute work done by a force and to project vectors in different directions.
  • Computer Graphics: Dot products are used in shading calculations and to determine angles between surfaces and light sources.
  • Linear Algebra: Dot products are foundational in solving systems of linear equations and in transformations.

Example: Using Dot Product in Machine Learning

In machine learning, dot products are often used to compute the weights and biases in neural networks.

In this example, the dot product computes the weighted sum of the input features, which is an important step in the computation of neural network outputs −

import numpy as np

# Define input vector (features)
input_vector = np.array([0.5, 1.5, -1.0])

# Define weight vector (weights)
weights = np.array([2.0, -1.0, 0.5])

# Compute the weighted sum (dot product)
output = np.dot(input_vector, weights)
print(output)

Following is the output obtained −

-1.0
Advertisements