Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessonsQUESTIONS · MODELS · EVIDENCE
Task workflows · ML-N04 · 18–25 MIN

Neural regression workflow

Keep feature scaling and target transformation distinct.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. PractiseAdapt the Python
  4. TransferAdapt the Python

Understand the idea

The feature scaler acts on X. TransformedTargetRegressor learns a separate transformation of y during each fit and inverse-transforms predictions automatically. Evaluate returned predictions in original target units.

Keep feature scaling and target transformation distinct.FeaturesHidden unitsOne quantityThe target wrapper returns predictions in original units.
Schematic · Keep feature scaling and target transformation distinct.Scroll the diagram horizontally if needed.

Python skill: Fits the target transformation inside each regressor fit and reverses it during prediction.

Meet the syntax

TransformedTargetRegressor(regressor=MLPRegressor(...), transformer=StandardScaler())
model__regressor__hidden_layer_sizes
TransformedTargetRegressor
Fits the target transformation inside each regressor fit and reverses it during prediction.
regressor=MLPRegressor(...)
Places neural regression inside the target wrapper.
transformer=StandardScaler()
Scales y within that wrapper; feature scaling belongs in the outer input pipeline.
model__regressor__hidden_layer_sizes
Addresses neural hidden widths through the pipeline’s target-wrapper step during search.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

model.fit(X_train,y_train)
answer=model.named_steps['model'].transformer_.mean_

This practice: Read and run the Python. Next: Change · Neural regression workflow.

Neural regression · from idea to workflow

Question: Can learned nonlinear combinations predict a quantity?

Mechanism: Hidden units transform scaled features; the target wrapper returns predictions in original units.

Watch for: Unstable optimisation and overfitting can dominate small datasets.

Interpret or debug · self-review: Check original target units and loss history, then compare a simple baseline on matching folds.

Independent full workflow: ML-X15. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.

Given data · Wine600

600 observations. One red or white wine sample. The dataframe df is supplied afresh for each Run.

Download source CSV · Source and original dictionary

Wine600 · first 8 prepared rows
fixed acidityvolatile aciditycitric acidresidual sugarchloridesfree sulfur dioxidetotal sulfur dioxidedensitypHsulphatesalcoholqualitywine_type
5.30.320.126.60.043221410.99373.360.610.46white
6.40.30.274.40.055171350.99253.230.4412.26white
6.50.220.322.20.02836920.990763.270.5911.97white
80.240.486.80.047131340.996163.230.7105white
8.90.290.351.90.06725570.9973.181.3610.36red
6.60.210.392.30.041311020.992213.220.5810.97white
8.80.240.542.50.08325570.99833.390.549.25red
5.40.150.322.50.03710510.988783.040.5812.66white

Column meanings and units

quality: ordered sensory score (0–10). alcohol: volume percent. pH: acidity scale. Other chemistry units follow the linked source dictionary.

The score is ordinal but modelled as regression here. ML removes exact duplicate rows before splitting. Chemistry must be available at prediction time; predictive associations do not establish effects of changing an ingredient.

Input schema
ColumnStored type
fixed acidityfloat64
volatile acidityfloat64
citric acidfloat64
residual sugarfloat64
chloridesfloat64
free sulfur dioxidefloat64
total sulfur dioxidefloat64
densityfloat64
pHfloat64
sulphatesfloat64
alcoholfloat64
qualityint64
wine_typestr
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
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.compose import TransformedTargetRegressor
from sklearn.neural_network import MLPRegressor
X=df.drop(columns=['quality','wine_type'])
y=df.quality
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42)
model=Pipeline([('scale',StandardScaler()),('model',TransformedTargetRegressor(regressor=MLPRegressor(hidden_layer_sizes=(24,),max_iter=800,early_stopping=True,tol=1e-3,random_state=42),transformer=StandardScaler()))])

Your task · Follow

Fit the supplied feature pipeline and target wrapper.

Hint 1 — Think

The target wrapper learns a separate transformation of y during fit.

Hint 2 — Tools

TransformedTargetRegressor and its fitted transformer_.

Hint 3 — Approach

Fit the full supplied recipe and inspect the target transformer’s learned mean.

Explained solution
model.fit(X_train,y_train)
answer=model.named_steps['model'].transformer_.mean_

The target scale is learned from training outcomes within the wrapper, rather than globally before validation.

Helpful prior knowledge: Convergence, early stopping and validation · Read residual patterns and limits 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 feature pipeline and target wrapper.

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.