Sub topic 1
Parameters versus hyperparameters
Prerequisites
This chapter assumes you know basic Python and have trained a supervised model with scikit-learn or a similar library. You should also know what overfitting and underfitting mean. Later chapters build on the distinction made here.
Two kinds of numbers in a model
A model has two kinds of values that shape its behavior.
- Parameters are values the model learns from data during training. Examples are the coefficients of a linear regression, the weights of a neural network, and the split thresholds in a decision tree.
- Hyperparameters are settings you choose before training starts. The training algorithm does not change them. Examples are the regularization strength
C, the number of trees in a forest, and the maximum depth of a tree.
The test is simple. If fit() computes the value, it is a parameter. If you pass it to the constructor, it is a hyperparameter.
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True)
X = StandardScaler().fit_transform(X)
model = LogisticRegression(C=0.5, max_iter=1000) # hyperparameters: chosen by you
print("Before fit, has coef_:", hasattr(model, "coef_"))
model.fit(X, y) # parameters: learned here
print("After fit, coef_ shape:", model.coef_.shape)
print("Hyperparameters:", model.get_params()["C"], model.get_params()["max_iter"])In scikit-learn, learned parameters end in an underscore, such as coef_ and intercept_. They exist only after fit(). Hyperparameters appear in get_params() and can be changed with set_params().
Why the difference matters
Training can fit parameters by minimizing a loss on the training data. That works because the loss gives a direction in which to move. Hyperparameters usually have no such direction. A larger max_depth lowers the training loss almost every time, so minimizing training loss would always pick the deepest tree. That choice overfits.
For this reason you must judge hyperparameters on data the model did not train on. The following chapters cover how to do that with validation sets and cross-validation.
Common examples
| Model | Hyperparameters (you set) | Parameters (learned) |
|---|---|---|
| Logistic regression | `C`, `penalty`, `max_iter` | `coef_`, `intercept_` |
| Decision tree | `max_depth`, `min_samples_leaf` | Split features and thresholds |
| Random forest | `n_estimators`, `max_features` | The trees themselves |
| SVM with RBF kernel | `C`, `gamma` | Support vectors and their weights |
| Neural network | Learning rate, layer sizes, batch size | Weights and biases |
Common mistakes
- Treating defaults as tuned. Defaults are reasonable starting points. They are not optimal for your data.
- Tuning on the test set. If you pick hyperparameters by looking at test scores, the test score no longer measures generalization.
- Confusing the two in discussion. Saying "we trained the learning rate" is wrong when you set it by hand or by search. You tuned it. You trained the weights.
Exercise
A scikit-learn RandomForestClassifier is created with n_estimators=200. After fit(), you read feature_importances_. Which of the two is a hyperparameter and which is a learned value, and how can you tell?
Show answer
n_estimators is a hyperparameter. You passed it to the constructor before training, and fit() did not change it. feature_importances_ is learned. It ends in an underscore and exists only after fit().