Basic Linear Algebra For Deep Learning and Machine Learning Python Tutorial - by Towards AI Team - Towards AI - Oct, 2020 - Medium PDF
Basic Linear Algebra For Deep Learning and Machine Learning Python Tutorial - by Towards AI Team - Towards AI - Oct, 2020 - Medium PDF
This is your last free member-only story this month. Sign up for Medium and get an extra one
Figure 1: A three-dimensional Euclidean space used to represent solutions of linear equations [1] [2]. Image is a vector
derivative from “High-dimensional Simplexes for Supermetric Search” by Richard Connor, Lucia Vadicamo, and
Fausto Rabitti [3].
This tutorial’s code is available on Github and its full implementation as well on
Google Colab.
Table of Contents
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 1/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
1. Introduction
3. Matrix
4. Vector
5. Matrix Multiplication
6. Transpose Matrix
7. Inverse Matrix
8. Orthogonal Matrix
9. Diagonal Matrix
16. Lasso
17. Ridge
21. Orthogonality
23. Span
24. Basis
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 2/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
27. Conclusion
Introduction
The foundation of machine learning and deep learning systems wholly base upon
mathematics principles and concepts. It is imperative to understand the fundamental
foundations of mathematical principles. During the baseline and building of the model,
many mathematical concepts like the curse of dimensionality, regularization, binary,
multi-class, ordinal regression, and others must be artistic in mind.
The basic unit of deep learning, commonly called a neuron, is wholly based on its
mathematical concept, and such involves the sum of the multiplied values involving
input and weight. Its activation functions like Sigmoid, ReLU, and others, have been
built using mathematical theorems.
These are the essential mathematical areas to understand the basic concepts of machine
learning and deep learning appropriately:
Linear Algebra.
Vector Calculus.
Matrix Decomposition.
Analytic Geometry.
These are some of the areas in linear algebra that we use in machine learning (ML) and
deep learning:
Vector Space.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 3/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Basis.
Also, these are the areas of machine learning (ML) and deep learning, where we apply
linear algebra’s methods:
Dimensionality Reduction.
Regularization.
Covariance Matrix.
Convolution.
Matrix
A matrix is an essential part of linear algebra. It stores m*n elements of data, and we use
it for the computation of the linear equation system or the linear mappings. It is an m*n
tuple of real-value elements.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 4/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
The number of rows and columns is called the dimension of the matrix.
Vector
In linear algebra, a vector is an n*1 matrix. It has only one column.
Matrix Multiplication
Matrix multiplication is a dot product of rows and columns where the row of the matrix
is multiplied and summed up with another matrix column.
In Linear Regression
There are multiple features to predict the price of a house. Below is the table of different
houses with their features and the target value (price).
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 5/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Figure 6: Table 1 showcasing features of houses for the prediction of their price.
Transpose Matrix
For A ∈ R^m*n the matrix B ∈ R^n*m with bij = aij is called transpose of A. It is
represented as B = A^T.
Example:
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 6/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Inverse Matrix
Consider a square matrix A ∈ R^n*n. Let matrix B∈R^n*n has the property that AB =
In = BA, B is called the inverse of A and denoted by A^-1.
Orthogonal Matrix
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 7/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
A square matrix A∈R^n*n is an orthogonal matrix if and only if its columns are
orthonormal (unit length) so that:
Example:
Consequently,
Diagonal Matrix
A square matrix A∈R^n*n is a diagonal matrix where all the elements are zero except
those on the main diagonal like:
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 8/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Example:
Implementation by taking the data from the table above, “Table1” is shown in figure 5.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 9/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Transpose of matrix x:
# Transpose of x
transpose_x = x.transpose()
transpose_x
Figure 21: Multiplication of the transposed matrix with the original matrix.
The inverse of the multiplication of the transposed matrix with the original matrix
inverse_of_multi_transpose_x_to_x =
np.linalg.inv(multi_transpose_x_to_x)inverse_of_multi_transpose_x_to_
x
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 10/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
multiplication_transposed_x_y = np.dot(transpose_x,
y)multiplication_transposed_x_y
theta = np.dot(inverse_of_multi_transpose_x_to_x,
multiplication_transposed_x_y)theta
Linear Equation
The linear equation is the central part of linear algebra by which many problems are
formulated and solved. It is an equation for a straight line.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 11/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Example: x = 2
Y = bX + a
Where,
a = It is a Y-intercept and determines the point where the line crosses the Y-axis.
b = It is a slope and determines the direction and degree to which the line is tilted.
Implementation
Predict the price of the house where the variables are square feet and price.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 12/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
def get_mean(value):
total = sum(value)
length = len(value)
mean = total/length
return mean
def get_variance(value):
mean = get_mean(value)
mean_difference_square = [pow((item - mean), 2) for item in
value]
variance = sum(mean_difference_square)/float(len(value)-1)
return variance
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 13/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
def linear_regression(df):
X = df['square_feet']
Y = df['price']
m = len(X) square_feet_mean = get_mean(X)
price_mean = get_mean(Y)
#variance of X
square_feet_variance = get_variance(X)
price_variance = get_variance(Y)
covariance_of_price_and_square_feet = get_covariance(X, Y)
w1 = covariance_of_price_and_square_feet
float(square_feet_variance) w0 = price_mean - w1 *
square_feet_mean
linear_regression(df)
Vector Norms
Vector norms measure the magnitude of a vector [5]. Fundamentally, the size of a given
variable x can be represented by its norm ||x||, and the norm represents the distance
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 14/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 15/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
As shown in figure 32, the red lines symbolize the set of vectors for the L1 norm
equation.
As shown in figure 34, the red lines symbolize the set of vectors for the L2 norm
equation.
To handle collinearity.
To prevent overfitting.
L1 Regularization (Lasso)
L2 Regularization (Ridge)
L1 Regularization (Lasso)
Lasso is a widespread regularization technique. Its formula is shown in figure 35:
L2 Regularization (Ridge)
The equation of L2 regularization (Ridge):
Where, λ = Controls the tradeoff of complexity by adjusting the weight of the penalty
term.
Feature Extraction
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 17/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
In feature extraction, we find a set of features from the existing features through some
function mapping.
Feature Selection
In feature selection, select a subset of the original features.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 18/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
PCA is a critical feature extraction method, and it is vital to know the concepts of the
covariance matrix, eigenvalues, or eigenvectors to understand the concept of PCA.
Covariance Matrix
The covariance matrix is an integral part of PCA derivation. The below concepts are
important to compute a covariance matrix:
Variance.
Covariance.
Variance
or
The limitation of variance is that it does not explore the relationship between variables.
Covariance
Covariance is used to measure the joint variability of two random variables.
Covariance Matrix
A covariance matrix is a squared matrix that gives the covariance between each pair of
given random vector elements.
Let m be an n*n matrix. A scalar λ is called the eigenvalue of m if there exists a non-zero
vector x in R^n such that mx = λx.
mx — λx = 0
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 20/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Figure 45: Solving this equation for λ gives us all the eigenvalues of m.
Example:
Solution:
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 21/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Orthogonality
Two vectors v and w are called orthogonal if their dot product is zero [7].
v.w = 0
Example:
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 22/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Orthonormal Set
If all vectors in the set are mutually orthogonal and all of the unit lengths, then it is
called an Orthonormal set [8]. An orthonormal set that forms a basis is called an
orthonormal basis.
Span
Let V is a vector space and its elements are v1, v2, ….., vn ∈ V.
Any sum of these elements multiplied by the scalars representing the equation shown in
figure 53, a set of all linear combinations is called the span.
Example:
Hence:
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 23/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Basis
A basis for a vector space is a sequence of vectors that form a set that is linearly
independent and that spans the space [9].
Example:
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 24/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Let there is an N*1 vector with values x1, x2, ….., xm.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 25/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 26/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Standardization
It is always good to standardize the data to keep all features of the data in the same
scale.
standardized_x =
StandardScaler().fit_transform(load_iris.data)standardized_x[:2]
covariance_matrix_x = np.cov(standardized_x.T)covariance_matrix_x
eigenvalues
eigenvectors
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 27/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
The values shown in figure 68 indicate the variance giving the analysis below:
So, the third and fourth Components have very low variance respectively. These can be
dropped. Because these components can’t add any value.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 28/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
matrix_weighing = np.hstack((eigenpairs[0]
[1].reshape(4,1),eigenpairs[1][1].reshape(4,1)))matrix_weighing
Y = standardized_x.dot(matrix_weighing)Y
Plotting
plt.figure()target_names = load_iris.target_names
y = load_iris.targetfor c, i, target_name in zip("rgb", [0, 1, 2],
target_names):
plt.scatter(Y[y==i,0], Y[y==i,1], c=c,
label=target_name)plt.xlabel('PCA 1')
plt.ylabel('PCA 2')
plt.legend()
plt.title('PCA')
plt.show()
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 29/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Figure 73: The plotted result of the PCA 1 and PCA 2 from the first dataset.
Let M is a rectangular matrix and can be broken down into three products of matrix —
(1) orthogonal matrix (U), (2) diagonal matrix (S), and (3) transpose of the orthogonal
matrix (V).
Conclusion
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 30/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
Machine learning and deep learning has been build upon the concept of mathematics. A
vast area of mathematics is used to build algorithms and also for the computation of
data.
Linear algebra is the study of vectors [10] and several rules to manipulate vectors. It is a
key infrastructure, and it covers many areas of machine learning like linear regression,
one-hot encoding in the categorical variable, PCA (Principle component analysis) for the
dimensionality reduction, matrix factorization for recommender systems.
Deep learning is completely based on linear algebra and calculus. It is also used in
several optimization techniques like gradient descent, stochastic gradient descent, and
others.
Matrices are an essential part of linear algebra that we use to compactly represent
systems of linear equations, linear mapping, and others. Also, vectors are unique objects
that can be added together and multiplied by scalars that produce another object of
similar kinds. Any suggestions or feedback is crucial to continue to improve. Please let us
know in the comments if you have any.
DISCLAIMER: The views expressed in this article are those of the author(s) and do not
represent the views of Carnegie Mellon University, nor other companies (directly or
indirectly) associated with the author(s). These writings do not intend to be final
products, yet rather a reflection of current thinking, along with being a catalyst for
discussion and improvement.
Resources:
Google colab implementation.
Github repository.
References
[1] Linear Algebra, Wikipedia, https://en.wikipedia.org/wiki/Linear_algebra
[5] Vector Norms by Roger Crawfis, CSE541 — Department of Computer Science, Stony
Brook University, https://www.slideserve.com/jaimie/vector-norms
Towards AI publishes the best of tech, science, and engineering. Subscribe with us to receive our
newsletter right on your inbox. For sponsorship opportunities, please email us at
[email protected] Take a look
Your email
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information
about our privacy practices.
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 32/33
10/20/2020 Basic Linear Algebra for Deep Learning and Machine Learning Python Tutorial | by Towards AI Team | Towards AI | Oct, 2020 | Me…
https://medium.com/towards-artificial-intelligence/basic-linear-algebra-for-deep-learning-and-machine-learning-ml-python-tutorial-444e23db3e9e 33/33