Today we’re going to introduce some terms that are important to machine learning:
- Variance
- r2 score
- Mean square error
We illustrate these concepts using scikit-learn.
(This article is part of our scikit-learn Guide. Use the right-hand menu to navigate.)
Why these terms are important
You need to understand these metrics in order to determine whether regression models are accurate or misleading. Following a flawed model is a bad idea, so it is important that you can quantify how accurate your model is. Understanding that is not so simple.
These first metrics are just a few of them. Other concepts, like bias and overtraining models, also yield misleading results and incorrect predictions.
(Learn more in Bias and Variance in Machine Learning.)
To provide examples, let’s use the code from our last blog post, and add additional logic. We’ll also introduce some randomness in the dependent variable (y) so that there is some error in our predictions. (Recall that, in the last blog post we made the independent y and dependent variables x perfectly correlate to illustrate the basics of how to do linear regression with scikit-learn.)
Getting started with AIOps is easy. Learn how you can manage escalating IT complexity with ease! ›
What is variance?
In terms of linear regression, variance is a measure of how far observed values differ from the average of predicted values, i.e., their difference from the predicted value mean. The goal is to have a value that is low. What low means is quantified by the r2 score (explained below).
In the code below, this is np.var(err), where err is an array of the differences between observed and predicted values and np.var() is the numpy array variance function.