Photo: Markus Spiske

Day-58 Math Behind the ML with Python-5 (Linear Algebra-Matrices)

Samet Girgin
PursuitOfData
Published in
5 min readNov 15, 2019

--

In the previous piece, it was shared some tips and pratices about vector and
and revealed vectors by representing them as matrices.
In general terms, a matrix is an array of numbers that are arranged into rows and columns.

Matrices are a foundational element of linear algebra. Matrices are used throughout the field of machine learning in the description of algorithms and processes such as the input data variable (X) when training an algorithm.

Two 2X3 Matrices & the the summation and the transpose results

The matrices are generally named as a capital letter. We refer to the elements of the matrix using the lower case equivalent with a subscript row and column indicator, like this:

First of all, I prefer to share how to represent the matrix and how to compute the summation and subtractions operations. Then after we will look over the other operations. Now let’s jump to the Pyhton notebook to look over some examples of the basic two matrix operations.

Multiplication of Matrix:

Multiplying matrices is a little more complex than the operations up to this point. There are two cases to consider, scalar multiplication (multiplying a matrix by a single number), and dot product matrix multiplication multiplying a matrix by another matrix.

Scalar and dot Products of the Matrices

The commmutative law of multiplication can not be applied şn dot product matrix multiplication.

Identity Matrix: An identity matrix (usually indicated by a capital I) is the equivalent in matrix terms of the number 1. All the elements are 1 in the diagonal positions and 0 in the other positions:

Matrix Division: We can’t actually divide by a matrix; but when we want to divide matrices, we can take advantage of the fact that division by a given number is the same as multiplication by the reciprocal of that number.

Inverse of a Matrix: Inverse of a matrix seems to reciprocal of a number.

Solving Systems of Equations with Matrices: Matrices are commonly used to solve equation systems

Let’s look over the continue from the notebook below:

Matrix Transformation:

Matrices and vectors are used together to manipulate spatial dimensions. This has a lot of applications, including the mathematical generation of 3D computer graphics, geometric modeling, and the training and optimization of machine learning algorithms. We’re not going to cover the subject exhaustively here; but we’ll focus on a few key concepts useful to know when you plan to work with machine learning.

Linear Transformations: A vector is manipulated by multiplying it with a matrix. The matrix acts a function that operates on an input vector to produce a vector output. Specifically, matrix multiplications of vectors are linear transformations that transform the input vector into the output vector.

Let’s suppose we have the vector v, with elements 1 and 2. We could define a function T to operate on a vector (Transformation), and apply it to v. In this case, the function multiplies the first element of the vector by 3, and the second by 2; which changes our vector elements to 3 and 4. This transforms our vector, changing both its length and direction.

Now let’s suppose we have a matrix. We can express our transformation in the form of this matrix. To apply the transformation, we just perform the dot product multiplication of the matrix with the vector; which gives us a transformed vector

Transformations of Magnitude and Amplitude

When we multiply a vector by a matrix, we can transform it by scaling the length (magnitude) of the matrix or changing the direction (amplitude) of the matrix.

Afine Transformations: An afine transformation multiplies a vector by a matrix and adds an offset vector, sometimes referred to as bias; like this: 𝑇(𝑣⃗)=𝐴𝑣⃗+𝑏⃗

Eigenvalues and Eigenvectors: We can see that when we transform a vector using a matrix, we change its direction, length, or both. When the transformation only affects scale (in other words, the output vector has a different magnitude but the same amplitude as the input vector), the matrix multiplication for the transformation is the equivalent operation as some scalar multiplication of the vector.

In cases like these, where a matrix transformation is the equivelent of a scalar-vector multiplication, the scalar-vector pairs that correspond to the matrix are known respectively as eigenvalues (λ) and eigenvectors (𝑣⃗).

𝑇(𝑣⃗)=𝐴𝑣⃗=𝜆𝑣⃗

The below notebook contains the operation related to the detailed calculations of transformation, eigenvalues and eigenvectors

That’s the end of the introduction to linear algbera from ML. Please follow me from my Twitter, Linkedin and Medium pages. Have a good ML journey.

References and Further Readings:

https://courses.edx.org/courses/course-v1:Microsoft+DAT256x+3T2019/courseware/bc6a0702b886405fad4f7a888702e252/59091f9f4cc6442b91180c8e0fb301f7/?child=first

https://machinelearningmastery.com/matrix-operations-for-machine-learning/

https://machinelearningmastery.com/introduction-matrices-machine-learning/

--

--

Samet Girgin
PursuitOfData

Data Analyst, Petroleum & Natural Gas Engineer, PMP®