Method · Statistics Library

Neural network (multilayer perceptron)

A multilayer perceptron predicts by combining predictors through one or two layers of learned nonlinear features, trained with the Adam optimizer, with cross-validated (never in-sample) metrics as the only honest measure of how good the resulting network actually is.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

A multilayer perceptron builds its own intermediate features rather than using your predictors directly. Each 'hidden unit' looks at a weighted combination of your predictors and bends it through a nonlinear function, effectively asking its own soft yes/no question. The final prediction combines many of these learned questions. With enough hidden units, a network like this can approximate almost any smooth relationship, which sounds powerful but is exactly why it must be evaluated with real caution.

The network learns by gradient descent: starting from random weights, it nudges every weight a tiny bit in whatever direction reduces its prediction error, thousands of times over. A 'loss curve' tracks this error during training: a healthy curve falls steeply then flattens; one still falling at the end means training was cut short; a jagged or rising curve means something (usually the learning rate) is off.

Because this kind of model has so many adjustable weights (often more than the number of rows in a typical dataset), it can memorize its training data almost perfectly while still being a poor predictor of new data, exactly the same over-fitting trap decision trees fall into, but usually worse. Being a 'black box' with no coefficients to inspect does not lower the bar of evidence needed; if anything it raises it, since you have given up interpretability and now owe a genuinely honest, cross-validated accounting of whether the extra complexity actually bought anything.

An MLP, short for multilayer perceptron, is a basic neural network: layers of simple units that each combine their inputs and pass the result on. An MLP is trained with the Adam optimizer, which adjusts the weights in many small steps to reduce the prediction error. Networks with many layers are what people mean by deep learning; an MLP with one or two hidden layers is the small, classic version.

Practitioner explanation

KernelStats fits 1 or 2 hidden layers via scikit-learn's MLPClassifier/MLPRegressor with the Adam optimizer; predictors are standardized by default INSIDE each cross-validation fold (no test-fold information leaks into the scaler), and for regression the OUTCOME itself is also standardized internally for optimization purposes, with predictions transformed back to original units before any metric is computed: Adam's default step size genuinely cannot reach a raw intercept of several thousand without this. Everything (weight initialization, Adam's shuffling, the CV split) is seeded (default 42) for exact reproducibility.

Regularization is explicit and named: an L2 penalty (option alpha) shrinks weights toward zero exactly like ridge regression, and the product frames architecture size (fewer hidden units/layers = fewer parameters to overfit with) and more data as the other two levers against this model's inherently high variance; with hundreds or thousands of trainable weights, an MLP's bias is tiny, so essentially ALL of its risk is variance, meaning it can fit noise as happily as signal without these controls. As with the tree-based models, the card headlines k-fold cross-validated performance and shows in-sample performance only as an explicit warning label, because this network typically has more trainable weights than most datasets have rows; near-100% in-sample accuracy is described as 'normal and meaningless' rather than a sign of quality.

The product states directly, as a matter of methodology rather than a footnote, that a network's Learn More content REQUIRES three things of any reported result: honest (cross-validated, not training) error, proof of convergence via the loss curve (an unconverged network is described as 'a random number generator with good marketing'), and a stated architecture (inputs-hidden-outputs and total parameter count) so a reader knows exactly how flexible the fitted model was. It also states plainly that on small-to-medium TABULAR data specifically, carefully tuned simpler models (regularized regression, tree ensembles) usually match or beat a neural network: deep learning's advantage is on images, text, and signals, not spreadsheets, and the product recommends reporting a simpler model whenever it gets within a whisker of the network's cross-validated score.

When to use it

Consider it when

The relationship between predictors and outcome may be smoothly non-linear and interactive, simpler models (regression, tree ensembles) have already been tried and hit a real cross-validated ceiling, and the dataset has at least a few hundred rows.

Use caution when

The dataset has only a small number of rows relative to the number of parameters (the parameter count will dwarf the data, and cross-validation will expose this), interpretability or individual-effect explanation is required for stakeholders or regulators, or a simpler model (linear/logistic regression, a tree ensemble) already achieves comparable cross-validated performance; the added complexity has earned nothing in that case.

Formulas and symbols

Multilayer perceptron forward pass

h=σ(W(1)x+b(1)),y^=g(W(2)h+b(2))h = \sigma(W^{(1)}x + b^{(1)}), \qquad \hat y = g(W^{(2)}h + b^{(2)})

sigma a nonlinear activation function (relu, tanh, or logistic); h the learned hidden-layer features; g the output activation.

L2 regularization

loss+α2n∑w2\text{loss} + \dfrac{\alpha}{2n} \sum w^2

alpha the regularization strength (scikit-learn scales it by 1/(2n), n the training rows), shrinking network weights toward zero like ridge regression's penalty; biases are not penalized.

Cross-validated performance

CV accuracy / RMSE, never the in-sample number\text{CV accuracy / RMSE, never the in-sample number}

With more trainable weights than rows in many practical cases, in-sample metrics are described explicitly as normal and meaningless.

Assumptions and checks

  • Gradient-descent training on unstandardized predictors that don't already share one scale is likely unstable and is the single most common cause of a badly-fit neural network.

    In KernelStats: Checked directly: the card reports whether predictors were standardized (inside each CV fold, avoiding test-fold leakage) and flags it as a warning when off.

  • An optimizer that stops before the loss plateaus (hitting the iteration cap without converging) has not actually found a fitted model; its weights, and every downstream number computed from them, describe an unfinished optimization, not a trained network.

    In KernelStats: Checked directly: the card reports whether Adam converged within max_iter (and across CV folds) and flags it as a warning otherwise.

  • With more trainable weights than training rows (or close to it), the network has the capacity to memorize the training data outright, which the cross-validated headline metrics are specifically reported to catch, but the in-sample numbers alone would hide.

    In KernelStats: Checked directly: the card compares the parameter count to n and flags it as a warning when the network has as many or more parameters than rows; every headline metric shown is itself cross-validated for this reason.

  • With a severely imbalanced classification target, cross-entropy training and overall accuracy can both be dominated by the majority class, understating how useful the model is for the minority class.

    In KernelStats: Checked directly for classification: the card reports the smallest class's share of rows and flags it below 10%.

Alternatives

  • Gradient Boosting (Trees). You want a flexible, high-accuracy model for tabular data that typically matches or beats a neural network on this kind of data with less tuning.
  • Logistic Regression. A simpler, interpretable model achieves comparable cross-validated performance: prefer it for the coefficients, intervals, and p-values it provides for free.

Worked example

Scenario

Classifying penguin species from four body measurements with the default network (one hidden layer of 64 relu units, seed 42), compared against linear discriminant analysis on the same predictors. (A multinomial logistic regression on these four predictors does not converge (the species are nearly separable), which is why LDA is the linear baseline here.)

Result

The 4-64-3 network has 515 trainable parameters for 342 rows (the card flags this); it reaches in-sample accuracy of 99.1% and 5-fold cross-validated accuracy of 98.8%. LDA reaches 98.5% cross-validated accuracy with a handful of interpretable discriminant coefficients.

Interpretation

The two models perform almost identically once judged honestly by cross-validation, despite the MLP having more parameters than rows, exactly the case where the simpler, interpretable linear model should be preferred, since the network's extra 500-odd parameters bought a third of a percentage point, well within fold-to-fold noise.

Dataset: penguins.csv

Common misconceptions

Misconception

A neural network's in-sample accuracy near 100% demonstrates it has learned the true underlying pattern.

Correction

A network with more trainable weights than the dataset has rows can memorize the training data almost perfectly regardless of whether it has learned anything generalizable: near-100% in-sample performance is normal and expected for a flexible model, not evidence of quality. Only cross-validated performance, on rows the network never saw during that fold's training, reflects genuine predictive ability.

Read more: Misconception: a model's training-set accuracy is its real-world performance

Misconception

Because a neural network gives no coefficients or p-values to scrutinize, it is exempt from the usual standards of statistical evidence.

Correction

Being a 'black box' RAISES the bar of evidence rather than lowering it: an honest result requires the cross-validated (not training) metric, proof the training loss curve actually converged, and a stated architecture (parameter count) so a reader knows how flexible the model was allowed to be. Skipping any of these leaves no way to distinguish a genuinely useful network from one that simply memorized noise.

References

  1. An Introduction to Statistical Learning with Applications in R (2nd ed.), Chapter 10, James, G., Witten, D., Hastie, T. and Tibshirani, R. (2021).
  2. Deep Learning, Goodfellow, I., Bengio, Y. and Courville, A. (2016).