Show Sidebar Hide Sidebar

Dot

How to find the dot product from two NumPy array.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Imports

This tutorial imports Plotly and Numpy.

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

Dot Product

To compute the dot product of two NumPy arrays, we can do so with np.dot().

The dot product is a mathematical binary operation which takes, in the computer science case, two arrays of equal length and returns the sum of the pairwise products of the elements in the arrays. To provide clarity to this, if $A$ is an array $[a_1, a_2, ..., a_n]$ and $B$ is an array $[b_1, b_2, ..., b_n]$ both with length $n$, then the dot product is defined as:

$$ \begin{align*} a_1 \times b_1 + a_2 \times b_2 + ... + a_n \times b_n \end{align*} $$

where $\times$ is just the multiplication operator.

In [2]:
array_a = np.array([2, 3, 0]) 
array_b = np.array([2, -1, 1]) 

np.dot(array_a, array_b)
Out[2]:
1

Visualize as Vectors

We can think of these arrays as defining a vector with each value representing the magnitiude of along the x-axis, y-axis, etc. And the dot product can also be used as a metric for orthogonality between these vectors. In general terms, the larger the number, to more parallel the two vectors are. If the dot product returns $0$, this means the vectors point orthogonally.

We can plot the vectors defined above to get a sense for what the dot product means in that example:

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter3d(
    x=[0, array_a[0]],
    y=[0, array_a[1]],
    z=[0, array_a[2]],
    mode='markers+lines',
    name='array a',
    marker=dict(
        size=[12, 1]
    )
)

trace2 = go.Scatter3d(
    x=[0, array_b[0]],
    y=[0, array_b[1]],
    z=[0, array_b[2]],
    mode='markers+lines',
    name='array b',
    marker=dict(
        size=[12, 1]
    )
)

data = [trace1, trace2]
layout = go.Layout(
    title = 'Vector Representation of Arrays'
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='numpy-min')
Out[3]:
In [2]:
help(np.dot)
Help on built-in function dot in module numpy.core.multiarray:

dot(...)
    dot(a, b, out=None)
    
    Dot product of two arrays.
    
    For 2-D arrays it is equivalent to matrix multiplication, and for 1-D
    arrays to inner product of vectors (without complex conjugation). For
    N dimensions it is a sum product over the last axis of `a` and
    the second-to-last of `b`::
    
        dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
    
    Parameters
    ----------
    a : array_like
        First argument.
    b : array_like
        Second argument.
    out : ndarray, optional
        Output argument. This must have the exact kind that would be returned
        if it was not used. In particular, it must have the right type, must be
        C-contiguous, and its dtype must be the dtype that would be returned
        for `dot(a,b)`. This is a performance feature. Therefore, if these
        conditions are not met, an exception is raised, instead of attempting
        to be flexible.
    
    Returns
    -------
    output : ndarray
        Returns the dot product of `a` and `b`.  If `a` and `b` are both
        scalars or both 1-D arrays then a scalar is returned; otherwise
        an array is returned.
        If `out` is given, then it is returned.
    
    Raises
    ------
    ValueError
        If the last dimension of `a` is not the same size as
        the second-to-last dimension of `b`.
    
    See Also
    --------
    vdot : Complex-conjugating dot product.
    tensordot : Sum products over arbitrary axes.
    einsum : Einstein summation convention.
    matmul : '@' operator as method with out parameter.
    
    Examples
    --------
    >>> np.dot(3, 4)
    12
    
    Neither argument is complex-conjugated:
    
    >>> np.dot([2j, 3j], [2j, 3j])
    (-13+0j)
    
    For 2-D arrays it is the matrix product:
    
    >>> a = [[1, 0], [0, 1]]
    >>> b = [[4, 1], [2, 2]]
    >>> np.dot(a, b)
    array([[4, 1],
           [2, 2]])
    
    >>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
    >>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
    >>> np.dot(a, b)[2,3,2,1,2,2]
    499128
    >>> sum(a[2,3,2,:] * b[1,2,:,2])
    499128

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.