Skip to main content

Vectors and Vector Arithmetic,dot product,vector norm

Vectors are a foundational element of linear algebra. Vectors are used throughout the fi eld of machine learning in the description of algorithms and processes such as the target variable (y) when training an algorithm
A vector is a tuple of one or more values called scalars.Vectors are often represented using a lowercase character such as v; for example:
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:
v =v1
      v2
      v3
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 defi ne a vector with the length of 3 and the integer values 1,2,3.
from numpy import array
# define vector
v = array([1, 2, 3])
print(v)
[1 2 3]
Vector Arithmetic
Vector Addition
Two vectors of equal length can be added together to create a new third vector
# 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)
o/p:
[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.
# vector subtraction
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)
# subtract vectors
c = a - b
print(c)
o/p:
[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.
# vector multiplication
from numpy import array
# define first vector
a = array([1, 2, 3])
print("vector a")
print(a)
# define second vector
b = array([1, 2, 3])
print("vector b")
print(b)
# multiply vectors
c = a * b
print("vector c")
print(c)
o/p:
vector a
[1 2 3]
vector b
[1 2 3]
vector c
[1 4 9]
Vector Division
Two vectors of equal length can be divided.
# 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)
o/p:
[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 decomposition,and determining orthogonality. The name dot product comes from the symbol used to denote it.
# vector dot product
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.dot(b)
print(c)
o/p:
[1 2 3]
[1 2 3]
14
Vector outer product
import numpy as np
a=np.array([1,2])
b=np.array([3,4,5])
c=np.outer(a,b)
print("vector a")
print(a)
print("vector b")
print(b)
print("vector c - outer product")
print(c)
o/p
vector a 
[1 2] 
vector b 
[3 4 5] 
vector c - outer product 
[[ 3 4 5] 
[ 6 8 10]] 
Vector Cross Product
import numpy as np
a=np.array([2,3,4])
b=np.array([5,6,7])
c=np.cross(a,b)
print("vector a")
print(a)
print("vector b")
print(b)
print("vector c - cross product -vector product")
print(c)
o/p
vector a 
[2 3 4] 
vector b 
[5 6 7] 
vector c - cross product -vector product
 [-3 6 -3]
Vector-Scalar Multiplication
A vector can be multiplied by a scalar, in e ect scaling the magnitude of the vector. To keep notation simple, we will use lowercase s to represent the scalar value.
c = s x v
# 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)
o/p:
[1 2 3]
0.5
[ 0.5 1. 1.5]
Vector Norms
Calculating the length or magnitude of vectors is often required either directly as a regularization method in machine learning, or as part of broader vector or matrix operations.The length of the vector is referred to as the vector norm or the vector's magnitude.The length of a vector is a nonnegative number that describes the extent of the vector in space, and is sometimes referred to as the vector's magnitude or the norm.The length of the vector is always a positive number, except for a vector of all zero values.
It is calculated using some measure that summarizes the distance of the vector from the origin of the vector space. For example, the origin of a vector space for a vector with 3 elements is (0; 0; 0).
Vector L1 Norm
The length of a vector can be calculated using the L1 norm, where the 1 is a superscript of the L. The notation for the L1 norm of a vector is ||v||1, where 1 is a subscript. As such, this length is sometimes called the taxicab norm or the Manhattan norm.
L1(v) = ||v||1 The L1 norm is calculated as the sum of the absolute vector values, where the absolute value of a scalar uses the notation |a1|. In effect, the norm is a calculation of the Manhattan distance
from the origin of the vector space.
||v||1 = |a1| + |a2| + |a3|
The L1 norm of a vector can be calculated in NumPy using the norm() function with a parameter to specify the norm order, in this case 1.
# vector L1 norm
from numpy import array
from numpy.linalg import norm
# define vector
a = array([1, 2, 3])
print(a)
# calculate norm
l1 = norm(a, 1)
print(l1)
o/p:
[1 2 3]
6.0
The L1 norm is often used when fitting machine learning algorithms as a regularization method, e.g. a method to keep the coefficients of the model small, and in turn, the model less complex.
Vector L2 Norm
The length of a vector can be calculated using the L2 norm, where the 2 is a superscript of the . The notation for the L2 norm of a vector is ||v||2 where 2 is a subscript.
L2(v) = ||v||2
The L2 norm calculates the distance of the vector coordinate from the origin of the vector space. As such, it is also known as the Euclidean norm as it is calculated as the Euclidean distance from the origin. The result is a positive distance value. The L2 norm is calculated as the square root of the sum of the squared vector values.
||v||2 = sqrt(a1^2+ a2^2+a3^2)

The L2 norm of a vector can be calculated in NumPy using the norm() function with default parameters.

# vector L2 norm
from numpy import array
from numpy.linalg import norm
# define vector
a = array([1, 2, 3])
print(a)
# calculate norm
l2 = norm(a)
print(l2)
o/p:
[1 2 3]
3.74165738677
Like the L1 norm, the L2 norm is often used when fitting machine learning algorithms as a regularization method, e.g. a method to keep the coefficients of the model small and, in turn, the model less complex. By far, the L2 norm is more commonly used than other vector norms in machine learning.
Vector Max Norm
The length of a vector can be calculated using the maximum norm, also called max norm. Max norm of a vector is referred to as Linf where inf is a superscript and can be represented with the in finity symbol. The notation for max norm is ||v||inf , where inf is a subscript.
Linf (v) = ||v||inf 
The max norm is calculated as returning the maximum value of the vector, hence the name.
||v||inf = max a1; a2; a3 
The max norm of a vector can be calculated in NumPy using the norm() function with the  order parameter set to inf.

# vector max norm
from math import inf
from numpy import array
from numpy.linalg import norm
# define vector
a = array([1, 2, 3])
print(a)
# calculate norm
maxnorm = norm(a, inf)
print(maxnorm)
o/p:
[1 2 3]
3.0
Max norm is also used as a regularization in machine learning, such as on neural network weights, called max norm regularization.

Comments

Popular posts from this blog

Mathematics for Machine Learning- CST 284 - KTU Minor Notes - Dr Binu V P

  Introduction About Me Syllabus Course Outcomes and Model Question Paper Question Paper July 2021 and evaluation scheme Question Paper June 2022 and evaluation scheme Overview of Machine Learning What is Machine Learning (video) Learn the Seven Steps in Machine Learning (video) Linear Algebra in Machine Learning Module I- Linear Algebra 1.Geometry of Linear Equations (video-Gilbert Strang) 2.Elimination with Matrices (video-Gilbert Strang) 3.Solving System of equations using Gauss Elimination Method 4.Row Echelon form and Reduced Row Echelon Form -Python Code 5.Solving system of equations Python code 6. Practice problems Gauss Elimination ( contact) 7.Finding Inverse using Gauss Jordan Elimination  (video) 8.Finding Inverse using Gauss Jordan Elimination-Python code Vectors in Machine Learning- Basics 9.Vector spaces and sub spaces 10.Linear Independence 11.Linear Independence, Basis and Dimension (video) 12.Generating set basis and span 13.Rank of a Matrix 14.Linear Mapping and Matri

1.1 Solving system of equations using Gauss Elimination Method

Elementary Transformations Key to solving a system of linear equations are elementary transformations that keep the solution set the same, but that transform the equation system into a simpler form: Exchange of two equations (rows in the matrix representing the system of equations) Multiplication of an equation (row) with a constant  Addition of two equations (rows) Add a scalar multiple of one row to the other. Row Echelon Form A matrix is in row-echelon form if All rows that contain only zeros are at the bottom of the matrix; correspondingly,all rows that contain at least one nonzero element are on top of rows that contain only zeros. Looking at nonzero rows only, the first nonzero number from the left pivot (also called the pivot or the leading coefficient) is always strictly to the right of the  pivot of the row above it. The row-echelon form is where the leading (first non-zero) entry of each row has only zeroes below it. These leading entries are called pivots Example: $\begin

4.3 Sum Rule, Product Rule, and Bayes’ Theorem

 We think of probability theory as an extension to logical reasoning Probabilistic modeling  provides a principled foundation for designing machine learning methods. Once we have defined probability distributions corresponding to the uncertainties of the data and our problem, it turns out that there are only two fundamental rules, the sum rule and the product rule. Let $p(x,y)$ is the joint distribution of the two random variables $x, y$. The distributions $p(x)$ and $p(y)$ are the corresponding marginal distributions, and $p(y |x)$ is the conditional distribution of $y$ given $x$. Sum Rule The addition rule states the probability of two events is the sum of the probability that either will happen minus the probability that both will happen. The addition rule is: $P(A∪B)=P(A)+P(B)−P(A∩B)$ Suppose $A$ and $B$ are disjoint, their intersection is empty. Then the probability of their intersection is zero. In symbols:  $P(A∩B)=0$  The addition law then simplifies to: $P(A∪B)=P(A)+P(B)$  wh