1. Diagnosing Bias and Variance Problems
In this video, you'll learn how to diagnose bias and variance problems.
2. Estimating the Generalization Error
Given that you've trained a supervised machine learning model labeled fhat, how do you estimate the fhat's generalization error?
This cannot be done directly because:
- f is unknown,
- usually you only have one dataset,
- you don't have access to the error term due to noise.
3. Estimating the Generalization Error
A solution to this is to first split the data into a training and test set.
The model fhat can then be fit to the training set and its error can be evaluated on the test set.
The generalization error of fhat is roughly approximated by fhat's error on the test set.
4. Better Model Evaluation with Cross-Validation
Usually, the test set should be kept untouched until one is confident about fhat's performance. It should only be used to evaluate fhat's final performance or error.
Now, evaluating fhat's performance on the training set may produce an optimistic estimation of the error because fhat was already exposed to the training set when it was fit.
To obtain a reliable estimate of fhat's performance, you should use a technique called cross-validation or CV.
CV can be performed using K-Fold-CV or hold-out-CV . In this lesson, we'll only be explaining K-fold-CV.
5. K-Fold CV
The diagram here illustrates this technique for K=10:
- First, the training set (T) is split randomly into 10 partitions or folds,
- The error of fhat is evaluated 10 times on the 10 folds,
- Each time, one fold is picked for evaluation after training fhat on the other 9 folds.
- At the end, you'll obtain a list of 10 errors.
6. K-Fold CV
Finally, as shown in this formula, the CV-error is computed as the mean of the 10 obtained errors.
7. Diagnose Variance Problems
Once you have computed fhat's cross-validation-error, you can check if it is greater than fhat's training set error.
If it is greater, fhat is said to suffer from high variance.
In such case, fhat has overfit the training set.
To remedy this try decreasing fhat's complexity.
For example, in a decision tree you can reduce the maximum-tree-depth or increase the maximum-samples-per-leaf.
In addition, you may also gather more data to train fhat.
8. Diagnose Bias Problems
On the other hand, fhat is said to suffer from high bias if its cross-validation-error is roughly equal to the training error but much greater than the desired error.
In such case fhat underfits the training set. To remedy this try increasing the model's complexity or gather more relevant features for the problem.
9. K-Fold CV in sklearn on the Auto Dataset
Let's now see how we can perform K-fold-cross-validation using scikit-learn on the auto-dataset which is already loaded.
In addition to the usual imports, you should also import the function cross_val_score() from sklearn-dot-model_selection.
First, split the dataset into 70%-train and 30%-test using train_test_split().
Then, instantiate a DecisionTreeRegressor() dt with the parameters max_depth set to 4 and min_samples_leaf to 0-dot-14.
10. K-Fold CV in sklearn on the Auto Dataset
Next, call cross_val_score() by passing dt, X_train, y_train; set the parameters cv to 10 for 10-fold-cross-validation and scoring to neg_mean_squared_error to compute the negative-mean-squared-errors. The scoring parameter was set so because cross_val_score() does not allow computing the mean-squared-errors directly. Finally, set n_jobs to -1 to exploit all available CPUs in computation.
The result is a numpy-array of the 10 negative mean-squared-errors achieved on the 10-folds. You can multiply the result by minus-one to obtain an array of CV-MSE.
After that, fit dt to the training set and evaluate the labels of the training and test sets.
11. K-Fold CV in sklearn on the Auto Dataset
The CV-mean-squared-error can be determined as the mean of MSE_CV.
Finally, you can use the function MSE to evaluate the train and test set mean-squared-errors.
Given that the training set error is smaller than the CV-error, we can deduce that dt overfits the training set and that it suffers from high variance.
Notice how the CV and test set errors are roughly equal.
12. Let's practice!
Now it's your turn.