Loss functions in Python are an integral part of any machine learning model. These functions tell us how much the predicted output of the model differs from the actual output.
There are multiple ways of calculating this difference. In this tutorial, we are going to look at some of the more popular loss functions.
We are going to discuss the following four loss functions in this tutorial.
Out of these 4 loss functions, the first three are applicable to regressions and the last one is applicable in the case of classification models.
Let’s look at how to implement these loss functions in Python.
Mean square error (MSE) is calculated as the average of the square of the difference between predictions and actual observations. Mathematically we can represent it as follows :
Python implementation for MSE is as follows :
Output :
You can also use mean_squared_error from sklearn to calculate MSE. Here’s how the function works:
Output :
Root Mean square error (RMSE) is calculated as the square root of Mean Square error. Mathematically we can represent it as follows :
Python implementation for RMSE is as follows:
Output :
You can use mean_squared_error from sklearn to calculate RMSE as well. Let’s see how to implement the RMSE using the same function:
Output :
If the parameter ‘squared’ is set to True then the function returns MSE value. If set to False, the function returns RMSE value.
Mean Absolute Error (MAE) is calculated as the average of the absolute difference between predictions and actual observations. Mathematically we can represent it as follows :
Python implementation for MAE is as follows :
Output :
You can also use mean_absolute_error from sklearn to calculate MAE.
Output :
Cross-Entropy Loss is also known as the Negative Log Likelihood. This is most commonly used for classification problems. A classification problem is one where you classify an example as belonging to one of more than two classes.
Let’s see how to calculate the error in case of a binary classification problem.
Let’s consider a classification problem where the model is trying to classify between a dog and a cat.
The python code for finding the error is given below.
Output :
We are using the log_loss method from sklearn.
The first argument in the function call is the list of correct class labels for each input. The second argument is a list of probabilities as predicted by the model.
The probabilities are in the following format :
This tutorial was about Loss functions in Python. We covered different loss functions for both regression and classification problems. Hope you had fun learning wiht us!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.