Basic Mathematics for Neural Networks | Vectors and Matrices with Matplotlib
Updated on: May 2, 2020 ·
12 mins read
Vector and Matrices are at the heart of all Neural Networks. Today we are going to learn about vector and Matrix mathematics with the help of
Matplotlib
and numpy
.
First, we are going to understand different analogies in Neural Networks which correspond to Vectors and Matrices.
1. Vector in Machine Learning
2. Matrix representation in Machine Learning
3. Vectors in Simple Mathematics and its operations
4. Addition and Subtraction of Vectors
5. Dot product of two vectors
6. Projection of a vector onto another vector
7. Matrices in Mathematics
8. Matrix Multiplication.
Also, we are going to plot some vectors and see how vector operations work.
2. Matrix representation in Machine Learning
3. Vectors in Simple Mathematics and its operations
4. Addition and Subtraction of Vectors
5. Dot product of two vectors
6. Projection of a vector onto another vector
7. Matrices in Mathematics
8. Matrix Multiplication.
Vector in Machine Learning
A vector represents one of the data points in the whole dataset.For example, let’s say you are working with a YouTube video creator who makes unboxing videos of different phones and other electronic stuff. Just for the simplicity, your task is to predict which phone to review next so that your channel can get maximum views and maximum social media shares. You already have the historical data of the channel. You also have a list of 50 upcoming phones but you are only allowed to review 5 out of them. The features available to you are:
| Company | Price | Number of Cameras | Screen length | Traction |
|----------|-------|-------------------|---------------|----------|
| Apple | 1000 | 3 | 5.5 | 85% |
Matrix representation in Machine Learning
A Matrix can be represented as a set of a few vectors. The collection of data points is a Matrix.For example, a few data points for predicting the next phone to review.
| Company | Price | Number of Cameras | Screen length | Traction |
|----------|-------|-------------------|---------------|----------|
| Apple | 1000 | 3 | 5.5 | 85% |
| Samsung | 800 | 1 | 6 | 70% |
Vectors in Simple Mathematics and its operations
In general, a vector is defined as something having both magnitude and direction. If we can plot this in the 2-D graph, it will be represented as a line pointing in a direction. From now on, we will be talking about vectors in a 2-D planes to make things simple. In mathematics, a vector is represented as,
$\overrightarrow{x}$
Let’s represent a simple vector in 2-D plane using matplotlib and numpy.
0, 0, 5, 3
$\begin{vmatrix}0\\ 0\end{vmatrix}\ and\ \begin{vmatrix}5\\ 3\end{vmatrix}$
To represent this vector in a plane, we need to import NumPy and Matplotlib.
Import required packages
import numpy as np
import matplotlib.pyplot as plt
quiver
method to plot the vector on the 2D plane.
plt.quiver(0, 0, 5, 3, scale_units='xy', angles='xy', scale=1, color="red")
# Just to make the plot look better.
plt.xlim(-5, 10)
plt.ylim(-5, 10)
def plot_2D_vectors(vectors, colors=('red', 'blue', 'green')):
# random numbers
min_x = 0
max_x = 0
min_y = 0
max_y = 0
i = -1
for points in vectors:
for a in range(0, 4):
if a % 2 == 0:
if points[a] <= min_x:
min_x = points[a] - 1
if points[a] >= max_x:
max_x = points[a] + 1
else:
if points[a] <= min_y:
min_y = points[a] - 1
if points[a] >= max_y:
max_y = points[a] + 1
i = i + 1
plt.quiver(points[0], points[1], points[2], points[3], scale_units='xy', angles='xy', scale=1, color=colors[i])
plt.xlim(min_x, max_x)
plt.ylim(min_y, max_y)
Addition and Subtraction of Vectors
Before doing any operation on vectors, we will have to represent them using Numpy.np_vector_1 = np.array([0, 0, 5, 3])
np_vector_2 = np.array([0, 0, 6, 8])
plot_2D_vectors([np_vector_1, np_vector_2])
np_vector_3 = np_vector_1 + np_vector_2
plot_2D_vector(np_vector_1)
plot_2D_vector(np_vector_2, 'b')
plot_2D_vector(np_vector_3, 'g')
np_vector_1 = np.array([0, 0, -5, -3])
np_vector_2 = np.array([0, 0, 6, 8])
np_vector_3 = np_vector_1 + np_vector_2
plot_2D_vectors((np_vector_1, np_vector_2, np_vector_3), ['r', 'b', 'g'])
np_vector_1 = np.array([0, 0, -5, -3])
np_vector_2 = np.array([0, 0, 6, 8])
np_vector_3 = np_vector_2 - np_vector_1
plot_2D_vectors((np_vector_1, np_vector_2, np_vector_3), ['r', 'b', 'g'])
Dot product of two vectors
The dot product of two vectors gives just the magnitude by multiplying the point-wise values in the vector.np.dot(np.array([3, 4]), np.array([5, -1]))
$\overrightarrow{x} . \overrightarrow{y} = x_i y_i + x_j y_j$
Projection of a vector onto another vector
Projection of a vector onto another vector is the image that it makes when the vector image is seen when there is a light source right at the top of the vector whose projection is being calculated.np_vector_1 = np.asarray([0, 0, 3, 4])
np_vector_2 = np.asarray([0, 0, 4, -1])
dot_product = np.dot(np_vector_1, np_vector_2)
proj_vector = (dot_product/np.power(np.linalg.norm(np_vector_2), 2))*np_vector_2
plot_2D_vectors((np_vector_1, np_vector_2, proj_vector), ['r', 'green', 'red'])
np_vector_1 = np.array([0, 0, 4, 0])
np_vector_2 = np.array([0, 0, 0, 4])
dot_product = np.dot(np_vector_1, np_vector_2)
proj_vector = (dot_product/np.power(np.linalg.norm(np_vector_2), 2))*np_vector_2
plot_2D_vectors((np_vector_1, np_vector_2, proj_vector), ['r', 'green', 'red'])
$\frac{\overrightarrow{x} . \overrightarrow{y}}{\begin{Vmatrix}\overrightarrow{y}\end{Vmatrix}^2}\ \overrightarrow{y}$
Matrices in Mathematics
Matrices are the collection of vectors. Mathematically they are represented as, \begin{vmatrix}x_1 & x_2 & x_3\ x_4 & x_5 & x_6\ x_7 & x_8 & x_9\end{vmatrix}Matrix Multiplication.
Matrix multiplication is done by multiplying and adding every row to the corresponding columns. Read the following post to understand it thoroughly. We can do the same using NumPy dot product as well.vector_1 = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
vector_2 = np.array([(1, 4), (2, 5), (3, 6)])
vector_1.dot(vector_2)
> array([[ 14, 32],
[ 32, 77],
[ 50, 122]])
numpy
and matplotlib
. We will talk about a more extended version of mathematics in different models in later versions.
Do subscribe to the newsletter, so that you get to know whenever I push a new article.
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!