# How To Calculate Cost Function J(θ0, θ1) - Machine Learning

What is cost function: The cost function “J( θ01)” is used to measure how good a fit (measure the accuracy of hypothesis function) a line is to the data. If the line is a good fit, then your predictions will be far better. The idea is to minimize the value of J by calculating it from given values of  θ0 and θ1.

Line as good fit: The line we're trying to make as good a fit as possible is defined by equation “hθ(x)= θ0 + θ1x”.
So how to calculate cost function. Let see the steps

Step 1.  Pick the value of  θ0  and  θ1
Step 2. Find value of hθ(x)=   by using formula “hθ(x)= θ0 + θ1x”.
Step 3. Find  “hθ(x)-Y”    for all values of X
Step 4. Find  Square of (hθ(x) -Y) for all values of Y
Step 5. Substitute all values in equation   (1/2m Σ( h(x)-y)2)  which is equal to “J(θ0, θ1)”

Example:
Let’s consider we have m sample values.

 X Y 4 1 3 2 2 2 1 4

Total sample test data is 4 i.e. m=4

Example 1: Calculate value of J(θ0, θ1)

Step 1.    θ0=0 and θ1=1

 Step 2 Step 3 Step 4 X Y hθ(x) i.e. (θ0 + θ1x) hθ(x)-Y (hθ(x) - y )2 4 1 0+1*4 => 4 4-1 => 3 9 3 2 0+1*3 => 3 3-2 => 1 1 2 2 0+1*2 => 2 2-2 => 0 0 1 4 0+1*1 => 1 1-4 => -3 9

Step 5:  Substitute value in {1/2m Σ( h(x)-y)2}
m=6 ;   h(x)-y)2 is calculated, we have to du summation

J(θ0, θ1) = (1/(2*4))* (9+1+0+9) = (1/8 )*19 = 2.375
So  J(θ0, θ1) = 2.375 for θ0=0 and θ1=1

You can pick other values of θ0 and θ1 but that we will discuss in other post

##### 1 comment:
1. 