Does NN fit data better than logistic regression?
Multi-layer neural networks with non-linear activations can fit complex non-linear data far better than logistic regression, which is restricted to linear decision boundaries in input space (and is itself equivalent to a single-layer sigmoid network). However, on small, noisy, or linearly separable tabular datasets, logistic regression often generalizes better due to lower variance and convex optimization.
Study Card: Neural Networks (NN) vs. Logistic Regression
Direct Answer
Multi-layer neural networks (NNs) can fit non-linear, high-dimensional relationships better than logistic regression, though they do not always outperform logistic regression on unseen test data. Logistic regression applies a sigmoid to a single linear combination —mathematically equivalent to a single-layer neural network with no hidden layers—limiting it to linear hyperplanes unless features are manually transformed. By stacking hidden layers with non-linear activations (such as ReLU or GELU), multi-layer NNs act as universal function approximators, automatically learning hierarchical feature interactions. That flexibility comes with tradeoffs: NNs require more data, feature scaling, regularization, and non-convex optimization, so on small or near-linear tabular datasets, regularized logistic regression often generalizes just as well or better with lower variance.
Key Terms
- Linear Decision Boundary: A hyperplane separating classes in input space, which limits vanilla logistic regression.
- Non-Linear Activation Functions: Element-wise non-linearities (e.g., ReLU, tanh, GELU) that prevent stacked linear layers from collapsing into a single linear map.
- Universal Approximation Theorem: A feedforward NN with a single sufficiently wide hidden layer and non-polynomial activation can approximate any continuous function on a compact domain.
- Convexity vs. Non-Convexity: Logistic regression’s cross-entropy objective is convex (guaranteed global optimum), whereas multi-layer NNs have non-convex loss landscapes with many local optima and saddle points.
Example
Consider classifying raw images or non-linearly separable 2D coordinates such as interleaving half-moons. Logistic regression can only place a single straight cut through the input space, leaving curved regions misclassified regardless of training time. A multi-layer perceptron (MLP) with ReLU hidden layers bends multiple piecewise-linear hyperplanes into a smooth non-linear decision boundary around each moon. Conversely, if a spam filter already uses thousands of sparse TF-IDF bag-of-words features where classes separate almost linearly, L2-regularized logistic regression trains in seconds and resists overfitting better than an unregularized MLP.
Code Implementation
from sklearn.datasets import make_moons
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
# Generate a clearly non-linear dataset (two interleaving moons with noise)
X, y = make_moons(n_samples=1000, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Logistic Regression (linear boundary in input space)
logreg = make_pipeline(
StandardScaler(),
LogisticRegression(random_state=42, max_iter=1000)
)
logreg.fit(X_train, y_train)
logreg_accuracy = accuracy_score(y_test, logreg.predict(X_test))
print(f"Logistic Regression Test Accuracy: {logreg_accuracy:.4f}")
# Multi-Layer Perceptron Neural Network (non-linear decision boundary)
nn = make_pipeline(
StandardScaler(),
MLPClassifier(
hidden_layer_sizes=(64, 32),
activation='relu',
solver='adam',
max_iter=1000,
early_stopping=True,
validation_fraction=0.15,
n_iter_no_change=25,
random_state=42,
)
)
nn.fit(X_train, y_train)
nn_accuracy = accuracy_score(y_test, nn.predict(X_test))
print(f"Neural Network (MLP) Test Accuracy: {nn_accuracy:.4f}")
Related Concepts
- Kernel SVMs and Gradient Boosted Trees: For tabular data with non-linear interactions, Gradient Boosted Trees (XGBoost, LightGBM) and RBF SVMs often rival or outperform MLPs with less tuning.
- Feature Engineering + Basis Expansions: Adding polynomial or interaction features lets logistic regression represent non-linear boundaries in the original feature space while keeping a convex objective.
- Bias-Variance Tradeoff: Logistic regression has higher inductive bias (linearity) and lower variance; deep NNs have very low bias and rely on regularization (weight decay, dropout, early stopping) and ample data to control variance.
- Interpretability: Logistic regression weights directly reflect log-odds contributions, whereas deep NNs generally require post-hoc attribution tools (e.g., SHAP, integrated gradients).