A Gentle Introduction to Vectors for Machine Learning

Vectors are a foundational element of linear algebra.

Vectors are used throughout the field of machine learning in the description of algorithms and processes such as the target variable (y) when training an algorithm.

In this tutorial, you will discover linear algebra vectors for machine learning.

After completing this tutorial, you will know:

  • What a vector is and how to define one in Python with NumPy.
  • How to perform vector arithmetic such as addition, subtraction, multiplication and division.
  • How to perform additional operations such as dot product and multiplication with a scalar.

Kick-start your project with my new book Linear Algebra for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

A Gentle Introduction to Vectors for Machine Learning

A Gentle Introduction to Vectors for Machine Learning
Photo by Lachlan Donald, some rights reserved.

Tutorial Overview

This tutorial is divided into 5 parts; they are:

  1. What is a Vector?
  2. Defining a Vector
  3. Vector Arithmetic
  4. Vector Dot Product
  5. Vector-Scalar Multiplication

Need help with Linear Algebra for Machine Learning?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

What is a Vector?

A vector is a tuple of one or more values called scalars.

Vectors are built from components, which are ordinary numbers. You can think of a vector as a list of numbers, and vector algebra as operations performed on the numbers in the list.

— Page 69, No Bullshit Guide To Linear Algebra, 2017

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

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

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

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.

The example defines a vector with 3 elements.

Running the example prints the defined vector.

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.

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:

Or, put another way:

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

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.

Vector Subtraction

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

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.

Or, put another way:

The NumPy arrays can be directly subtracted in Python.

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.

Vector Multiplication

Two vectors of equal length can be multiplied together.

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

or

Or, put another way:

We can perform this operation directly in NumPy.

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.

Vector Division

Two vectors of equal length can be divided.

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

or

Or, put another way:

We can perform this operation directly in NumPy.

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.

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.

— Page 110, No Bullshit Guide To Linear Algebra, 2017

The operation can be used in machine learning to calculate the weighted sum of a vector.

The dot product is calculated as follows:

or

We can calculate the dot product between two vectors in Python using the dot() function on a NumPy array. It can also be calculated using the newer @ operator, since Python version 3.5. The example below demonstrates both methods.

The example defines two vectors with three elements each, then calculates the dot product.

Running the example first prints the two parent vectors, then the scalar dot product.

Vector-Scalar Multiplication

A vector can be multiplied by a scalar, in effect scaling the magnitude of the vector.

To keep notation simple, we will use lowercase “s” to represent the scalar value.

or

The multiplication is performed on each element of the vector to result in a new scaled vector of the same length.

Or, put another way:

We can perform this operation directly with the NumPy array.

The example first defines the vector and the scalar then multiplies the vector by the scalar.

Running the example first prints the parent vector, then scalar, and then the result of multiplying the two together.

Similarly, vector-scalar addition, subtraction, and division can be performed in the same way.

Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

  • Create 5 examples using each operation using your own data.
  • Implement each vector operation manually for vectors defined as lists.
  • Search machine learning papers and find 1 example of each operation being used.

If you explore any of these extensions, I’d love to know.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Books

API

Articles

Summary

In this tutorial, you discovered linear algebra vectors for machine learning.

Specifically, you learned:

  • What a vector is and how to define one in Python with NumPy.
  • How to perform vector arithmetic such as addition, subtraction, multiplication and division.
  • How to perform additional operations such as dot product and multiplication with a scalar.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Get a Handle on Linear Algebra for Machine Learning!

Linear Algebra for Machine Learning

Develop a working understand of linear algebra

...by writing lines of code in python

Discover how in my new Ebook:
Linear Algebra for Machine Learning

It provides self-study tutorials on topics like:
Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more...

Finally Understand the Mathematics of Data

Skip the Academics. Just Results.

See What's Inside

