A cost function is a measure of how well a machine learning algorithm is performing. It is used to evaluate the performance of a model and determine how well it generalizes to unseen data. The cost function calculates the difference between the predicted output of the model and the actual output.

For example, the Mean Squared Error (MSE) cost function is used in linear regression to measure the difference between the predicted output and the actual output. The MSE cost function is calculated as the average of the squared differences between the predicted and actual output. The lower the MSE, the better the model is performing.