栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Vectors and Vector Arithmetic

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vectors and Vector Arithmetic

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.
1.1 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
1.2 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.

        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.

1.3 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.

# create a Vector 
from numpy import array
# define vector
v = array([1, 2, 3])
print(v)

The example defines a vector with 3 elements. Running the example prints the defined vector.

1.4 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

1.4.1 Vector Addition

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

                                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:

                                c = (a1 + b1, a2 + b2, a3 + b3)

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

# Example of vector addition
# vector addition
from numpy import array
# define first vector
a = array([1, 2, 3])
print(a)

# define second vector
b = array([1, 2, 3])
print(b)

# add vectors
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.4.2 Vector Substraction

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

                                                        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.

                                        c = (a1 − b1, a2 − b2, a3 − b3)

Or, put another way:

                                        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.

# Example of vector substraction
# vector substraction
from numpy import array
# define first vector
a = array([1, 2, 3])
print(a)
# define second vector
b = array([0.5, 0.5, 0.5])
print(b)

# substract vectors
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.4.3 Vector Multiplication

Two vectors of equal length can be multiplied together:

                        c = a x b

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

                        c = (a1 × b1, a2 × b2, a3 × b3)

or

                        c = (a1b1, a2b2, a3b3)

Or, put another way:

                        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.

# Example of vector multiplication
# vector multiplication
from numpy import array
# define first vector
a = array([1, 2, 3])
print(a)
# define second vector
b = array([1, 2, 3])
print(b)
# multiply vectors
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.

# Example of vector multiplication
# vector multiplication
from numpy import array
# define first vector
a = array([1, 2, 3])
print(a)
# define second vector
b = array([1, 2, 3])
print(b)
# multiply vectors
c = a * b
print(c)
1.4.4 Vector Division

# vector division 
from numpy import array
# define first vector
a = array([1, 2, 3])
print(a)

# define second vector
b = array([1, 2, 3])
print(b)

# divide vectors
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.5 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.

c = a · b

The operation can be used in machine learning to calculate the weighted sum of a vector. The dot product is calculated as follows:

c = (a1 × b1 + a2 × b2 + a3 × b3)

or

c = (a1b1 + a2b2 + a3b3)

We can calculate the dot product between two vectors in Python using the dot() function on a NumPy array.

# vector dot product
from numpy import array
# define first array
a = array([1, 2, 3])
print(a)
# define second vector
b = array([1, 2, 3])
print(b)
# multiply vectors
c = a.dot(b)
print(c)

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.

 1.6 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.

                        c = s × v

or

                        c = sv

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

                        c = (s × v1, s × v2, s × v3)

or , put another way:

                        c[0] = v[0] × s

                        c[1] = v[1] × s

                        c[2] = v[2] × s

We can perform this operation directly with the NumPy array.

# vector-scalar multiplication
from numpy import array
# define vector
a = array([1, 2, 3])
print(a)

# define scalar
s = 0.5
print(s)

# multiplication
c = s * a
print(c)

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.

1.7 Extensions

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

  • Create one example using each operation using your own small array data.
  • Implement each vector arithmetic operation manually for vectors defined as lists.
  • Search machine learning papers and find 1 example of each operation being used.
1.8 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.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号