Sub topic 1
Parameters versus Hyperparameters
Prerequisites
Assume you know basic Python, supervised learning, and the difference between overfitting and underfitting. You should also have used scikit-learn or a similar library.
Parameters and hyperparameters
A parameter is a value a model learns from training data. In linear regression, the learned weights are parameters. In a decision tree, the chosen split rules are parameters.
A hyperparameter is a setting you choose before training. It controls how the model learns. Examples include a tree's maximum depth, an SVM's C, and a neural network's learning rate.
| Type | Chosen by | Example |
|---|---|---|
| Parameter | The training algorithm | Linear model weight |
| Hyperparameter | You or a search procedure | Tree depth |
Why settings matter
Hyperparameters affect generalization, which is how well a model predicts new data. A model with too much flexibility can memorize training examples. This is overfitting. It has low training error but poor performance on new examples.
A model with too little flexibility cannot learn useful patterns. This is underfitting. It performs poorly on both training and new data. Tuning seeks a setting that balances these errors.
Choosing the metric you optimize
Choose a metric that matches the real cost of mistakes. Accuracy may be suitable when classes are balanced and errors have similar costs. For rare positive cases, precision, recall, or area under the precision-recall curve may be more useful. For regression, common choices include mean absolute error and root mean squared error.
Use the same metric for model selection and reporting unless you have a clear reason to use different metrics.
Train, validation, and test splits
Use the training set to learn parameters. Use a separate validation set to compare hyperparameter settings. Keep the test set untouched until the final evaluation.
The test score estimates performance after all choices are complete. Repeatedly checking the test set turns it into another validation set and can make the score optimistic.
Cross-validation for comparing settings
When data is limited, cross-validation repeatedly divides the training data into folds. In five-fold cross-validation, four folds train the model and one fold evaluates it. This repeats five times, with each fold used for evaluation once. Compare settings using the mean validation score.
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
model = make_pipeline(StandardScaler(), SVC(C=1, gamma="scale"))
scores = cross_val_score(model, X_train, y_train, cv=5, scoring="accuracy")
print(scores.mean())Cross-validation reduces dependence on one arbitrary validation split. It does not replace a final untouched test set.
Exercise
Why should the test set not choose hyperparameters?
Show answer
Reusing it for choices makes its score optimistic because the choices have adapted to that data.