Introduction
Artificial Intelligence and Machine Learning may sound like complicated topics filled with difficult math and smart computers, but at the core, they are all about mathematics.
Whenever we train an AI model, try to improve its performance, or use methods like gradient descent, we are actually using math. It is the language that helps machines learn and make decisions.
Before moving to advanced AI topics, it is important to start with the basics, and one of the most important foundations is Algebra.
Algebra helps us find unknown values, understand relationships, and discover patterns, just like how AI learns patterns from data.
In this post, we will see how simple algebra equations can help solve real life problems and how we can use Python to visualize them.
What is Algebra
Solving for unknows within system of linear equations is
Algebra
Problem Statement
Solution
There are unknows in this problem statement which can be
solved by algebra.
Consider d for distance and t for time.
Police van has a speed of 150 km/h
=>d/t = 150 mk/h
=>d/t = 2.5 km/minute ---------------------------- Equation1
Bank robber van has a speed 180km/h and started 5 minutes
later than police van.
=>d/(t-5) = 180 km/h
=>d/(t-5) = 3 km/h
=>d = 3t – 15 -----------------------------------------Equation2
From the above 2 equations
=>3t-15 = 2.5t
=>0.5t = 15
=>t = 30 minutes
Using the value of t in equation 2
d = 3x30 – 15
=>d = 75 kilometers
From the above result we are clear that after a distance of
75 kilometers police can catch the rubber in 30 minutes of drive.
Using pen and paper we can plot the
graph for the above to equations to get the unknowns. Same can be done using
python code as well.
Let’s try using python programming.
Find the unknowns in Python program using NumPy and
matplotlib
Download the following packages if not
downloaded already.
pip install NumPy
pip install matplotlib
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 40, 1000) # start, finish, n points
dr = 2.5 * t
dc = 3*t - 15
#Matplotlib command that creates a Figure and Axes — the two main objects used for plotting.
fig, ax = plt.subplots()
plt.title('Bank Robber Caught Sample Plot')
plt.xlabel('time in minutes')
plt.ylabel('distance in km')
ax.set_xlim([0, 40])
ax.set_ylim([0, 100])
ax.plot(t, dr, c='green')
ax.plot(t, dc, c='blue')
plt.axvline(x=30, color='yellow', linestyle='--')
plt.axhline(y=75, color='yellow', linestyle='--')
# To show the graph
plt.show()
What is Tensor?
A tensor is a mathematical object that generalizes scalars, vectors
and matrices to higher dimensions. Tensors are widely used in fields like
physics, engineering and machine learning to represent and manipulate data.
|
Dimension |
Mathematical
Name |
Description |
|
0 |
Scalar |
Only
magnitude of x |
|
1 |
Vector |
An array. [x,
y] |
|
2 |
Matrix |
Square or a
table |
|
3 |
3-Tensor |
Cube or 3D
table |
|
n |
n-Tensor |
Nth dimension |
Scalars in Python
Scalar is just a value like integer 25
In python declare a variable and assign 25 which is a scalar value.
Widely used two automatic differentiation libraries in Python
are Pytorch and TensorFlow.Let’s see the scalar value in these libraries
Scalar in Pytorch
Install libraries if not install already.
pip install TensorFlow
pip install torch torchaudio
Scalar in TensorFlow
>>> import tensorflow as tf
>>> x = tf.Variable(25, dtype=tf.int32)
>>> print(x) #returns <tf.Variable 'Variable:0' shape=()
dtype=int32, numpy=25>
>>> print(x.shape) #returns ()
Vectors
The vector is 1-dimensional array of numbers. Example [x1,
x2] = [10,20]
The vector of length 2 represents a location in 2-dimensional matrix
The vector of length 3 represents a location in 3-dimensional matrix
The vector of length n represents a location in n-dimensional tensor
Vector Transposition
The concept of converting row vector to column vector and
vice versa is called vector transposition.
Example: [x1, x2, x3] T = [x1
x2
x3]
Let’s see the example in Python code


No comments:
Post a Comment