Understand the idea
Width increases representational capacity. Alpha controls weight regularisation. Use controlled training/validation evidence to reason about both; production’s small width grid does not explore every possible network.
Python skill: Use set_params(), tuples and a float to describe capacity and regularisation before fitting.
Meet the syntax
hidden_layer_sizes=(16, 8)
alpha=0.01
model.set_paramshidden_layer_sizes=(16, 8)- Requests two hidden layers, with 16 units and then 8 units.
alpha=0.01- Sets the L2 weight penalty; its useful value needs validation evidence.
model.set_params- Changes estimator settings. Refit before evaluating the changed model.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
from sklearn.neural_network import MLPClassifier
model = MLPClassifier(hidden_layer_sizes=(24,), alpha=0.0001, random_state=42)
model.set_params(hidden_layer_sizes=(16, 8), alpha=0.01)
layers = model.hidden_layer_sizes
alpha = model.alpha
This practice: Read and run the Python. Next: Change · Capacity and regularisation.
Given data · LINE24
24 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
| distance | duration |
|---|---|
| 1 | 5.30501 |
| 1.47826 | 11.2361 |
| 1.95652 | 11.8488 |
| 2.43478 | 14.809 |
| 2.91304 | 10.7589 |
| 3.3913 | 19.4972 |
| 3.86957 | 17.89 |
| 4.34783 | 15.531 |
Column meanings and units
Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.
Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.
| Column | Stored type |
|---|---|
| distance | float64 |
| duration | float64 |
Your task · Follow
Create a network with one 24-unit hidden layer and alpha=0.0001. Change its hidden layers to (16, 8) and alpha to 0.01. Store the resulting settings in layers and alpha.
Hint 1 — Think
Use set_params(), tuples and a float to describe capacity and regularisation before fitting.
Hint 2 — Tools
Use hidden_layer_sizes=(16, 8), alpha=0.01, model.set_params. Read the visible syntax meanings before editing.
Hint 3 — Approach
Create a network with one 24-unit hidden layer and alpha=0.0001. Change its hidden layers to (16, 8) and alpha to 0.01. Store the resulting settings in layers and alpha. Keep the supplied row order and inspect the named output after running.
Explained solution
from sklearn.neural_network import MLPClassifier
model = MLPClassifier(hidden_layer_sizes=(24,), alpha=0.0001, random_state=42)
model.set_params(hidden_layer_sizes=(16, 8), alpha=0.01)
layers = model.hidden_layer_sizes
alpha = model.alpha
The new tuple changes the network capacity and alpha changes the penalty on large weights. These are candidate settings, not automatic improvements. Compare candidates on the same training folds and consider convergence and cost.
Helpful prior knowledge: Convergence, early stopping and validation These links are guidance, not locks.
Sources and API context
Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.