Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessonsQUESTIONS · MODELS · EVIDENCE
Go Further · ML-N06 · 12–18 MIN

Capacity and regularisation

Interpret controlled width and alpha experiments.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeReason about the Python
  3. TransferExplain the Python result

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.

Interpret controlled width and alpha experiments.InputsHidden unitsOutputs
Schematic · Interpret controlled width and alpha experiments.Scroll the diagram horizontally if needed.

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_params
hidden_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.

LINE24 · first 8 prepared rows
distanceduration
15.30501
1.4782611.2361
1.9565211.8488
2.4347814.809
2.9130410.7589
3.391319.4972
3.8695717.89
4.3478315.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.

Input schema
ColumnStored type
distancefloat64
durationfloat64

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.

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.

layersalpha

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.