Deriving Formulas
To start predicting the value of y with x lets just see the 2 points given.
Let’s assume the points are A (1,5) and B (2,9)
now put the points on graph we get:
So how much we traveled on x-axis from the initial point ‘A’ till the final point ‘B’
we traveled(change in x): x2-x1 on the x-axis.
change in x = 2–1 change in x = 1
Similarly,
How much we traveled on y-axis from the initial point ‘A’ till the final point ‘B’
we traveled(change in y): y2-y1 on the y-axis.
change in y = 9–5 chnage in y = 4
now let’s find the ratio of change in y with respect to change in x(m). (Because we wanna find y using x).
cahnge in y y2-y1
m = ----------- = -----
change in x x2-x1
cahnge in y 4
m = ----------- = - (putting the values)
change in x 1
Thus m = 4
This means y = m(x) and m = (y2-y1)/(x2-x1)
Let’s try this:
for A (1,5) x = 1 and y = 5
using formula: y = m(x)
5 = 4(1)
5 = 4 ,here it’s not equal we must add 1 but why so
Let’s try for B (2,9) x = 2 and y = 9
using formula: y = m(x)
9 = 4(2)
9 = 8 ,here it’s not equal we must add 1 but why so
why we are adding 1 to make it equal, let’s again move towards graph:
In the Graph if we extend the line AB, we can see it cuts the y-axis on 1
which tells that yes y = m(x) but we have to add the starting of y(c)to get the value which is 1 in this case.
so the new formula to get y with respect to x is: y = m(x) +c (proof for Slope Intercept form of straight line)
But how we can get c for other cases: By now we know y = m(x) + c
we can write:
c = y-m(x)
Let’s try this:
for A (1,5) c = 5–4(1), using c = y-m(x)
c = 1, it is satisfying.
Let’s try this for B (2,9):
c = 9–4(2), using c = y-m(x)
c = 1, it is satisfying.
The logic is we are drawing a line from c where the line cuts y-axis (x=0) till x given with the slope m and then finding the y at the end.
Formulas are:
- m = (y2-y1)/(x2-x1) :slope of the line
- c = y-m(x) :starting point of the line
- x = given :total length of line (x-0, as the value of x at starting point is 0)
- y = m(x) +c
Let’s code this to find m and c
In [1]:
def learn(x1,y1,x2,y2):
m = (y2-y1)/(x2-x1)
c = y1-(m*x1)
return m,c
In [2]:
m_c=learn(1,5,2,9)
print(m_c)
(4.0, 1.0)
Let’s code to predict the y using x (m and c)
In [3]:
def predict(x,m_c):
return (m_c[0]*x)+m_c[1]
In [4]:
predict (3,m_c)
Out[4]:
13.0
Here we are trying to draw the below line:
Let’s do it another way
we know:
- m = (y2-y1)/(x2-x1) :slope of the line
- c = y-m(x) :starting point of the line
- x = given :total length of line (x-0, as the value of x at starting point is 0)
- y = m(x) +c
lets say:
- m = (y2-y1)/(x2-x1) : slope of the line
- c = y1 : let starting point be y1
- total length of line = x-x1 (as value of x at the starting of point is x1)
- y = y1 + m(x-x1)
- So the formula will be: y = y1 + m(x-x1) (proof for Point slope form of straight line)
Here we are trying to draw the below line:
Let’s code to predict y using x (without c)
In [5]:
def Predict(x1,y1,x2,y2,x):
return y1 + ((y2-y1)/(x2-x1)*(x-x1))
In [6]:
Predict(1,5,2,9,3)
Out[6]:
13.0