33 Responses to A Gentle Introduction to Vectors for Machine Learning

  1. Avatar
    Edward June 6, 2019 at 4:05 pm #

    So how do you determine a vector to help in classification?

    • Avatar
      Jason Brownlee June 7, 2019 at 7:50 am #

      What do you mean exactly Edward?

      • Avatar
        Edward June 7, 2019 at 8:51 pm #

        When you have a feature vector and asked to determine the vector, what does that mean?

        • Avatar
          Jason Brownlee June 8, 2019 at 6:53 am #

          If you have a feature vector, it can be classified with a model.

          A feature vector is just a row where each value is measurement for a different feature or column.

          Does that help?

  2. Avatar
    Ahmed Ramadan June 23, 2019 at 8:23 pm #

    what is vector addition mean in machine learning?
    I have two vector contain features, can I use vector add to preserve two features into single vector?

    • Avatar
      Jason Brownlee June 24, 2019 at 6:24 am #

      Adding two vectors together.

      Typically we do not add features together unless it has a specific meaning in the domain, e.g. both are coordinates in some larger n-dimensional space.

      We can explore an embedding using vector arithmetic or a GAN latent space.

  3. Avatar
    Vania Todorova July 12, 2019 at 1:47 am #

    Have you worked with vectors for data for the SageMaker? my data is in numpy arrays but the error msg i get is labels must be a Vector..

  4. Avatar
    Ricardo Lizano G August 8, 2019 at 11:02 am #

    Very clear, thanks!

  5. Avatar
    Aminzai August 9, 2019 at 12:51 am #

    thanks
    Jason Brownlee great explaination.

  6. Avatar
    mohara January 30, 2020 at 7:37 am #

    hi, as far as I know for text classification we need some features and it is up to us to vectorized each sentences based on the specific teacher yes??
    I mean we should write suitable program to convert each sentence as vector based on our feature yes?

    • Avatar
      mohara January 30, 2020 at 7:44 am #

      ##i corrected my question sir
      hi, as far as I know for text classification we need some features and it is up to us to vectorized each sentences based on the specific feature yes??
      I mean we should write suitable program to convert each sentence as vector based on our feature yes?
      for feature 1 we should write a program to represent our sentences as a vector while for feature 2 we should consider another pieces of code to represent our sentences as a vector yea?

    • Avatar
      Jason Brownlee January 30, 2020 at 2:13 pm #

      You can use a bag of words model:
      https://machinelearningmastery.com/gentle-introduction-bag-words-model/

  7. Avatar
    Saurabh Singh April 17, 2020 at 3:39 am #

    I had been looking for similar tutorials for a long time and now I have found. Thank you sincerely.

  8. Avatar
    Faiqa July 2, 2020 at 10:49 pm #

    Just like the way we have a feature vector, can we also possibly have a response vector in here? if yes then what it would consist of sir?

    • Avatar
      Jason Brownlee July 3, 2020 at 6:16 am #

      Yes.

      You can define the composition of the feature vectors and target vectors for your project.

  9. Avatar
    Jim October 4, 2020 at 10:04 am #

    I know squat about Liner Algebra and this made total sense to me. Thanks for a simple, clear and concise explanation.

  10. Avatar
    firoza October 19, 2020 at 2:53 am #

    Explanation was awesome!! its easy to understand.

  11. Avatar
    Tony December 19, 2020 at 5:12 am #

    Great explanation. Thank you!

  12. Avatar
    Terry Jerry January 27, 2021 at 12:38 am #

    My code for vector arithmetic operations with lists. I know it’s not perfect.

    • Avatar
      Terry Jerry January 27, 2021 at 12:40 am #

      I forgot to add:

      after import modules you should add line:

      Vector = List[float]

    • Avatar
      Jason Brownlee January 27, 2021 at 6:09 am #

      Well done, thanks for sharing!

  13. Avatar
    Maanas February 1, 2022 at 4:47 pm #

    What is the intuition behind applying dot product on 2 vectors? For instance, why do we apply the dot product on the item vector and user vector in content-based filtering (recommender system)

  14. Avatar
    Guillermo February 16, 2022 at 11:57 pm #

    Hi.Could you explain the concept of reference vectors? I see that mentioned in Kohonen Self-Organizing maps, I cannot understand it.

Leave a Reply