Linear Algebra for Data Science With Python
Last Updated on December 11, 2023 by Editorial Team
Author(s): Karthik Bhandary
Originally published on Towards AI.
Linear Algebra, a branch of mathematics, is very useful in Data Science. We can mathematically operate on large amounts of data by using Linear Algebra.
Most algorithms used in ML use Linear Algebra, especially matrices. Most of the data is represented in matrix form.
Now that we know how L. A is used, letβs just get into it!!! Weβll start with the basics β VECTORS
Vectors
βIn mathematics and physics, vector is a term that refers colloquially to some quantities that cannot be expressed by a single number, or to elements of some vector spaces.β [1]
[1] is the definition that is available on Wikipedia. Most people understand it, but to make it simpler you can just say that a vector is:
It is a term that refers to a quantity that has both magnitude and direction.
It is the fundamental building block of linear algebra. Now, if you take a look at the definition from Wiki, you can see that quantities cannot be expressed by a single number. Meaning, that they have a dimension and it can be anything.
The dimensionality of a vector is determined by the number of numerical elements in that vector.
Example: A vector with 4 elements will have a dimensionality of four.
The magnitude of a vector is calculated by using the formula:
Now an example to understand better.
Question: A ball is traveling through the air and given the velocities of the ball, its x, y, and z directions in a standard cartesian coordinate system. The velocity of the component values are: x = -12, y = 8, z = -2. Convert the velocities into a vector and find the total speed of the ball.
Solution:
Applying Vectors using Python:
Numpy arrays are n-dimensional array data structures that can be used to represent both vectors and matrices.
import numpy as np
v = np.array([1,2,3,4,5]) #vector
Basic Vector Operations:
Scalar Multiplication:
Example with Python:
import numpy as np
A = np.array([1,2,3,4]) #vector
print(A*4) #scalar multiplication
Vector Addition and Subtraction:
Example with Python:
import numpy as np
A = np.array([1,2,3,4]) #vector1
B = np.array([-4,-3, -2, -1]) #vector2
print(A+B) #vector Addition
print(A-B) #vector Subtraction
Vector Dot Products:
The dot product takes 2 equal dimension vectors and returns a single scalar value by summing the products of the vectors corresponding components.
The formula is
The dot product is both commutative and distributive.
a.b = b.c
a.(b+c) = a.b + a.c
The resulting scalar value represents how much one vector goes into the other.
If two vectors are perpendicular, then their dot product is 0, as neither goes into the other. The dot product can also be used to find the magnitude of a vector and the angle between two vectors.
Letβs look at some examples of dot products.
Example for dot product using Python:
import numpy as np
A = np.array([1,2,3,4]) #vector1
B = np.array([-4,-3, -2, -1]) #vector2
print(np.dot(A, B)) #dot product
Example for finding the angle:
Matrices:
It's a quantity with m rows and n columns of data. We can combine multiple vectors into a matrix for each matrix is one of the vectors. Matrices are helpful because they allow us to perform operations on large amounts of data, such as representing entire systems of equations in a matrix quantity. We can even access the elements by using the row and column numbers.
In the above, we also show you how to access the elements of the matrix. Like A(1,2), which is 1st row and 2nd column.
Example for matrix using python:
import numpy as np
A = np.array([[1,2],[3,4]]) #matrix
print(A[1,1]) #accessing the elements of the matrix A
Matrix Operations:
Matrix Addition and Subtraction:
We can do this if two matrices have equal shapes.
Example with Python:
import numpy as np
A = np.array([[1,2],[3,4]]) #matrix1
B = np.array([[-3,-2],[-4,-5]]) #matrix2
print(A+B) #Addition
print(A-B) #Subtraction
Matrix Multiplication:
It works by computing the dot product between each row of the first matrix and each column of the 2nd matrix.
If the matrices have dimensions m x n and k x l. If n = k, only then are we able to perform the multiplication.
Example with Python:
There are two ways to do this. One is using the β@β and the other is to use the .matmul()
from numpy
module
import numpy as np
A = np.array([[1,2],[3,4]]) #matrix1
B = np.array([[-3,-2],[-4,-5]]) #matrix2
print(np.matmul(A, B))
print(A@B)
Both the print statements give the same result.
Special Matrices:
There are particularly 3 types of special matrices.
Identity Matrix:
A square matrix with diagonal elements as 1 and remaining as 0. Any matrix multiplied by the identity matrix is itself.
In Python, we can create an identity matrix by using the .eye() method from numpy
import numpy as np
identity = np.eye(4) #creates a 4x4 matrix.
Transpose Matrix:
It is computed by swapping the rows and cols of a matrix. It is denoted by βTβ
In Python, we can use .T to transpose the matrix
import numpy as np
A = np.array([[1,2],[3,4]]) #matrix
A_trans = A.T
Permutation Matrix:
Itβs a square matrix that allows us to flip rows and columns of a separate matrix. Itβs kind of similar to identity, where every element is 0 except for one element in a row, which is 1.
To flip rows in a matrix A we multiply a permutation matrix P on the left (PA). To flip cols, we multiply a permutation matrix P on the right (AP)
Click here to learn how to implement this in Python.
Linear System in Matrix Form:
An extremely useful application of matrices is for solving systems of linear equations.
Letβs take a look at an example.
We will write the above equations in the form of matrices shown below.
Our final goal is to represent the above in the form Ax = b
We can write Ax = b
as [AU+007Cb]
With NumPy's linalg submodule, we can do this.
Example:
We converted the above linear equation system into matrices.
A = np.array([[1,4,-1],[-1,-3,-2],[2, -1, -2]])
b = np.array([-1,2,-2])
x, y, z = np.linalg.solve(A, b)
Inverse Matrix:
βIn linear algebra, an n-by-n square matrix A is called invertible (also nonsingular or nondegenerate), if there exists an n-by-n square matrix B such that AB = BA = Iβ [2]
[2] Wikipedia
As mentioned above, the product of the inverse of a matrix and itself is identity. Not all matrices have an inverse. Those who donβt have an inverse are called singular matrices.
Example with Python:
import numpy as np
A = np.array([[1,2],[3,4]])
print(np.linalg.inv(A))
Miscellaneous:
- we can create a matrix or vector consisting of zeros by using
.zeros() from numpy
Example:
import numpy as np
print(np.zeros((3,2)) #prints a matrix of dimensions 3x2.
- βnormβ of a vector can be found using NumPy's linalg submodule.
Example:
import numpy as np
A = np.array([2,-4,1])
A_norm = np.linalg.norm(A) #gives 4.5825
Conclusion:
In this blog, we learned:
- what matrices and vectors are.
- The operations performed on them.
- Implemented them using Pythonβs NumPy module.
- We even saw different types of matrices.
That is it, guys. I hope you found this helpful, if you did, please follow me on LinkedIn.
If you like my work, you can buy me a cup of coffee: dataguy6@ybl
Check out my recent works:
DagsHub Is The Way To Go For Data Scientists
The Best To Start And Store Your Data Science Projects!
medium.com
Best Resources For Learning Data Science
Are you planning on learning Data Science?U+1F929 But have no idea where to and how to learn?U+1F62BDonβt worry because I got youβ¦
medium.com
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI