Lesson 23 of 27By Simplilearn

Last updated on Apr 2, 20213873#### Machine Learning Tutorial: A Step-by-Step Guide for Beginners

Overview#### What is Machine Learning and How Does It Work?

Lesson - 1#### Random Forest Algorithm

Lesson - 2#### The Ultimate Guide to Cross-Validation in Machine Learning

Lesson - 3#### How to Leverage KNN Algorithm in Machine Learning?

Lesson - 4#### Everything You Need to Know About Classification in Machine Learning

Lesson - 5#### Top 34 Machine Learning Interview Questions and Answers in 2021

Lesson - 6#### PCA in Machine Learning - Your Complete Guide to Principal Component Analysis

Lesson - 7#### Top 10 Machine Learning Applications in 2020

Lesson - 8#### The Best Guide On How To Implement Decision Tree In Python

Lesson - 9#### Supervised and Unsupervised Learning in Machine Learning

Lesson - 10#### What Is Reinforcement Learning? The Best Guide To Reinforcement Learning

Lesson - 11#### The Best Guide to Confusion Matrix

Lesson - 12#### Understanding Naive Bayes Classifier

Lesson - 13#### Machine Learning Tutorial

Lesson - 14#### Linear Regression in Python

Lesson - 15#### How to Become a Machine Learning Engineer?

Lesson - 16#### An Introduction to Logistic Regression in Python

Lesson - 17#### What Is Q-Learning? The Best Guide to Understand Q-Learning

Lesson - 18#### An Introduction to the Types Of Machine Learning

Lesson - 19#### Everything You Need to Know About Feature Selection

Lesson - 20#### The Best Guide to Regularization in Machine Learning

Lesson - 21#### Everything You Need to Know About Bias and Variance

Lesson - 22#### What is Cost Function in Machine Learning

Lesson - 23#### Embarking on a Machine Learning Career? Here’s All You Need to Know

Lesson - 24#### A One-Stop Guide to Statistics for Machine Learning

Lesson - 25#### Mathematics for Machine Learning - Important Skills You Must Possess

Lesson - 26#### K-Means Clustering Algorithm: Applications, Types, Demos and Use Cases

Lesson - 27

Machine Learning models require a high level of accuracy to work in the actual world. But how do you calculate how wrong or right your model is? This is where the cost function comes into the picture. A machine learning parameter that is used for correctly judging the model, cost functions are important to understand to know how well the model has estimated the relationship between your input and output parameters.

In this ‘Cost Function in Machine Learning’ article, you will learn all that you need to know about cost function! The topics covered in this article are :

- What is Cost Function in Machine Learning?
- What is Gradient Descent?
- What is the Cost Function for Linear Regression?
- What is the Cost Function for Neural Networks?
- How to Implement Cost Functions in Python?

After training your model, you need to see how well your model is performing. While accuracy functions tell you how well the model is performing, they do not provide you with an insight on how to better improve them. Hence, you need a correctional function that can help you compute when the model is the most accurate, as you need to hit that small spot between an undertrained model and an overtrained model.

A Cost Function is used to measure just how wrong the model is in finding a relation between the input and output. It tells you how badly your model is behaving/predicting

Consider a robot trained to stack boxes in a factory. The robot might have to consider certain changeable parameters, called Variables, which influence how it performs. Let’s say the robot comes across an obstacle, like a rock. The robot might bump into the rock and realize that it is not the correct action.

It will learn from this, and next time it will learn to avoid rocks. Hence, your machine uses variables to better fit the data. The outcome of all these obstacles will further optimize the robot and help it perform better. It will generalize and learn to avoid obstacles in general, say like a fire that might have broken out. The outcome acts as a cost function, which helps you optimize the variable, to get the best variables and fit for the model.

Figure 1: Robot learning to avoid obstacles

Gradient Descent is an algorithm that is used to optimize the cost function or the error of the model. It is used to find the minimum value of error possible in your model.

Gradient Descent can be thought of as the direction you have to take to reach the least possible error. The error in your model can be different at different points, and you have to find the quickest way to minimize it, to prevent resource wastage.

Gradient Descent can be visualized as a ball rolling down a hill. Here, the ball will roll to the lowest point on the hill. It can take this point as the point where the error is least as for any model, the error will be minimum at one point and will increase again after that.

In gradient descent, you find the error in your model for different values of input variables. This is repeated, and soon you see that the error values keep getting smaller and smaller. Soon you’ll arrive at the values for variables when the error is the least, and the cost function is optimized.

Figure 2: Gradient Descent

A Linear Regression model uses a straight line to fit the model. This is done using the equation for a straight line as shown :

Figure 3: Linear regression function

In the equation, you can see that two entities can have changeable values (variable) a, which is the point at which the line intercepts the x-axis, and b, which is how steep the line will be, or slope.

