Cost Function in Machine Learning Explained for Beginners
If you’re starting out with machine learning, one of the first concepts you’ll hear about is the cost function. But what does it really mean?
Think of the cost function as a way of measuring how “wrong” your model is. If the predictions are close to the actual answers, the cost will be small. If the predictions are far off, the cost will be large. In short, it’s the number that tells us whether the model is improving or not.
Here’s a simple example. Imagine throwing darts at a board:
The bullseye is the correct answer.
Each dart you throw is a prediction.
The distance from the bullseye is the cost.
The closer you get to the bullseye, the better you’re doing — and that’s exactly how cost functions work.
One of the most common formulas is the Mean Squared Error (MSE), often used in regression. It takes the difference between predicted and actual values, squares it, and averages it across all data points. The smaller the number, the better the model.
But cost functions do more than just measure performance. They guide optimisation. Algorithms like gradient descent use the cost function as a map, adjusting parameters step by step until the model reaches its best version.
If you’re exploring data science or machine learning courses, mastering cost functions is the first step to building reliable models that work in the real world.