Sub topic 6
Common hyperparameters and what they control
This chapter assumes you can train and score a scikit-learn model, and that you know the difference between a learned parameter and a hyperparameter. It also assumes you know what overfitting and underfitting look like. Here you will learn which settings matter for common model families, and which direction each one pushes the fit.
Two jobs a hyperparameter can do
Most hyperparameters do one of two jobs.
- Capacity settings decide how complex a function the model can represent. Examples are tree depth, the number of hidden units, or the number of neighbors.
- Regularization settings penalize or limit complexity so the model cannot chase noise. Examples are the L2 penalty strength and the minimum number of samples in a leaf.
Some settings do neither. Learning rate and number of iterations control how the optimizer gets to a solution, not what the model can represent. They still affect the result, and they often interact with each other.
A reference by model family
| Model | Key hyperparameters | What they control |
|---|---|---|
| Logistic regression, SVM | `C` | Inverse regularization strength. Smaller `C` means a simpler model. |
| RBF SVM | `gamma` | Reach of each training point. Large values give wiggly boundaries. |
| k-nearest neighbors | `n_neighbors` | Small k gives a flexible, noisy fit. Large k smooths it. |
| Decision tree | `max_depth`, `min_samples_leaf` | Tree size. Deep trees overfit. |
| Random forest | `n_estimators`, `max_features`, `max_depth` | Number of trees, and how different the trees are from each other. |
| Gradient boosting | `learning_rate`, `n_estimators`, `max_depth` | Step size per tree, number of steps, and the complexity of each tree. |
| Neural network | learning rate, weight decay, layer sizes | Step size, penalty on large weights, and capacity. |
Note the direction of C. In scikit-learn, C is the inverse of regularization strength, while alpha in Ridge and Lasso works the other way around. Check the documentation before you decide which direction to search.
Seeing the effect
The loop below varies one capacity setting and reports the cross-validated accuracy. A very shallow tree underfits, and an unlimited tree tends to fit the training folds too closely.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
for depth in [1, 3, 5, None]:
model = DecisionTreeClassifier(max_depth=depth, random_state=0)
scores = cross_val_score(model, X, y, cv=5)
print(depth, round(scores.mean(), 3))Run it and compare the four scores. The best depth is usually in the middle, not at either end.
Interactions and priorities
Hyperparameters rarely act alone. In gradient boosting, a lower learning_rate needs more n_estimators to reach the same fit. In an RBF SVM, C and gamma both change how flexible the boundary is, so a good value for one depends on the other. This is why tuning one setting at a time can miss the best combination.
You also do not need to tune everything. A practical order:
- Start with the settings that change capacity the most, such as
max_depthfor trees orCfor SVMs. - Add regularization settings next.
- Leave defaults for settings that rarely matter, until the budget allows more.
Exercise
A gradient boosting model overfits. You lower learning_rate from 0.3 to 0.03 and keep n_estimators at 100. The score drops. What is a likely cause, and what would you change?
Show answer
With a smaller step size, 100 trees may not be enough to fit the data, so the model now underfits. Increase n_estimators (or use early stopping to choose it), or raise learning_rate somewhat. The two settings need to be tuned together.