5. Overfitting and Its Avoidance - 6bolin9/Data-Science-For-Business-Foster-Provost-and-Tom-Fawcett- GitHub Wiki
Generalization; Fitting and overfitting; Complexity control; Cross-validation; Attribute selection; Tree pruning; Regularization.
Generalization
Overfitting Model Example:
- Goal: Reduce customer churn.
- Provide historical data on customers who have stayed with the company and customers who have departed within six months of contract expiration
- Mine the data and build a model to distinguish customers who are likely to churn based on some features.
- Give back the code for the model, to implement in the company’s churn-reduction system.
- Evaluate the performance: check the performance of the model on the historical data.
Understand that historical performance is no guarantee of future success, but former experience tells you that churn patterns remain relatively stable, except for major changes to the industry (such as the introduction of the iPhone), and you know of no such major changes since these data were collected. So, the tech team runs the historical dataset through the model.
背后的方法是:save churned customers' features vector Tc. Then, in use, when the model is presented with a customer to determine the likelihood of churning, it takes the customer’s feature vector, looks her up in Tc, and reports “100% likelihood of churning” if she is in Tc and “0% likelihood of churning” if she is not in Tc.(There is never an overlap between the training and holdout sets)
This simple approach is a table model: memorizes the training data and performs no generalization.
What is the problem with this? The model does not generalize at all beyond the data that were used to build it.
Generalization and overfitting. Generalization is the property of a model or modeling process, whereby the model applies to data that were not used to build the model. It is tailored, or “fit,” perfectly to the training data. In fact, it is “overfit.”
Every dataset is a finite sample of a population. Make the models apply not just to the exact training set but to the general population from which the training data came. The training data were not representative of the true population, but that is not the problem here. The data were representative, but the data mining did not create a model that generalized beyond the training data.
Overfitting
Overfitting is the tendency of data mining procedures to tailor models to the training data, at the expense of generalization to previously unseen data points. The example from the previous section was contrived(人为的); the data mining built a model using pure memorization, the most extreme overfitting procedure possible. However, all data mining procedures have the tendency to overfit to some extent—some more than others. The idea is that if we look hard enough we will find patterns in a dataset.
As the Nobel Laureate Ronald Coase said, “If you torture the data long enough, it will confess.”
The answer is not to use a data mining procedure that doesn’t overfit because all of them do. Nor is the answer to simply use models that produce less overfitting, because there is a fundamental trade-off between model complexity and the possibility of overfitting.
Sometimes we may simply want more complex models because they will better capture the real complexities of the application and thereby be more accurate. There is no single choice or procedure that will eliminate overfitting. The best strategy is to recognize overfitting and to manage complexity in a principled way.
Overfitting Examined
When the model is not allowed to be complex enough, it is not very accurate. As the models get too complex, they look very accurate on the training data, but in fact, are overfitting—the training accuracy diverges from the holdout (generalization) accuracy.

Base Rate (or base rate performance)
Since the table model always predicts no churn for every new case with which it is presented, it will get every no churn case right and every churn case wrong. Thus the error rate will be the percentage of churn cases in the population. This is known as the base rate, and a classifier that always selects the majority class is called a base rate classifier.
A corresponding baseline for a regression model is a simple model that always predicts the mean or median value of the target variable.
OVERFITTING IN TREE INDUCTION
Overfitting: keep splitting on attributes and subdividing our data until we’re left with a single instance at each leaf node, which is pure by definition.
A procedure that grows trees until the leaves are pure tends to overfit. Tree-structured models are very flexible in what they can represent. Indeed, they can represent any function of the features, and if allowed to grow without bound they can fit it to arbitrary precision. But the trees may need to be huge in order to do so. The complexity of the tree lies in the number of nodes.

