
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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