Sub topic 7
Defining a search space: ranges, types, and scales
This chapter assumes you know basic Python, can train and score a scikit-learn model, and understand overfitting. A search space is the set of hyperparameter values a tuner is allowed to try. Grid search, random search, and Bayesian methods all depend on it. A poor space wastes your budget no matter which method you choose.
Three decisions per hyperparameter
For each hyperparameter, decide its type, its range, and its scale.
- Type is integer, float, or categorical.
max_depthis an integer.Cis a float.kernelis categorical. - Range is the lowest and highest value you will accept.
- Scale is how values are spread across the range, either linear or logarithmic.
Choosing the scale
Some hyperparameters matter in proportion, not in absolute amounts. Going from C=0.01 to C=0.02 changes the model about as much as going from C=1 to C=2. Going from C=1 to C=1.01 changes almost nothing. Learning rates, regularization strengths, and the SVM gamma behave this way.
A log scale spreads samples evenly across orders of magnitude. On a linear scale from 0.0001 to 100, about 90% of random draws land above 10. Most of the useful region below 1 would get almost no samples.
| Hyperparameter | Typical type | Typical scale |
|---|---|---|
| Learning rate | float | log |
| `C`, `alpha`, `lambda` | float | log |
| `gamma` | float | log |
| `n_estimators` | integer | linear |
| `max_depth` | integer | linear |
| `subsample`, `colsample_bytree` | float | linear |
| `kernel`, `penalty` | categorical | none |
Defining spaces in scikit-learn
For grid search, you list explicit values. Build log-spaced lists with numpy.logspace. For random search, you pass distributions. Use loguniform for log-scaled floats and randint for integers.
import numpy as np
from scipy.stats import loguniform, randint
grid_space = {
"svc__C": np.logspace(-2, 2, 5), # 0.01, 0.1, 1, 10, 100
"svc__gamma": np.logspace(-4, 0, 5), # 0.0001 ... 1
"svc__kernel": ["rbf"],
}
random_space = {
"svc__C": loguniform(1e-2, 1e2),
"svc__gamma": loguniform(1e-4, 1e0),
"svc__kernel": ["rbf", "sigmoid"],
}
forest_space = {
"n_estimators": randint(50, 301), # upper bound is exclusive
"max_depth": randint(2, 17),
}
print(grid_space["svc__C"])
print(loguniform(1e-2, 1e2).rvs(5, random_state=0))
print(randint(2, 17).rvs(5, random_state=0))The svc__ prefix is the step name that make_pipeline gives to SVC. It lets the search reach inside a pipeline.
Setting ranges
- Start with the library defaults or published values as a center point.
- Go wide on the first pass. Cover two to four orders of magnitude for log-scaled values.
- Look at the results. If the best value sits at the edge of a range, the true optimum may lie outside it. Extend the range and rerun.
- Narrow the range around good regions only after the wide pass.
Common mistakes
- Using a linear scale for
C,gamma, or learning rate. - Searching dependent values as if they were independent. A learning rate and
n_estimatorstrade off, so a very low learning rate needs many more trees. - Including hyperparameters that do not apply together, such as
gammawith a linear kernel. These waste trials. - Making the space too large. Each added dimension multiplies the grid size and dilutes random samples.
- Forgetting that
randintexcludes its upper bound.
Exercise
You are tuning a gradient boosting model. The learning rate should range from 0.001 to 1, and max_depth from 2 to 10. Which scipy distributions would you use for random search, and why?
Show answer
Use loguniform(1e-3, 1) for the learning rate, because its useful values span three orders of magnitude and a log scale samples each decade equally. Use randint(2, 11) for max_depth, because it is an integer and the upper bound of randint is exclusive.
Exercise
A random search over C in loguniform(0.01, 10) returns a best value of 9.8. What should you do next?
Show answer
The best value sits at the upper edge of the range, so the optimum may be higher. Extend the upper bound, for example to 1000, and rerun the search.