Vector inner products
Compute a vector's similarity or weighted sum from matching one-dimensional numeric arrays.
NumPy · Function
Computes dot products for scalar, vector, matrix, and higher-dimensional array combinations. Its exact operation depends on operand dimensionality, and an optional output array can receive the result.
numpy.dot combines arrays according to their dimensions: it computes an unconjugated inner product for two 1-D inputs, matrix multiplication for two 2-D inputs, scalar multiplication when either input is 0-D, and generalized sum-products for higher-dimensional combinations. The optional out array receives the result and is returned.
numpy.dot(a, b, out=None)| Name | Type | Description |
|---|---|---|
a | array_like | First array operand. |
b | array_like | Second array operand. |
out | ndarray, optional | An optional output array in which to place the result. It must have the correct shape, dtype, and be C-contiguous. |
Scalar or ndarray; if out is supplied, that output array is returned.
numpy.dot computes a dot product between two array-like operands.out is both the destination for the result and the returned object.matmul or the @ operator.import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b)) # 32Computes the inner product of two one-dimensional arrays.
Compute a vector's similarity or weighted sum from matching one-dimensional numeric arrays.
Apply a matrix multiplication operation when using the legacy dot API or dimension-specific behavior is required.
Reduce the final axis of a tensor against a vector using a sum-product.
numpy.dot compute for 1-D and 2-D inputs?For two 1-D arrays, numpy.dot returns their inner product without complex conjugation. For two 2-D arrays, it performs matrix multiplication.
Follow-up: How does it behave for an N-D array and a 1-D array? It sums products over the N-D array's last axis and the 1-D array.
If either operand is 0-D, the operation is equivalent to multiplication. For two scalars or two 1-D arrays, the result is a scalar; otherwise it is an array.
Follow-up: What should replace dot for scalar multiplication? Use numpy.multiply or the * operator.
out parameter?The signature is numpy.dot(a, b, out=None). a and b are the operands, while optional out specifies an output array that receives the result and is returned.
Follow-up: When should out be used? Use it when a suitably shaped output array is available and explicit output placement is useful.
numpy.matmulnumpy.multiplynumpy.innernumpy.outernumpy.vdot