- For each tree size, we create a new tree from scratch, using the training data.
- Measure two values: its accuracy on the training set and its accuracy on the holdout (test) set.
- If the data subsets at the leaves are not pure, predict the target variable based on some average over the target values in the subset.
- As it is allowed more and more nodes it improves rapidly, and both training-set accuracy and holdout-set accuracy improve. Also, that training-set accuracy always is at least a little better than holdout-set accuracy.
- But at some point, the tree starts to overfit: it acquires details of the training set that are not characteristic of the population in general, as represented by the holdout set. sweet spot.
- As the trees are allowed to get larger, the training-set accuracy continues to increase—in fact, it is capable of memorizing the entire training set if we let it, leading to an accuracy of 1.0 (not shown).
- But the holdout accuracy declines as the tree grows past its “sweet spot”; the data subsets at the leaves get smaller and smaller, and the model generalizes from fewer and fewer data.
This represents the best trade-off between the extremes of (i) not splitting the data at all and simply using the average target value in the entire dataset, and (ii) building a complete tree out until the leaves are pure.
OVERFITTING IN MATHEMATICAL FUNCTIONS
- Change the function from being truly linear in the original attributes by adding new attributes that are nonlinear versions of original attributes. You cannot fit the dataset perfectly, you can fit it better and better with more dimensions with more attributes.
Modelers carefully prune the attributes in order to avoid overfitting. Careful manual attribute selection is a wise practice in cases where considerable human effort can be spent on modeling, and where there are reasonably few attributes. In many modern applications, where large numbers of models are built automatically, and/or where there are very large sets of attributes, manual selection may not be feasible. For example, companies that do data science-driven targeting of online display advertisements can build thousands of models each week, sometimes with millions of possible features. In such cases, there is no choice but to employ automatic feature selection (or to ignore feature selection).
Logistic VS SVM with new outliers
Add outliers that may not have a strong influence on the model because they contribute little to the “mass” of the rest examples.
- Yet in the case of logistic regression, if a linear boundary exists, logistic regression will find it, even if this means moving the boundary to accommodate outliers.
- The SVM tends to be less sensitive to individual examples. The SVM training procedure incorporates complexity control.
Why Is Overfitting Bad?
why overfitting often causes models to become worse. This section goes into a detailed example showing how this happens and why.
The short answer is that as a model gets more complex it is allowed to pick up harmful spurious correlations. These correlations are idiosyncracies of the specific training set used and do not represent characteristics of the population in general. The harm occurs when these spurious correlations produce incorrect generalizations in the model. This is what causes performance to decline when overfitting occurs.
Solutions: From Holdout Evaluation to Cross-Validation
Cross-validation is a more sophisticated holdout training and testing procedure.
Cross-validation makes better use of a limited dataset. Unlike splitting the data into one training and one holdout set, cross-validation computes its estimates over all the data by performing multiple splits and systematically swapping out samples for testing. 除此之外还要考虑的是train/test data是否能很现实中模型遇到的数据相吻合。我们收集到的数据是不是有某种假设前提会影响我们的sample不具有代表性。
- Cross-validation begins by splitting a labeled dataset into k partitions called folds.(k=5 or 10).
- Cross-validation then iterates training and testing k times.
- In each iteration of the cv, a different fold is chosen as the test data.
- In this iteration, the other k–1 folds are combined to form the training data.
- So, in each iteration, we have (k–1)/k of the data used for training and 1/k used for testing.
- Each iteration produces one model, and thereby one estimate of generalization performance or accuracy. When cv is finished, every example will have been used only once for testing but k–1 times for training. Finally, compute the average and standard deviation.
each piece is used in turn as the test set, with the other four used to train a model. The result is five different accuracy results, which then can be used to compute the average accuracy and its variance.
The Churn Dataset Revisited
- Entire dataset both for training and testing, and reported classification tree has an accuracy of 73%. Do you trust this number?
- The average accuracy of the 10 folds with classification trees is 68.6%. There was some overfitting occurring with the classification trees.
Tree and Logistic Models with K fold CV.
Certain commonalities in both graphs:
- Neither model type did very well on Fold Three and both performed well on Fold Ten.
- But there are definite differences between the two.
- logistic regression models show slightly lower average accuracy (64.1%) and with higher variation (standard deviation of 1.3) than the classification trees do.
- On this particular dataset, trees may be preferable to logistic regression because of their greater stability and performance. But this is not absolute; other datasets will produce different results.
Learning Curves
A plot of the generalization performance against the amount of training data is called a learning curve. The learning curve is another important analytical tool.
Learning curves for tree induction and logistic regression (telecommunications churn problem):
.
Learning Curves Shape
- They are steep initially as the modeling procedure finds the most apparent regularities in the dataset.
- Then as training on larger and larger datasets, it finds more accurate models. However, the marginal advantage of having more data decreases, so the learning curve becomes less steep. In some cases, the curve flattens out completely because the procedure can no longer improve accuracy even with more training data.
- learning curves VS fitting graphs (or fitting curves): A learning curve shows the generalization performance—the performance only on testing data, plotted against the amount of training data used.
- A fitting graph shows the generalization performance as well as the performance on the training data but plotted against model complexity. Fitting graphs generally are shown for a fixed amount of training data.
As the training size grows (x axis), generalization performance (y axis) improves. Importantly, the improvement rates are different for the two induction technique, and change over time. Logistic regression has less flexibility, which allows it to overfit less with small data, but keeps it from modeling the full complexity of the data. Tree induction is much more flexible, leading it to overfit more with small data, but to model more complex regularities with larger training sets.
- This means two things: for smaller data, tree induction will tend to overfit more. This leads logistic regression to perform better for smaller datasets (not always, though).
- On the other hand, the flexibility of tree induction can be an advantage with larger training sets: the tree can represent substantially nonlinear relationships between the features and the target.
- Whether the tree induction can actually capture those relationships needs to be evaluated empirically using an analytical tool such as learning curves.
Learning curves可以帮助判定是否还需要增加training数据量,以便优化investments。

.