📚 The CoCalc Library - books, templates and other resources
License: OTHER
Validation and Model Selection
Credits: Forked from PyCon 2015 Scikit-learn Tutorial by Jake VanderPlas
In this section, we'll look at model evaluation and the tuning of hyperparameters, which are parameters that define the model.
Validating Models
One of the most important pieces of machine learning is model validation: that is, checking how well your model fits a given dataset. But there are some pitfalls you need to watch out for.
Consider the digits example we've been looking at previously. How might we check how well our model fits the data?
Let's fit a K-neighbors classifier
Now we'll use this classifier to predict labels for the data
Finally, we can check how well our prediction did:
It seems we have a perfect classifier!
Question: what's wrong with this?
Validation Sets
Above we made the mistake of testing our data on the same set of data that was used for training. This is not generally a good idea. If we optimize our estimator this way, we will tend to over-fit the data: that is, we learn the noise.
A better way to test a model is to use a hold-out set which doesn't enter the training. We've seen this before using scikit-learn's train/test split utility:
Now we train on the training data, and validate on the test data:
This gives us a more reliable estimate of how our model is doing.
The metric we're using here, comparing the number of matches to the total number of samples, is known as the accuracy score, and can be computed using the following routine:
This can also be computed directly from the model.score
method:
Using this, we can ask how this changes as we change the model parameters, in this case the number of neighbors:
We see that in this case, a small number of neighbors seems to be the best option.
Cross-Validation
One problem with validation sets is that you "lose" some of the data. Above, we've only used 3/4 of the data for the training, and used 1/4 for the validation. Another option is to use 2-fold cross-validation, where we split the sample in half and perform the validation twice:
Thus a two-fold cross-validation gives us two estimates of the score for that parameter.
Because this is a bit of a pain to do by hand, scikit-learn has a utility routine to help:
K-fold Cross-Validation
Here we've used 2-fold cross-validation. This is just one specialization of -fold cross-validation, where we split the data into chunks and perform fits, where each chunk gets a turn as the validation set. We can do this by changing the cv
parameter above. Let's do 10-fold cross-validation:
This gives us an even better idea of how well our model is doing.
Overfitting, Underfitting and Model Selection
Now that we've gone over the basics of validation, and cross-validation, it's time to go into even more depth regarding model selection.
The issues associated with validation and cross-validation are some of the most important aspects of the practice of machine learning. Selecting the optimal model for your data is vital, and is a piece of the problem that is not often appreciated by machine learning practitioners.
Of core importance is the following question:
If our estimator is underperforming, how should we move forward?
Use simpler or more complicated model?
Add more features to each observed data point?
Add more training samples?
The answer is often counter-intuitive. In particular, Sometimes using a more complicated model will give worse results. Also, Sometimes adding training data will not improve your results. The ability to determine what steps will improve your model is what separates the successful machine learning practitioners from the unsuccessful.
Illustration of the Bias-Variance Tradeoff
For this section, we'll work with a simple 1D regression problem. This will help us to easily visualize the data and the model, and the results generalize easily to higher-dimensional datasets. We'll explore a simple linear regression problem. This can be accomplished within scikit-learn with the sklearn.linear_model
module.
We'll create a simple nonlinear function that we'd like to fit
Now let's create a realization of this dataset:
Now say we want to perform a regression on this data. Let's use the built-in linear regression function to compute a fit:
We have fit a straight line to the data, but clearly this model is not a good choice. We say that this model is biased, or that it under-fits the data.
Let's try to improve this by creating a more complicated model. We can do this by adding degrees of freedom, and computing a polynomial regression over the inputs. Scikit-learn makes this easy with the PolynomialFeatures
preprocessor, which can be pipelined with a linear regression.
Let's make a convenience routine to do this:
Now we'll use this to fit a quadratic curve to the data.
This reduces the mean squared error, and makes a much better fit. What happens if we use an even higher-degree polynomial?
When we increase the degree to this extent, it's clear that the resulting fit is no longer reflecting the true underlying distribution, but is more sensitive to the noise in the training data. For this reason, we call it a high-variance model, and we say that it over-fits the data.
Just for fun, let's use IPython's interact capability (only in IPython 2.0+) to explore this interactively:
Detecting Over-fitting with Validation Curves
Clearly, computing the error on the training data is not enough (we saw this previously). As above, we can use cross-validation to get a better handle on how the model fit is working.
Let's do this here, again using the validation_curve
utility. To make things more clear, we'll use a slightly larger dataset:
Now let's plot the validation curves:
Notice the trend here, which is common for this type of plot.
For a small model complexity, the training error and validation error are very similar. This indicates that the model is under-fitting the data: it doesn't have enough complexity to represent the data. Another way of putting it is that this is a high-bias model.
As the model complexity grows, the training and validation scores diverge. This indicates that the model is over-fitting the data: it has so much flexibility, that it fits the noise rather than the underlying trend. Another way of putting it is that this is a high-variance model.
Note that the training score (nearly) always improves with model complexity. This is because a more complicated model can fit the noise better, so the model improves. The validation data generally has a sweet spot, which here is around 5 terms.
Here's our best-fit model according to the cross-validation:
Detecting Data Sufficiency with Learning Curves
As you might guess, the exact turning-point of the tradeoff between bias and variance is highly dependent on the number of training points used. Here we'll illustrate the use of learning curves, which display this property.
The idea is to plot the mean-squared-error for the training and test set as a function of Number of Training Points
Let's see what the learning curves look like for a linear model:
This shows a typical learning curve: for very few training points, there is a large separation between the training and test error, which indicates over-fitting. Given the same model, for a large number of training points, the training and testing errors converge, which indicates potential under-fitting.
As you add more data points, the training error will never increase, and the testing error will never decrease (why do you think this is?)
It is easy to see that, in this plot, if you'd like to reduce the MSE down to the nominal value of 1.0 (which is the magnitude of the scatter we put in when constructing the data), then adding more samples will never get you there. For , the two curves have converged and cannot move lower. What about for a larger value of ?
Here we see that by adding more model complexity, we've managed to lower the level of convergence to an rms error of 1.0!
What if we get even more complex?
For an even more complex model, we still converge, but the convergence only happens for large amounts of training data.
So we see the following:
you can cause the lines to converge by adding more points or by simplifying the model.
you can bring the convergence error down only by increasing the complexity of the model.
Thus these curves can give you hints about how you might improve a sub-optimal model. If the curves are already close together, you need more model complexity. If the curves are far apart, you might also improve the model by adding more data.
To make this more concrete, imagine some telescope data in which the results are not robust enough. You must think about whether to spend your valuable telescope time observing more objects to get a larger training set, or more attributes of each object in order to improve the model. The answer to this question has real consequences, and can be addressed using these metrics.
Summary
We've gone over several useful tools for model validation
The Training Score shows how well a model fits the data it was trained on. This is not a good indication of model effectiveness
The Validation Score shows how well a model fits hold-out data. The most effective method is some form of cross-validation, where multiple hold-out sets are used.
Validation Curves are a plot of validation score and training score as a function of model complexity:
when the two curves are close, it indicates underfitting
when the two curves are separated, it indicates overfitting
the "sweet spot" is in the middle
Learning Curves are a plot of the validation score and training score as a function of Number of training samples
when the curves are close, it indicates underfitting, and adding more data will not generally improve the estimator.
when the curves are far apart, it indicates overfitting, and adding more data may increase the effectiveness of the model.
These tools are powerful means of evaluating your model on your data.