At first, if the variables are not properly optimized, you get a line that might not properly fit the model. As you optimize the values of the model, for some variables, you will get the perfect fit. The perfect fit will be a straight line running through most of the data points while ignoring the noise and outliers. A properly fit Linear Regression model looks as shown below :

Figure 4: Linear regression graph

For the Linear regression model, the cost function will be the minimum of the Root Mean Squared Error of the model, obtained by subtracting the predicted values from actual values. The cost function will be the minimum of these error values.

Figure 5: Linear regression cost function

By the definition of gradient descent, you have to find the direction in which the error decreases constantly. This can be done by finding the difference between errors. The small difference between errors can be obtained by differentiating the cost function and subtracting it from the previous gradient descent to move down the slope.

Figure 6: Linear regression gradient descent function

After substituting the value of the cost function (J) in the above equation, you get :

Figure 7: Linear regression gradient descent function simplified

In the above equations, a is known as the learning rate. It decides how fast you move down the slope. If alpha is large, you take big steps, and if it is small; you take small steps. If alpha is too large, you can entirely miss the least error point and our results will not be accurate. If it is too small it will take too long to optimize the model and you will also waste computational power. Hence you need to choose an optimal value of alpha.

Figure 8: (a) Large learning rate, (b) Small learning rate, (c) Optimum learning rate

A neural network is a machine learning algorithm that takes in multiple inputs, runs them through an algorithm, and essentially sums the output of the different algorithms to get the final output.

The cost function of a neural network will be the sum of errors in each layer. This is done by finding the error at each layer first and then summing the individual error to get the total error. In the end, it can represent a neural network with cost function optimization as :

Figure 9: Neural network with the error function

For neural networks, each layer will have a cost function, and each cost function will have its own least minimum error value. Depending on where you start, you can arrive at a unique value for the minimum error. You need to find the minimum value out of all local minima. This value is called the global minima.

Figure 10: Cost function graph for Neural Networks

The cost function for neural networks is given as :

Figure 11: Cost function for Neural Networks

Gradient descent is just the differentiation of the cost function. It is given as :

Figure 12: Gradient descent for Neural Networks

You have looked at what a cost function is and the formulae required to find the cost function for different algorithms. Now let’s implement cost functions using Python. For this, you must take a numpy array of random numbers as our data.

Start by importing important modules.

Figure 13: Importing necessary modules

Now, let’s load up the data.

Figure 14: Importing data

The numpy array is a 2-D array with random points. Each element of the array corresponds to an x and y coordinate. Here, x is the input and y is the output required. Let’s separate these points and plot them.

Figure 15: Plotting the data

Now, let's set our theta value and store the y values in a different array so we can predict the x values.

Figure 16: Setting theta values and separating x and y

Let’s initialize the ‘m’ and ‘b’ values along with the learning rate.

Figure 17: Setting learning parameters

Using mathematical operations, find the cost function value for our inputs.

Figure 18: Finding cost function

Using the cost function, you can update the theta value.

Figure 19: Updating theta value

Now, find the gradient descent and print the updated value of theta at every iteration.

Figure 20: Finding gradient descent

On plotting the gradient descent, you can see the decrease in the loss at each iteration.

Figure 21: Plotting gradient descent

Enhance your skill set and give a boost to your career with the Post Graduate Program in AI and Machine Learning.

In this article titled ‘Cost Function in Machine Learning: The important parameter you must know about’, you saw the important machine learning parameter, the cost function, and tell you why it is important. Then you explored the gradient descent, which can be used to optimize the cost function. You then look at cost functions for linear regression and neural networks. Finally, you saw how the cost functions in machine learning can be implemented from scratch in Python.

We hope this article taught you all that you need to know about cost functions in machine learning. If you are serious about building a career in machine learning, the Post Graduate Program in AI and Machine Learning from Simplilearn should be your next step. Rated #1 AI and ML course by TechGig and delivered in partnership with Purdue & in collaboration with IBM, this bootcamp program will fulfill all your learning needs and equip you with the most in-demand skills you need to become a machine learning expert today.

Do you have any doubts or questions for us? Mention them in this article's comments section, and we'll have our experts answer them for you at the earliest!

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

Post Graduate Program in AI and Machine Learning

1786 Learners

Lifetime Access*

Machine Learning

18082 Learners

Lifetime Access*

*Lifetime access to high-quality, self-paced e-learning content.

- Video Tutorial
Linear Regression in Python

- Ebook
Machine Learning Career Guide: A complete playbook to becoming a Machine Learning Engineer

- Article
What Are Excel LOOKUP Functions? The Best Introduction.

- Video Tutorial
Machine Learning Tutorial

- Video Tutorial
The Best Guide to Regularization in Machine Learning

- Ebook
Introduction to Machine Learning: A Beginner's Guide

prevNext

- Disclaimer
- PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.