Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessons
Task workflows · ML-N04 · 25–40 MIN

Neural regression workflow

Keep feature scaling and target transformation distinct.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. PractiseAdapt the Python
  4. TransferAdapt the Python
  5. ApplyBuild a guided full workflow

Understand the idea

Two different transformations serve two different roles. The outer StandardScaler prepares input measurements X. TransformedTargetRegressor learns a separate scale for y within each fitting population, then reverses that target scale automatically when predict returns. Wine quality predictions and RMSE use quality-score points; the Apply electricity workflow uses kWh.

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

What the syntax does

model.named_steps['model']
  • The model step is the target wrapper.
  • After model.fit, transformer_ is its fitted target scaler; mean_ records the fitting target mean.
  • This is separate from the outer input scaler.
  • The prepare step contains fitted preparation; model contains the prediction estimator.
  • A ColumnTransformer also exposes its named_transformers_ when inspecting encoded categories.
TransformedTargetRegressor(regressor=MLPRegressor(...), transformer=StandardScaler())
  • Transforms outcomes for fitting and returns predictions in the original units.
  • transformer acts on the target; regressor names the prediction recipe.
  • Keep both transformations within every fit.
  • Wine predictions return quality-score points; household predictions return kWh.
  • Compare actual and predicted values in the same named unit.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df.drop(columns=['quality', 'wine_type'])
y
Target series, aligned with X.
y = df.quality
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model
An unfitted recipe is supplied; construction has not learned parameters. Fit it here only when the task requests fitting.
model = Pipeline([('scale', StandardScaler()), ('model', TransformedTargetRegressor(regressor=MLPRegressor(hidden_layer_sizes=(24,), max_iter=800, early_stopping=True, tol=0.001, random_state=42), transformer=StandardScaler()))])

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
Wine600 · first 8 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
Your task
  1. Fit the supplied feature pipeline and target wrapper.
answer
Hints

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

Tools: TransformedTargetRegressor and its fitted transformer_.

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.

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.