Linear Algebra for Data Science Ep1 — Introduction to Vectors and Matrices using Python | by Harshit Tyagi | Towards Data Science

Now that we have set the ground for learning mathematics for data science, it is time to get started with the first topic in the series which is Linear Algebra. This post is the first one in the linear algebra series where I introduce the basic components of algebra that we’ll be using going forward. It is short, crisp, and focused.
You can also follow me step-by-step here:
Topics we’ll cover in this post:
Important definitions and notations of scalars, vectors, matrices, and tensors.
Creating vectors and matrices using NumPy.
Transposing a matrix(2D array) and coding them.
Addition and broadcasting in matrices using NumPy
So, let’s get started...
Scalar: Any single numerical value is a scalar as shown in the image above. It is simply denoted by lowercase and italics. For example: n
Vector: An array of numbers(data) is a vector. You can assume a column in a dataset to be a feature vector.
A vector is conventionally denoted by a lowercase, italics and bold type variable(feature) name. For example: x
Matrix: A matrix is a 2-D array of shape (m×n) with m rows and n columns.
Each element can be reached via its row and column and is denoted by a subscript. For example, A1,1 returns the element at 1st row and 1st column.
A matrix is denoted by uppercase, italics, and bold variable name. For example: A
Tensor: Generally, an n-dimensional array where n>2 is called a Tensor. But a matrix or a vector is also a valid tensor.
Now that we know how they are defined, let’s look at how we are going to use NumPy to see our Math in action. If you want to why NumPy or if you need a refresher on NumPy, check the link below:
towardsdatascience.com
Since a vector is an array and NumPy has various methods of creating an array. Firstly, install NumPy and import it in your workspace/environment:
import numpy as np
And then creating an array requires you to use array() method from the library:
You can then check the shape of the array using the shape attribute:
Creating a matrix is again very simple, you just have to pass a 2D list to the array method:
Transposing a matrix is becomes a very important topic as it further contributes to calculating the inverse of a matrix and other data manipulation where you want to mirror the axes of your dataframe.
Transposing a matrix converts a row vector to a column vector and vice-versa, it is denoted by the superscript T:
Here is what a square matrix(identical number of rows and columns) looks like after transposition with interchanged axes:
And if the matrix is not square, here is how the shape of the matrix changes:
There are 2 ways to transpose a matrix, you can either use the T attribute or simply use the transpose method:
We can also check for the changed shape of the matrix(2D array):
Addition is just another simple operation that you would want to perform among many others.
We can add matrices of the same shape:
A + B = C
Each cell in matrix A gets added to the corresponding element in matrix B and the sum is stored at the corresponding position in the resulting matrix C as shown in the figure on the left.
Now, you’d ask what if we have to add 2 matrices of different shape or add a scalar to a matrix, well NumPy has got us covered for that with broadcasting:
Broadcasting is a technique that adds a scalar or a different shaped vector to a matrix by extending itself to all the elements of the matrix. The scalar is added to each and every element of the matrix which is the reason it is called “broadcasting”.
Let’s say we have 2 matrices of different shapes like in the image below:
In this case, the matrix B(the smaller matrix) extends itself to match the shape of matrix A(bigger matrix) like this:
You can check for other combinations and play around with the code using this Google Colab notebook:
colab.research.google.com
The next post in this series is on multiplication of matrices and vectors publishing early next week. Till then, stay tuned and keep learning Data Science with Harshit.
With this channel, I am planning to roll out a couple of series covering the entire data science space. Here is why you should be subscribing to the channel:
The series would cover all the required/demanded quality tutorials on each of the topics and subtopics like Python fundamentals for Data Science.
Explained Mathematics and derivations of why we do what we do in ML and Deep Learning.
Podcasts with Data Scientists and Engineers at Google, Microsoft, Amazon, etc, and CEOs of big data-driven companies.
Projects and instructions to implement the topics learned so far.
You can connect with me on Twitter, LinkedIn, or Instagram(where I talk about health and wellness.)