Understand the idea
A scaled MLPClassifier uses the shared optimisation ideas for class prediction. Search a small width grid and compare with logistic regression on the same training folds. Complexity needs evidence.
Python skill: Creates one hidden layer with 24 units; the trailing comma makes a one-element tuple.
Meet the syntax
MLPClassifier(hidden_layer_sizes=(24,), max_iter=500, early_stopping=True, random_state=42)hidden_layer_sizes=(24,)- Creates one hidden layer with 24 units; the trailing comma makes a one-element tuple.
max_iter=500- Caps optimisation iterations.
early_stopping=True- Reserves an internal portion of each fitting population for stopping; outer validation still serves a separate role.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
model.fit(X_train,y_train)
answer=model.predict(X_test)
This practice: Read and run the Python. Next: Change · Neural classification workflow.
Neural classification · from idea to workflow
Question: Can learned nonlinear combinations improve class prediction?
Mechanism: Hidden units transform scaled inputs; an output layer represents class evidence.
Watch for: Convergence, scale, random initialisation and overfitting require diagnosis.
Interpret or debug · self-review: Inspect loss and validation evidence together. A falling training loss alone does not prove useful predictions.
Guided full workflow: Apply this model with a baseline, training validation and one final evaluation →
Independent full workflow: ML-X16. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.
Given data · penguins
333 observations. One measured penguin. The dataframe df is supplied afresh for each Run.
Download source CSV · Source and original dictionary
| island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | species |
|---|---|---|---|---|---|---|---|
| Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | Adelie |
| Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | Adelie |
| Torgersen | 40.3 | 18 | 195 | 3250 | female | 2007 | Adelie |
| Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 | Adelie |
| Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 | Adelie |
| Torgersen | 38.9 | 17.8 | 181 | 3625 | female | 2007 | Adelie |
| Torgersen | 39.2 | 19.6 | 195 | 4675 | male | 2007 | Adelie |
| Torgersen | 41.1 | 17.6 | 182 | 3200 | female | 2007 | Adelie |
Column meanings and units
Bill length/depth and flipper length: mm. Body mass: grams. Year and island: sampling context.
ML uses 333 complete cases. Removing incomplete records may change the represented population. Geographic context may not generalise to new islands.
| Column | Stored type |
|---|---|
| island | str |
| bill_length_mm | float64 |
| bill_depth_mm | float64 |
| flipper_length_mm | int64 |
| body_mass_g | int64 |
| sex | str |
| year | int64 |
| species | str |
Supplied setup · available if you need to inspect it
This code runs before your editor on every Run. These are the objects your exercise uses.
from sklearn.model_selection import train_test_split
numeric=['bill_length_mm','bill_depth_mm','flipper_length_mm','body_mass_g']
X=df[numeric]
y=df.species
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42,stratify=y)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
model=Pipeline([('scale',StandardScaler()),('model',MLPClassifier(hidden_layer_sizes=(24,),max_iter=500,early_stopping=True,random_state=42))])
Your task · Follow
Fit the supplied scaled neural classifier and predict labels.
Hint 1 — Think
The supplied pipeline keeps feature scaling attached to the neural classifier.
Hint 2 — Tools
Pipeline.fit and predict.
Hint 3 — Approach
Fit the training pair through the supplied recipe and predict held-away class labels.
Explained solution
model.fit(X_train,y_train)
answer=model.predict(X_test)
The pipeline learns preparation only from training rows and applies the same representation during classification.
Helpful prior knowledge: Convergence, early stopping and validation · A logistic workflow 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.