Vectors are often represented using a lowercase character such as “v”; for example:

1

v = (v1, v2, v3)

Where v1, v2, v3 are scalar values, often real values.

Vectors are also shown using a vertical representation or a column; for example:

1

2

3

v1

v = ( v2 )

v3

It is common to represent the target variable as a vector with the lowercase “y” when describing the training of a machine learning algorithm.

It is common to introduce vectors using a geometric analogy, where a vector represents a point or coordinate in an n-dimensional space, where n is the number of dimensions, such as 2.

The vector can also be thought of as a line from the origin of the vector space with a direction and a magnitude.

These analogies are good as a starting point, but should not be held too tightly as we often consider very high dimensional vectors in machine learning. I find the vector-as-coordinate the most compelling analogy in machine learning.

Now that we know what a vector is, let’s look at how to define a vector in Python.

Defining a Vector

We can represent a vector in Python as a NumPy array.

A NumPy array can be created from a list of numbers. For example, below we define a vector with the length of 3 and the integer values 1, 2 and 3.

1

2

3

4

# create a vector

from numpy import array

v=array([1,2,3])

print(v)

The example defines a vector with 3 elements.

Running the example prints the defined vector.

1

[1 2 3]

Vector Arithmetic

In this section will demonstrate simple vector-vector arithmetic, where all operations are performed element-wise between two vectors of equal length to result in a new vector with the same length

Vector Addition

Two vectors of equal length can be added together to create a new third vector.

1

c = a + b

The new vector has the same length as the other two vectors. Each element of the new vector is calculated as the addition of the elements of the other vectors at the same index; for example:

1

a + b = (a1 + b1, a2 + b2, a3 + b3)

Or, put another way:

1

2

3

c[0] = a[0] + b[0]

c[1] = a[1] + b[1]

c[2] = a[2] + b[2]

We can add vectors directly in Python by adding NumPy arrays.

1

2

3

4

5

6

7

8

# add vectors

from numpy import array

a=array([1,2,3])

print(a)

b=array([1,2,3])

print(b)

c=a+b

print(c)

The example defines two vectors with three elements each, then adds them together.

Running the example first prints the two parent vectors then prints a new vector that is the addition of the two vectors.

1

2

3

4

5

[1 2 3]

[1 2 3]

[2 4 6]

Vector Subtraction

One vector can be subtracted from another vector of equal length to create a new third vector.

1

c = a - b

As with addition, the new vector has the same length as the parent vectors and each element of the new vector is calculated as the subtraction of the elements at the same indices.

1

a - b = (a1 - b1, a2 - b2, a3 - b3)

Or, put another way:

1

2

3

c[0] = a[0] - b[0]

c[1] = a[1] - b[1]

c[2] = a[2] - b[2]

The NumPy arrays can be directly subtracted in Python.

1

2

3

4

5

6

7

8

# subtract vectors

from numpy import array

a=array([1,2,3])

print(a)

b=array([0.5,0.5,0.5])

print(b)

c=a-b

print(c)

The example defines two vectors with three elements each, then subtracts the first from the second.

Running the example first prints the two parent vectors then prints the new vector that is the first minus the second.

1

2

3

4

5

[1 2 3]

[ 0.5 0.5 0.5]

[ 0.5 1.5 2.5]

Vector Multiplication

Two vectors of equal length can be multiplied together.

1

c = a * b

As with addition and subtraction, this operation is performed element-wise to result in a new vector of the same length.

1

a * b = (a1 * b1, a2 * b2, a3 * b3)

or

1

ab = (a1b1, a2b2, a3b3)

Or, put another way:

1

2

3

c[0] = a[0] * b[0]

c[1] = a[1] * b[1]

c[2] = a[2] * b[2]

We can perform this operation directly in NumPy.

1

2

3

4

5

6

7

8

# multiply vectors

from numpy import array

a=array([1,2,3])

print(a)

b=array([1,2,3])

print(b)

c=a *b

print(c)

The example defines two vectors with three elements each, then multiplies the vectors together.

Running the example first prints the two parent vectors, then the new vector is printed.

1

2

3

4

5

[1 2 3]

[1 2 3]

[1 4 9]

Vector Division

Two vectors of equal length can be divided.

1

c = a / b

As with other arithmetic operations, this operation is performed element-wise to result in a new vector of the same length.

1

a / b = (a1 / b1, a2 / b2, a3 / b3)

or

1

a / b = (a1b1, a2b2, a3b3)

Or, put another way:

1

2

3

c[0] = a[0] / b[0]

c[1] = a[1] / b[1]

c[2] = a[2] / b[2]

We can perform this operation directly in NumPy.

1

2

3

4

5

6

7

8

# divide vectors

from numpy import array

a=array([1,2,3])

print(a)

b=array([1,2,3])

print(b)

c=a/b

print(c)

The example defines two vectors with three elements each, then divides the first by the second.

Running the example first prints the two parent vectors, followed by the result of the vector division.

1

2

3

4

5

[1 2 3]

[1 2 3]

[ 1. 1. 1.]

Vector Dot Product

We can calculate the sum of the multiplied elements of two vectors of the same length to give a scalar.

This is called the dot product, named because of the dot operator used when describing the operation.

The dot product is the key tool for calculating vector projections, vector decompositions, and determining orthogonality. The name dot product comes from the symbol used to denote it.