I would like to welcome everyone to the first installment of what we are calling **Snake_Bytes** just a little nibble of some #Python snippets. Various developers from @PokitDokDev will be contributing on a weekly basis just to give you added venom in your coding skills. So let's begin with the first one:

I am continually asked how one gets started with #data-science other than putting a hashtag in a Twitter stream or getting a recommendation on it via LinkedIn. Almost all operations within machine learning start with the **Dot Product or Scalar Product**.

In vector calculus the dot product of two vectors in **R** is defined to be a number, for example if you have:

two vectors **A** = [*A*_{1}, *A*_{2}, ..., *A*_{n}] and **B** = [*B*_{1}, *B*_{2}, ..., *B*_{n}] is defined as:

where Σ denotes summation notation and *n* is the dimension of the vector space. For instance, in three-dimensional space, the dot product of vectors [1, 3, −5] and [4, −2, −1] is:

There is also a **Geometric **whereas in Euclidean space, a Euclidean vector is a geometrical object that possesses both a magnitude and a direction. A vector can be pictured as an arrow. Its magnitude is its length, and its direction is the direction that the arrow points. The magnitude of a vector **A** is denoted by . The dot product of two Euclidean vectors **A **and **B** is defined by^{[}

where θ is the angle between **A** and **B**.

In particular, if **A** and **B** are orthogonal, then the angle between them is 90° and

At the other extreme, if they are codirectional, then the angle between them is 0° and

This implies that the dot product of a vector **A** by itself is

which gives

which is the formula for the Euclidean length of the vector.

So what does this actually do for me? I think its time to code something already!

There are several different ways to approach the coding. Actually it is really simple:

1 2 |
def dot_product (u,v): return sum(u[i] * v[i] for i in range(len(u))]) |

or

1 2 |
def dot_product(u,v): return sum ([a*b) for (a,b) in zip(u,v)]) |

The advantage in the latter using the **zip()** is that it does the lifting for you and makes an iterator that aggregates elements from each of the iterables.

The dot product can be used in several applications ranging from audio, text, and similarity to graphics rendering. In fact, many machine learning algorithms can be expressed entirely in terms of dot products.

So there is the first Snake_Byte.

Hopefully it wasn't too painful.

- Why PokitDok Joined Hyperledger - June 26, 2018
- Full Consensus and Distributed Ledger Hardware Acceleration - May 10, 2017
- Blockchain Virtual Machines with Consensus in Silicon - October 11, 2016
- Snake_Bytes #3: Statistics! - September 23, 2016
- DokChain Technical Deep Dive - Part One - September 13, 2016
- Welcome To Snake_Bytes - September 9, 2016
- The DokChain is Now - July 13, 2016
- PokitDok Now in the Microsoft Azure Marketplace - June 8, 2016
- ClearingHouses Are Oxymorons - May 4, 2016
- Social Capital Within Social Health Networks (part 2) - August 22, 2012

## 4 comments

Skip to comment form

I need some help wrapping my head around this matrix calculus business. Adding two vectors makes sense to me, especially when I consider the operation in a two- or three-dimensional world. Cool. But the dot product. Where's that get me? So, consider this explanation I found on the google.

Let's start simple, and treat 3 x 4 as a dot product:

(3, 0) * (4,0)

The number 3 is "directional growth" in a single dimension (the x-axis, let's say), and 4 is "directional growth" in that same direction. 3 x 4 = 12 means we get 12x growth in a single dimension. Ok.

Now, suppose 3 and 4 refer to different dimension. Let's say 3 means "triple your bananas" (x-axis) and 4 means "quadruple your oranges" (y-axis). Now they're not the same type of number: what happens when apply growth (use the dot product) in our "bananas, oranges" universe?

(3,0) means "Triple your bananas, destroy your oranges"

(0,4) means "Destroy your bananas, quadruple your oranges"

Applying (0,4) to (3,0) means "Destroy your banana growth, quadruple your orange growth". But (3, 0) had no orange growth to begin with, so the net result is 0 ("Destroy all your fruit, buddy").

(3, 0) * (0, 4) = 0

Now I'm really lost. Help?

Dear Ms. Dalton:

We appreciate your interest in our blog. The dot product allows you to functionally traverse vector spaces when you want to find the similarity between say audio samples or text it also useful in backface culling, lighting and collision detection for 3-D graphics. To be honest I did not know it allowed you to destruct fruit. I will have to add that to the bag of algorithms.

oh.

The key to understanding the geometric interpretation of an inner product, Ms. Dalton, is to think of vectors as defining the locations of *points* in space, rather than arrows. This allows us to more intuitively interpret the cosine of the angle between any two points (or objects), A and B, as a measure of the "similarity" between those objects. This "cosine similarity" can be easily computed as "||A|| dot ||B||", per Ted's post.

We will be exploring in subsequent posts how inner products and other mathematical tools allow us to make and use models in very high dimensional vector spaces.