https://www.coursera.org/learn/machine-learning-linear-algebra
import numpy as np
Systems of equations as lines:
Terms:
Takeaway:
Determinant: formula that returns a 0 if the matrix is singular and a number different from 0 if the matrix is non singular
Example problems, and whether singular/nonsingular:
You can solve for a bigger matrix by just considering the variables. Constants don't matter for singularity.
Linear depenence works the same
Examples of averaging rows to show linear dependence:
Quiz: Depedence and singularity
You can also apply determinants to a 3x3 matrix:
# Calculations to solve week 1 quiz
matrix = np.array([[2,1,5], [1,2,1], [2,1,3]])
print(np.linalg.det(matrix))
vec = np.array([20, 10, 15])
print(np.linalg.solve(matrix, vec))
m2 = np.array([[1,2,3], [0,2,2], [1,4,5]])
print(np.linalg.det(m2))
Takeaway: Solved solution shows that there are infinitely many solutions, a degree of freedom $x$
Takeaway: Eliminate one variable, then solve others, then propagate back to equation with first variable
Mapping solving equations to a matrix form:
Row echelon form: The general way that a matrix in row echelon form looks like is the following.
Checking for singularity after an operation can be done by checking its determinant.
Swapping rows maintain singularity:
Multiplying rows by a non-zero scalar maintains singularity:
Adding a row to another row maintains singularity:
# Calculations to solve quiz 3
matrix = np.array([[-3, 8, 1], [2, 2, -1], [-5, 6, 2]])
print(np.linalg.det(matrix))
One way to determine rank is to see how many novel equations there are for each row. Rank is the information inherent in the matrix
But there is an easier way to do this - using row echelon form.
How to find row echelon form (REF) in a 2x2 matrix:
Interestingly, the rank of a matrix is the number of ones found in the diagonal cells:
How to find it with a 3x3 matrix. Here's how they are represented in equations and via matrix:
How to determine REF for a larger matrix ("*" are any zero or non-zero number):
These correspond to the rank of the matrix:
Recall how we calculated the reduced row echolon form:
Rules of reduced echelon form:
How to get to the reduced REF for a large matrix:
Detailed example of how we get to the reduced REF:
## Quiz 3 code
arr_1 = np.array([[7,5,3],[3,2,5],[1,2,1]])
arr_2 = np.array([120, 70, 20])
# Solve system of equations
print(np.linalg.solve(arr_1, arr_2))
# Find its determinant
print(np.linalg.det(arr_1))
# What is its rank
print(np.linalg.matrix_rank(arr_1))
How to calculate vector distance (L1, L2, cosine):
Vector is multplied element-wise:
Dot product of 2 vectors:
Clearer diagram of order of operations:
The L2 norm is always the square root of the dot product between the vector and itself.
Dot product, given $u$ and $v$ is the product of their absolute values times cosine of theta:
Think of equations as dot products by uniting them as a vector:
# Done in numpy
a1 = np.array([[2, -1], [0, 2]])
a2 = np.array([[3, 1],[1, 2]])
np.matmul(a1, a2)
Note on condition that must be met to matmul
# Example:
a1 = np.identity(10)
print("Identity matrix\n", a1)
a2 = np.array(np.arange(1,11), dtype="float32")
print("a2 vector\n", a2)
print("matrix multiplication of identity matrix and a2\n", np.matmul(a1,a2))
How to find an inverse matrix:
Note that some matrix will not an inverse, like $[[1,1],[2,2]]$
# Calculate matrix inverse with numpy
from numpy.linalg import LinAlgError
a1 = np.array([[3,1],[1,2]])
print("a1\n", a1)
print("inverse:", np.linalg.inv(a1))
a2 = np.array([[5,2],[1,2]])
print("a2\n", a2)
print("inverse:", np.linalg.inv(a2))
a3 = np.array([[1,1],[2,2]])
print("a3\n", a3)
try:
np.linalg.inv(a3)
except LinAlgError:
print("Can't solve, it's a singular matrix!")
Quiz:
Answer:
Graphical representation of best fit linear classifier:
You can see how the one-hot vectors' dot product with the model vecot returns a vector of products. We can then check whether it's above/below the threshold:
We can reconfigure the threshold as the bias. THen we can just check whether product is positive/negative:
AND operator can be modeled as a perceptron (remember ML course went over this):
Represented as a perceptron:
Rank: The rank of a linear transform is equal to the resulting number of dimensions afterwards.
Examples of rank: (see diagram above):
Determinant: The resulting area of a matrix after linear transform.
Rule: If you multiply two matrices ($A_1$ & $A_2$), its resulting determinant is just the product of the two matrices.
$$ \det(A_1 \cdot A_2) = det(A_1) \cdot det(A_2) $$ $$ det(AB)=det(A)det(B) $$Rule: The product of a singular and a non-singular matrix (in any order) is singular
Rule: The inverse of a matrix's determinant is the inversed matrix's determinant
$$ det(A^{-1}) = \frac{1}{det(A)} $$Proof:
a = np.array([0.25, -0.25])
b = np.array([-0.125, 0.625])
np.linalg.det([a,b])
# Code for Quiz
# Calculate determinant of a matrix
a = np.array([[1,2,-1], [1,0,1], [0,1,0]])
print(np.linalg.det(a))
# Calculate inverse matrix
print(np.linalg.inv(a))
# Multiply inverse with identity matrix
print(np.matmul(np.linalg.inv(a), np.identity(3)))
# Is the rank of a 3x3 identity matrix singular or non-singular
print(np.linalg.matrix_rank(np.identity(3)))
# Multiply matrix W with vector b
W = np.array([[1,2,-1],[1,0,1],[0,1,0]])
b = np.array([5,-2,0])
print(np.dot(W, b))
# Dot product of vector z1 and z2
z1 = np.array([3,1,7])
z2 = np.array([2,2,0])
print(np.dot(z1, z2))
# Multiply two matrices a and b
a = np.array([[5,2,3],[-1,-3,2],[0,1,-1]])
b = np.array([[1,0,-4],[2,1,0],[8,-1,0]])
print(np.matmul(a,b))
# Calculate the determinant of the inverse of above matrix
print(np.linalg.det(np.linalg.inv(np.matmul(a,b))))
Bases are two vectors coming from the origins.
What's not a basis?
The span of a set of vectors is the set of points that can be reached by walking in the direction of these vectors in any combination.
A basis is a minimal spanning set
The number of elements in the basis is the dimensions
Example of a "change of basis":
From ChatGPT:
An eigenbasis is a set of linearly independent eigenvectors of linear transformation or a square matrix
- Eigenvector: An eigenvector of a linear transformation or a matrix is a non-zero vector that, when transformed by the linear transformation or multiplied by the matrix, remains proportional to itself, possibly with a scaling factor known as the eigenvalue.
- Eigenvalue: An eigenvalue associated with an eigenvector represents the scaling factor by which the eigenvector is stretched or compressed during the transformation. It's a scalar value.
References:
My notes:
Given:
Matrix-mult with $A\vec{v}$ is equal to scalar mult $\lambda \vec{v}$. To find eigenvalue/vector, we solve for $\lambda$ and $\vec{v}$:
$$ \begin{align*} A\vec{v} &= \lambda \vec{v} \\ \text{ (Matrix-vector multiplication) } &= \text{ (Scalar multiplication)} \end{align*} $$Step 1 - $\lambda$ is actually an identity matrix, so we can use $I$ as the identity matrix of 1
$$ A\vec{v} = (\lambda I) \vec{v} $$Step 2 - Move all to one side to solve for zero, then factor out the $\vec{v}$
$$ \begin{align*} A\vec{v} - (\lambda I) \vec{v} &= \vec{0} \\ (A - \lambda I)\vec{v} &= \vec{0} \end{align*} $$Step 3 - Find $\lambda$ so that the determinant of $A - \lambda I$ is zero.
$$ det(A-\lambda I) = 0 $$Example: Given a matrix of coordinates $\begin{bmatrix} 3 & 1 \\ 0 & 2 \end{bmatrix}$:
A = np.array([[3,1],[0,2]])
val, vec = np.linalg.eig(A)
print("Eigenvalue", val)
print("Eigenvectors", vec)
It's determinant is given by this equation when we expanded, and that's called the characteristic polynomial. Finding where this is zero are the eigenvalues.
Then you can plug them back into the original matrix $m1$ to find eigenvectors $x$ and $y$:
Solution: