Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessons
Go Further · ML-N-K1 · 25–35 MIN

Neural regression checkpoint

  1. Retrieval 1Retrieve and applyCurrent exercise

Outputs you create

search
  1. Fitted GridSearchCV for the declared parameter comparison.
cv_results
  1. Candidate validation evidence; comparison workflows use a dataframe indexed by model ID.
reference_results
  1. cross_validate result for the dummy reference; test_score contains five scores.
loss_curve
  1. Training loss history of the selected neural regressor.
residuals
  1. Training-only actual, predicted and actual-minus-predicted residual evidence.
final_rmse
  1. Root mean squared error in original target units.
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. Complete the Wine600 neural regression workflow with feature/target scaling, width selection, convergence evidence and original-unit errors.
searchcv_resultsreference_resultsloss_curveresidualsfinal_rmse
Hints

Think: Reconstruct both feature and target preparation inside every neural fit.

Tools: Pipeline, TransformedTargetRegressor, nested width search, loss curve and original-unit RMSE.

Approach: Compare against the mean reference, select width on training folds, inspect convergence and residuals, then predict through the fitted wrapper.

Explained solution
from sklearn.base import clone
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold, cross_validate, cross_val_predict, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer, TransformedTargetRegressor
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.dummy import DummyRegressor, DummyClassifier
from sklearn.metrics import root_mean_squared_error, f1_score, accuracy_score, confusion_matrix

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=0.2, random_state=42)
model = Pipeline([('prepare', StandardScaler()), ('model', TransformedTargetRegressor(regressor=MLPRegressor(hidden_layer_sizes=(24,), max_iter=800, early_stopping=True, tol=1e-3, random_state=42), transformer=StandardScaler()))])
folds = KFold(n_splits=5, shuffle=True, random_state=42)
cv_results = cross_validate(model, X_train, y_train, cv=folds, scoring='neg_root_mean_squared_error')
reference_results = cross_validate(DummyRegressor(strategy='mean'), X_train, y_train, cv=folds, scoring='neg_root_mean_squared_error')
search = GridSearchCV(model, {'model__regressor__hidden_layer_sizes':[(16,), (24,)]}, cv=folds, scoring='neg_root_mean_squared_error')
search.fit(X_train, y_train)
oof_predictions = cross_val_predict(search.best_estimator_, X_train, y_train, cv=folds)
residuals = y_train - oof_predictions
loss_curve = search.best_estimator_.named_steps['model'].regressor_.loss_curve_
final_model = clone(search.best_estimator_).fit(X_train, y_train)
final_predictions = final_model.predict(X_test)
final_rmse = root_mean_squared_error(y_test, final_predictions)
print('Final RMSE in quality-score units:', final_rmse)

Fold-local target scaling supports optimisation without leakage, and the wrapper reverses it so final errors retain quality-score units.

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.