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.
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_sizesTransformedTargetRegressor- 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
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | quality | wine_type |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5.3 | 0.32 | 0.12 | 6.6 | 0.043 | 22 | 141 | 0.9937 | 3.36 | 0.6 | 10.4 | 6 | white |
| 6.4 | 0.3 | 0.27 | 4.4 | 0.055 | 17 | 135 | 0.9925 | 3.23 | 0.44 | 12.2 | 6 | white |
| 6.5 | 0.22 | 0.32 | 2.2 | 0.028 | 36 | 92 | 0.99076 | 3.27 | 0.59 | 11.9 | 7 | white |
| 8 | 0.24 | 0.48 | 6.8 | 0.047 | 13 | 134 | 0.99616 | 3.23 | 0.7 | 10 | 5 | white |
| 8.9 | 0.29 | 0.35 | 1.9 | 0.067 | 25 | 57 | 0.997 | 3.18 | 1.36 | 10.3 | 6 | red |
| 6.6 | 0.21 | 0.39 | 2.3 | 0.041 | 31 | 102 | 0.99221 | 3.22 | 0.58 | 10.9 | 7 | white |
| 8.8 | 0.24 | 0.54 | 2.5 | 0.083 | 25 | 57 | 0.9983 | 3.39 | 0.54 | 9.2 | 5 | red |
| 5.4 | 0.15 | 0.32 | 2.5 | 0.037 | 10 | 51 | 0.98878 | 3.04 | 0.58 | 12.6 | 6 | white |
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.
| Column | Stored type |
|---|---|
| fixed acidity | float64 |
| volatile acidity | float64 |
| citric acid | float64 |
| residual sugar | float64 |
| chlorides | float64 |
| free sulfur dioxide | float64 |
| total sulfur dioxide | float64 |
| density | float64 |
| pH | float64 |
| sulphates | float64 |
| alcohol | float64 |
| quality | int64 |
| wine_type | 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
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.