Understanding the Cost function.

Venkata Surya Prakash
3 min readMar 20, 2022

--

The main aim of Cost function is to minimize the error rate in predicting the target values. As we know the final result of cost function mainly depends on what Θ values we choose initially.

Below is the cost function formula, its parameters needed and how its going to work on Hypothesis or regression line.

The goal of the cost function is, for what values of Θ it could vomit the predicted values which are neared to the actual target values with atmost error rate.

For instance lets try taking some randome values of Θ and lets see how its going to show a great impact on predicting the values.

if m=1,b=0 then cost funtion is absolutely zero, that means predicted value h(x) and actual values Y are both same.There is no error rate for this, this is predicting the values with 100% accuracy. we can see that regression line is passing through the actual target values with out having any deviation on any value.

Lets take other scenario for better understanding.

But , if m=0.5 ,b=0 then cost function predicts the target values with some error in it and this error is nothing but a “Blue line” from actual target values to the regression line.

But if both the values of Θ is Zero, then cost function predicts the target values with “Maximum error” in it and this error is nothing but a “Blue line” drawn from actual target values to X-axis.

As a result we can depict that; lesser the cost function, lesser will be the Error values, higher the accuracy of prediction and vise versa.

Finally, below image depicts that, if we keep on changing the Θ values, the position of the linear regression line changes until it gets the best fit with least error rate that gives you accurate predictions. As a result, the sea blue colour regression line gives the predictions with minimum error rate in it. So we call it as “Best Fit Regression Line” among the the other 3 regression lines.

Have you ever thought that taking Θ values manually is soo difficult for a human as it has infinite number of values for Θ. So for our convenience we use Gradient Descent cost function algorithm for automatic updation of Θ values.

--

--