Processing math: 100%
Skip to main content

2.9 Gram-Schmidt Orthogolization

Gram-Schdmit method allows us to constructively transform any basis (b1,b2,....,bn) of an n-dimensional vector space V into an orthogonal/orthonormal basis (u1,u2,....un) of V. The method interactively constructs an orthogonal basis as follows

u1=b1

u2=b2u1.uT1b2uT1.u1

..

uk=bk projection of uk onto the span of (u1,u2,...uk1)

The k th basis vector bk is projected onto the subspace spanned by the first k1 constructed orthogonal vectors u1,u2,......uk1. This projection is then subtracted from bk and yields a vector uk that is orthogonal to the (k1) dimensional subspace spanned by u1,u2,......uk1.Repeating this procedure for all n basis vectors (b1,b2,....,bn) yields an orthogonal basis (u1,u2,....un) of V. If we normalize uk, we obtain a ortho normal basis where ||uk||=1 for k=1n

Consider a simple example



import numpy as np
b1=np.array([2,0])
b2=np.array([1,1])
u1=b1
u2=b2-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b2))
print("b1")
print(b1)
print("b2")
print(b2)
print("orthogonal vectors")
print("u1")
print(u1)
print("u2")
print(u2)
print("dot product of u1 and u2")
print(u1.dot(u2))

o/p
b1 
[2 0] 
b2 
[1 1] 
orthogonal vectors 
u1 
[2 0]
 u2 
[0. 1.]
dot product of u1 and u2 
0.0

Orthogonalization in three dimension
import numpy as np
np.set_printoptions(precision=4)
b1=np.array([1,2,2])
b2=np.array([-1,0,2])
b3=np.array([0,0,1])
u1=b1
u2=b2-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b2))
u3=b3-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b3))-((np.outer(u2,u2)/np.dot(u2,u2)).dot(b3))
print("b1")
print(b1)
print("b2")
print(b2)
print("b3")
print(b3)
print("orthogonal vectors")
print("u1")
print(u1)
print("u2")
print(u2)
print("u3")
print(u3)
print("length of u1 u2 u3")
lu1=np.sqrt(u1.dot(u1))
lu2=np.sqrt(u2.dot(u2))
lu3=np.sqrt(u3.dot(u3))
print(lu1,lu2,lu3)
print("normalized vectors u1 u2 u3")
u1=u1/lu1
u2=u2/lu2
u3=u3/lu3
print(u1)
print(u2)
print(u3)
np.set_printoptions(suppress=True)
print("dot product of u1 and u2")
display(round(u1.dot(u2)))
print("dot product of u1 and u3")
print(round(u1.dot(u3)))
print("dot product of u2 and u3")
display(round(u2.dot(u3)))

o/p
b1 
[1 2 2] 
b2
 [-1 0 2]
 b3 
[0 0 1] 
orthogonal vectors 
u1 
[1 2 2]
u2 
[-1.3333 -0.6667 1.3333] 
u3 
[ 0.2222 -0.2222 0.1111] 
length of u1 u2 u3 
3.0 2.0 0.3333333333333333
 normalized vectors u1 u2 u3
 [0.3333 0.6667 0.6667] 
[-0.6667 -0.3333 0.6667] 
[ 0.6667 -0.6667 0.3333] 
dot product of u1 and u2
0
dot product of u1 and u3 
dot product of u2 and u3
0
Compact code
import numpy as np
def gram_schmidt(A):
    """Orthogonalize a set of vectors stored as the columns of matrix A."""
    # Get the number of vectors.
    n = A.shape[1]
    for j in range(n):
        # To orthogonalize the vector in column j with respect to the
        # previous vectors, subtract from it its projection onto
        # each of the previous vectors.
        for k in range(j):
            A[:, j] -= np.dot(A[:, k], A[:, j]) * A[:, k]
        A[:, j] = A[:, j] / np.linalg.norm(A[:, j])
    return A

if __name__ == '__main__':
    A = np.array([[1.0, 0.0, 1.0], [1.0, 0.0, -1.0], [0.0, 3.0, 4.0]])
    print(gram_schmidt(A))

Example ( university question)
Use the Gram-Schmidt process to find an orthogonal basis for the column space of the following matrix.

[210111031111]

here
u1=b1=[2101]

u2=b2(u1uT1)||u1||2.b2
u2=[1131]16[4202210100002101][1131]
u2=[0.33331.33333.0.6667]

u3=b3(u1uT1)||u1||2.b3(u2uT2)||u2||2.b3
u3=[0.73530.94120.38240.5294]

Example ( University Question)
Use Gram-Schmidt process to find an orthonormal basis from the ordered basis B={b1,b2}, where b1=[111]  and  b2=[120]

u1=b1=[111]
u2=b2u1.uT1||u21||.b2
=[120]13[111111111][120]
=[120]13[111]

=[4/35/31/3]

length of u1=3
length of u2=423
So the normalized vectors are
u1=13.[111]
u2=142.[451]

Note that u1.u2=0

Example ( University Question)
Obtain  an orthonormal basis with respect to the standard inner space for the subspace of R3 generated by (1,0,1),(1,0,1) and (0,3,4)

u1=b1=[101]
u2=b2u1.uT1||u21||.b2
=[101]12[101000101][101]
=[101]12[000]
=[101]

u3=b3(u1uT1)||u1||2.b3(u2uT2)||u2||2.b3
u3=[030]

length of u1=2
length of u2=2
length of u3=3

So the normalized vectors are
u1=12.[101]
u2=12.[101]
u3=13.[030]

Example ( University Question)
Using the Gram-Schmidt method, construct the orthonormal basis (u1,u2,u3) to the following sequence of vectors w1=[1111],w2=[1111],w3=[0121] of R4

u1=w1=[1111]
u2=w2u1.uT1||u21||.w2
u2=[1111]14[111111111111][1111]

u2=[1111]
u3=w3(u1uT1)||u1||2.w3(u2uT2)||u2||2.w3
u3=[0121]14[111111111111][0121]14[1111111111111111][0121]u3=[0121][1/21/21/21/2][1111]
u3=[1/21/21/21/2]

The length of vectors
||u1||=2,||u2||=2,||u3||=1

So the normalized orthogonal vectors ( orthonormal vectors) are
u1=[1/21/21/21/2],u2=[1/21/21/21/2],u3=[1/21/21/21/2]

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 University Question Papers and Evaluation Scheme -Mathematics for Machine learning CST 284 KTU 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...

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(AB)=P(A)+P(B)P(AB) Suppose A and B are disjoint, their intersection is empty. Then the probability of their intersection is zero. In symbols:  P(AB)=0  The addition law then simplifies to: $P(...

5.1 Optimization using Gradient Descent

Since machine learning algorithms are implemented on a computer, the mathematical formulations are expressed as numerical optimization methods.Training a machine learning model often boils down to finding a good set of parameters. The notion of “good” is determined by the objective function or the probabilistic model. Given an objective function, finding the best value is done using optimization algorithms. There are two main branches of continuous optimization constrained and unconstrained. By convention, most objective functions in machine learning are intended to be minimized, that is, the best value is the minimum value. Intuitively finding the best value is like finding the valleys of the objective function, and the gradients point us uphill. The idea is to move downhill (opposite to the gradient) and hope to find the deepest point. For unconstrained optimization, this is the only concept we need,but there are several design choices. For constrained optimization, we need to intr...