# <center><i class="fa fa-edit"></i> Basic Linear Algebra for Machine Learning </center> ###### tags: `notes` :::info **Goal:** To understand what is 'Linear Algebra' and how is it used in Machine Learning **Resources:** [Introduction to Machine Learning](https://hackmd.io/T4oKVVPiRXqtD-k6a5h7Ug?both) ::: ## <b> The Matriks </b> Matriks is the heart of Linear Algebra. A matrix is essentially bunch of numbers stored between 2 brackets in a tabular manner. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1611472884113/vA7tC2UwW.png?auto=compress) ## What could these number in the matrix be? Here are a couple of example :point_down: - Pixels in an image Left is an image of '8' and on the right is a matrix showing the numerical values of each pixel (black-0, white-255). ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1611472933323/lBQ14ADv5.png?auto=compress) - Tabular Data from an excel sheet ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1611472959029/UA2M_yM_G.png?auto=compress) ### <b>We represent matrices (plural for a matrix) in Python using a LoL (List of Lists). </b> ==matrix = [[1,2,3], &emsp;&emsp;&emsp;&emsp;&nbsp; [4,5,6], &emsp;&emsp;&emsp;&emsp;&nbsp; [7,8,9]]== ### <b> The below picture shows the mathematical notation for writing matrices. </b> 'i' denotes the number of terms on the Y-axis and 'j' denotes the number of terms on the X-Axis in the Matrix. ![](https://i.imgur.com/qwclqMP.png) ![](https://i.imgur.com/dStgtDB.png) ### Transpose of a Matrix 'Transpose' is one way to transform a matrix. Imagine you have an imaginary line going from the top-left corner of the matrix to the bottom right, and then you interchange the opposite values. The matrix you get after switching the values is the transpose of the matrix. ![](https://i.imgur.com/fHcvEi6.png) Think of a transpose as flipping the matrix along the red line. <i> <b> In mathematical notation, a transpose is denoted by a small 'T' at the top of the letter that was originally assigned to the matrix</b></i> ![](https://i.imgur.com/FrquGwY.png) ### <b> Identity Matrix</b> There is a special type of matrix called the 'identity' matrix. They look like this (basically the top-left to bottom-right diagonal consist of 1s and the other values are 0s.) ![](https://i.imgur.com/GIoVZ9l.png) ### <b> Adding, Substracting, and Multiplying matrices</b> We can also add, substract or multiply matrices. Adding and subtracting matrices is as simple as adding/subtracting the corresponding values of the matrices. ![](https://i.imgur.com/CBvp3k1.png) On the other hand, multiplying matrices is a bit tricky. It looks somewhat like this :point_down: ![](https://i.imgur.com/iB1a470.png) It probably doesn't make sense, which is why you should look at this [site](https://mathsisfun.com/algebra/matrix-multiplying.html). ### But, why is Linear Algebra important in Machine Learning? Let's say we want to make a machine learning model that can recognize numbers in a given image. We can't just give this image to a computer and ask "Hey, what number is in this image?" ![](https://i.imgur.com/6N1G4cD.png) We need to convert that image into something that a computer can understand, like a LoL (List of List) which is a representation of matrix in Python. ![](https://i.imgur.com/abveMTI.png) <i><b> Many times we need to transform this matrix in a way that our machine learning model wants it to be. </b></i> It could be a transpose, addition, or multiplication but I won't get into the spesifics of it. ### <b> Another thing that you must know about is the difference between a Scalar, Vector, and Matrix.</b> - Scalar: Just a number - Vector: A row or column of numbers between brackets - Matrix: Numbers in a tabular form between brackets with multiple rows and columns. ![](https://i.imgur.com/CSkWnbj.png)