ML Intro Numericals
ML Intro Numericals
Learning: Numericals
Gir House Help Resource
Preface :
I do not like Mathematics. I was a humanities student in classes 11th and 12th and really
hated Maths in classes 9th and 10th. I do not like complex symbols. I like simple addition
and subtraction and good marks and good grades. If you're like me, this is for you.
Contents:
1. Vector means some numbers written between brackets like [1, 2, 3] or [4, 3]
or [-3, 4, 5 ,-34] or (3, 7)
2. Rx means x number of items in a vector.
Example: [2, 34, 17, -84] has R⁴
3. exists only to scare you.You should add everything when you see the
thing
4. For some weird reason, they call numbers as scalars. Hence,Vector is
technically scalars within brackets separated by comma.
Vector : (2,6)
Scalar
5. Norm of a vector is a scalar. Next page tells you how to calculate norm.
Calculating Norm of a Vector
Let [3, 4, 2] be a vector named v. Then the norm of V is : (Remember Python variable assignment?)
Imagine playing a game where you guess how many candies are in a jar. You
lose if your guess is too high or too low. In machine learning, "loss" is a score that
tells us how wrong our guess (or prediction) was. The higher the score, the more
the loss.
e!
i s
Rev
Supervised Learning Unsupervised Learning
Explanation:
1. You will be given a list of X and Y value pairs.
2. Both X and Y will be a list of scalars.
3. f(x): X will have to undergo some formula or function.
4. Subtract corresponding Y from f(x).
5. Calculate square of the result.
6. Add all values obtained from the above process.
7. Divide by the total number of X-Y value pairs. That is the loss.
Solved Example: Regression 1 / 2
Explanation:
1. You will be given a list of X and Y value pairs.
2. Both X and Y will be a list of scalars.
3. f(x): X will have to undergo some formula or function.
4. Subtract corresponding Y from f(x).
5. Calculate square of the result.
6. Add all values obtained from the above process.
7. Divide by the total number of X-Y value pairs. That is the loss.
Solved Example: Regression 2 / 2
y -3 7 1 0 4
Options:
1. ŷ = 0.7 x - 1
2. ŷ = x + 0.5 Hint: The best fit line has the least loss
3. ŷ = x + 0.2 P.S: This hint won't be there in the Quiz.
4. ŷ = 0.2x + 1
Loss Algorithm: Dimensionality Reduction
Explanation:
1. You will be given X, an encoder function and a decoder function.
2. X will be a list of vectors.
3. g(f(X)): First apply the encoder function on X and then the decoder function.
4. Subtract the original X vectors from the g(f(X))
5. Calculate norm of the results. [Refer slide 4]
6. Square the norm.
7. Divide the square of norm with the number of vectors in X. That is the Loss.
Solved Example: Dimensionality Reduction
Check slide 5 if
you forgot how
to calculate the
Norm of a
Vector.
u
xi f(xi) or (x1+x2+x3) / 2 g(f(xi)) or [u,u,u] xi - g(f(xi)) Norm² or ||xi-g(f(xi))||²
sum of ||xi-g(f(xi))||² 5 + 20 + 45 + 80
= = 150 / 4 = use calculator to get
n or i 4 the answer please 🥰
Do it Yourself (Dimensionality Reduction)
Give the reconstruction error for this encoder decoder pair. The reconstruction
error is the mean of the squared distance between the reconstructed input and
input.
May, 2022
Basic Explanation: Classification
Examples:
1 (India is in Asia) = 1
1 (India is in Africa) = 0 ∈ symbol is the same as ‘belongs to’. The statement ‘Parrot ∈
1 (2 is even) = 1 the set of birds’ is the same as the statement ‘Parrot belongs to
1 (10%3=0) = 0 the set of birds’.
1 (Gir is the Best) = 1
1 (2 ∈ (2,3,4)) = 1 On the other hand, ∉ notation means ‘does not belong to’.
Loss Algorithm: Classification
Explanation:
1. You will be given a list of X and Y value pairs. Y will be a list of Signs (+1 or -1).
2. X list will have vectors (usually in R² like (3,2)).
3. f(x): All X values will have to undergo some formula or function.
4. Sign of X: If the result of f(x) is zero or positive integer, write +1. Else, write -1.
5. If Y and Sign of X are equal, write zero. If they are not equal, write one.
6. At the end, add all ones (and zeroes lol)
7. Divide the sum by number of X and Y pairs. That is the loss.
Solved Example: Classification
X Y Which among the following models has the least misclassification
[0,1] +1
error? [September 2023, Quiz 1]
[0,2] +1
1. Sign(x1 − 2)
[1,3] +1 2. Sign(x2 + 2)
[3,4] -1
3. Sign(x1 + x2)
4. Sign(x2 − x1)
[4,5] -1
[7,6] -1
Note:
1) Misclassification Error is the same thing as Loss.
2) Sign (statement) works the same way as 1 (statement).
3) This question could also have been rephrased as ‘which of the following models is the best?’
Solved Example: Classification (Contd.)
f(x) Option 1: Sign(x1 − 2)
X Y x1 − 2 Sign Error
Step 1: First, we have to calculate f(x) for all vectors in
[0,1] +1 0-2 = -2 -1 1 the X column. The formula is (x1 - 2). This means that if
X is [0,1], we will subtract 2 from 0.
[0,2] +1 0-2 = -2 -1 1
Step 2: Then we have to take the Sign value. If f(x) is
[1,3] +1 1-2 = -1 -1 1 more than or equal to zero ( f(x) >= 0), we will write +1
else, we will write -1 in the Sign column.
[3,4] -1 3-2 = 1 +1 1
[1,3] +1 3+2 = 5 +1 0 Step 2: Then we have to take the Sign value. If f(x) is
more than or equal to zero ( f(x) >= 0), we will write +1
[3,4] -1 4+2 = 6 +1 1 else, we will write -1 in the Sign column.
=3 Step 4: Add all 1s and 0s from step 3 and then divide the
sum by the number of X-Y pairs.
x1 x 2 Loss: 3/6 = 0.5
Solved Example: Classification (Contd.)
f(x)
Option 3: Sign(x1 + x2)
X Y x1 + x2 Sign Error
[0,1] +1 0+1 = 1 +1 0 Step 1: First, we have to calculate f(x) for all vectors in
the X column. The formula is (x1+ x2). This means that if
[0,2] +1 0+2 = 2 +1 0 X is [0,1], we will add 0 and 1.
[1,3] +1 1+3 = 4 +1 0
Step 2: Then we have to take the Sign value. If f(x) is
[3,4] -1 3+4 = 7 +1 1 more than or equal to zero ( f(x) >= 0), we will write +1
else, we will write -1 in the Sign column.
[4,5] -1 4+5 = 9 +1 1
Step 3: Compare Sign with Y. If Sign=Y, write 0. Else, if
[7,6] -1 7+6=13 +1 1 Sign≠Y, write 1 in the error column.
=3
Step 4: Add all 1s and 0s from step 3 and then divide the
sum by the number of X-Y pairs.
x1 x 2 Loss: 3/6 = 0.5
Solved Example: Classification (Contd.)
f(x)
Option 4: Sign(x2-x1)
X Y x2 - x1 Sign Error
[0,1] +1 1-0 = 0 +1 0 Step 1: First, we have to calculate f(x) for all vectors in
the X column. The formula is (x2- x1). This means that if
[0,2] +1 2-0 = 2 +1 0 X is [0,1], we will subtract 1 from 0.
[1,3] +1 3-1 = 2 +1 0
Step 2: Then we have to take the Sign value. If f(x) is
[3,4] -1 4-3 = 1 +1 1 more than or equal to zero ( f(x) >= 0), we will write +1
else, we will write -1 in the Sign column.
[4,5] -1 5-4 = 1 +1 1
Step 3: Compare Sign with Y. If Sign=Y, write 0. Else, if
[7,6] -1 6-7 = -1 -1 0 Sign≠Y, write 1 in the error column.
=2
Step 4: Add all 1s and 0s from step 3 and then divide the
sum by the number of X-Y pairs.
x1 x 2 Loss: 2/6 = 0.33
Solved Example: Classification (Contd.)
Let us see which option gives us the lowest loss/misclassification error:
1. 1.0
2. 0.5
3. 0.5
4. 0.33
Obviously Option 4 has the lowest value of Loss and is the best among the given models and the correct
answer.
Do it yourself
The question on the left was
asked in January 23, Quiz 1.
Try solving it on your own.
Use
paper and pen, my lazy
Do it Yourself (Classification)
NOTE :
Sometimes, you might be given a very different loss function to solve the questions.
Pay attention.