Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessonsQUESTIONS · MODELS · EVIDENCE
Task workflows · ML-N03 · 25–40 MIN

Neural classification workflow

Apply class outputs and classification evidence to a small network.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferExplain the Python result
  4. ApplyBuild a guided full workflow

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.

Apply class outputs and classification evidence to a small network.InputsHidden unitsOutputs
Schematic · Apply class outputs and classification evidence to a small network.Scroll the diagram horizontally if needed.

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

penguins · first 8 prepared rows
islandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_gsexyearspecies
Torgersen39.118.71813750male2007Adelie
Torgersen39.517.41863800female2007Adelie
Torgersen40.3181953250female2007Adelie
Torgersen36.719.31933450female2007Adelie
Torgersen39.320.61903650male2007Adelie
Torgersen38.917.81813625female2007Adelie
Torgersen39.219.61954675male2007Adelie
Torgersen41.117.61823200female2007Adelie

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.

Input schema
ColumnStored type
islandstr
bill_length_mmfloat64
bill_depth_mmfloat64
flipper_length_mmint64
body_mass_gint64
sexstr
yearint64
speciesstr
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.

Your task · Follow

Fit the supplied scaled neural classifier and predict labels.

answer

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.