VC Dimension and the Capacity of a Classifier
Understand shattering, work out the VC dimension of simple classifiers, and see what it does and does not say about generalization.
Key takeaways
- A class shatters a set of points if, for every assignment of two labels to them, some classifier in the class produces it.
- VC dimension is the size of the largest shattered set; one such set is enough, and not every set of that size need be shattered.
- Linear classifiers with a bias term in d dimensions have VC dimension d + 1.
- Parameter count and VC dimension often move together but can differ dramatically.
- Finite VC dimension guarantees learnability in the PAC sense, though the resulting numbers are often loose.
The VC dimension of a set of classifiers is the size of the largest set of points that the set can label in every possible way. It measures capacity: how rich a family of decision rules is, regardless of how many rules it contains. A family with finite VC dimension can be learned from a finite sample, whatever the data distribution.
The name comes from Vladimir Vapnik and Alexey Chervonenkis, who developed the idea in the Soviet Union around 1970.
Shattering, one point at a time
Take binary classifiers, each of which labels every input 0 or 1. Given k points, there are 2ᵏ ways to label them. A class H shatters those points if it can realize all 2ᵏ labelings.
Start with thresholds on a line: predict 1 when x ≥ t. One point can be labeled either way, by putting t below or above it. So one point is shattered. Two points a < b cannot be: the labeling a = 1, b = 0 is impossible, because anything at or above t includes b whenever it includes a. The VC dimension is 1.
Now take intervals: predict 1 when lo ≤ x ≤ hi. Two points can get all four labelings. Three points a < b < c cannot receive 1, 0, 1, since an interval containing a and c also contains b. The VC dimension is 2.
To prove that a VC dimension equals k, you need two arguments:
- Show some set of k points that is shattered.
- Show that every set of k + 1 points fails for at least one labeling.
Lines, boxes and higher dimensions
Linear classifiers in the plane predict 1 when w₁x₁ + w₂x₂ + b ≥ 0. Three points that do not lie on one straight line can be split in all eight ways. Three collinear points cannot, which is fine: you only need one good set.
No four points can be shattered. If the four form a convex quadrilateral, labeling opposite corners alike is impossible, which is the familiar XOR pattern. If one point lies inside the triangle formed by the others, you cannot give it a different label from all three corners. Degenerate arrangements fail too. So the VC dimension is 3.
In general, linear classifiers with a bias in d dimensions have VC dimension d + 1. Without the bias (the boundary must pass through the origin), it is d.
Axis-aligned rectangles in the plane shatter four points arranged in a diamond. With any five points, pick the ones that are leftmost, rightmost, highest and lowest. A rectangle containing those four must contain the fifth, so the VC dimension is 4.
Why d + 2 points defeat a linear classifier
The upper bound for linear classifiers has a tidy proof. A classical result in convex geometry, Radon's theorem, says that any d + 2 points in d dimensions can be split into two groups whose convex hulls overlap. The convex hull of a group is the smallest convex region containing it.
Label one group 1 and the other 0. A linear classifier that got this right would put one whole hull strictly on one side of a hyperplane and the other hull on the opposite side. Overlapping hulls cannot be separated that way. So no set of d + 2 points is shattered.
The same logic tells you what a feature map buys. If you feed a linear classifier all squares and pairwise products of 10 inputs along with the inputs themselves, you have 65 features. With a bias term, its VC dimension is at most 66, compared with 11 on the raw inputs. The boundary can now curve in the original space, and you pay for that flexibility in capacity.
Parameters are not the whole story
Counting parameters often gives a fair guess, as in the cases above. It is not a rule.
The family sign(sin(θx)) has one real parameter θ, yet its VC dimension is infinite. With suitably spaced points, a high enough frequency can produce any labeling. At the other extreme, a model with many redundant parameters, such as w₁ + w₂ + w₃ used as a single threshold, has VC dimension 1.
A 1-nearest-neighbor classifier over arbitrary training sets also has infinite VC dimension: store any labeled points and it reproduces their labels. It can still work well on real data, which shows that worst-case capacity is not the only thing that matters.
For finite classes, shattering k points needs at least 2ᵏ different classifiers, so the VC dimension is at most log₂|H|.
Checking small cases by computer
You can test shattering by brute force when the class is easy to enumerate. For intervals, only the positions relative to the points matter.
from itertools import product
def intervals_can(points, labels):
ones = [p for p, lab in zip(points, labels) if lab == 1]
if not ones:
return True # an empty interval
lo, hi = min(ones), max(ones) # tightest interval
return all((lo <= p <= hi) == bool(lab)
for p, lab in zip(points, labels))
def shattered(points):
return all(intervals_can(points, labs)
for labs in product([0, 1], repeat=len(points)))
print(shattered([1.0, 2.0])) # True
print(shattered([1.0, 2.0, 3.0])) # False The tightest interval is the best candidate, because any wider one only adds points. Failing to find a shattered set by random search does not prove anything, so the upper bound still needs an argument.
The growth function
How many distinct labelings can H produce on n points, at most? This count is the growth function. Up to the VC dimension d, it equals 2ⁿ. Beyond d, the Sauer-Shelah lemma caps it:
Π(n) ≤ C(n,0) + C(n,1) + ... + C(n,d) grows like a polynomial of degree d
Π(n) ≤ (e·n/d)^d a handy looser form, for n ≥ d ≥ 1 With d = 3 and n = 10, the first bound is 1 + 10 + 45 + 120 = 176, compared with 2¹⁰ = 1,024 possible labelings. The second gives about 744. As n grows, the fraction of labelings the class can reach shrinks toward zero.
That is why finite VC dimension permits learning. Once the class cannot fit arbitrary labels on a large sample, agreement with the real labels starts to carry information.
What it says about generalization
For a class of VC dimension d, with probability at least 1 − δ, every classifier in it has a gap between training error and true error that is at most on the order of
√( ( d·ln(n/d) + ln(1/δ) ) / n ) up to constant factors, for n > d The constants differ between textbook versions. The shape is the useful part: the gap shrinks roughly as 1/√n and grows roughly as √d. As a rule of thumb from the theory, n should be large compared with d. This connects to the PAC framework, where a binary class is learnable exactly when its VC dimension is finite, setting aside questions of computing time.
The results assume independent examples from one distribution, and they cover every classifier in the class, including ones your algorithm would never pick.
Choosing among families of different capacity
Suppose you can train a threshold, an interval, or a union of many intervals on the same one-dimensional data. The richer families always match the training labels at least as well, so training error alone would always favor the largest.
Structural risk minimization adds a penalty that grows with VC dimension. You arrange the families from small to large, find the best classifier in each, and pick the one with the lowest sum of training error and capacity term. A bigger family wins only if it reduces training error by more than its penalty rises.
In practice, people usually replace the theoretical penalty with a validation set or cross-validation, which measures the gap directly instead of bounding it. The logic is the same: extra capacity has to earn its place. Regularization offers a smoother version of this trade, adjusting capacity continuously inside one family.
Margins and modern networks
Capacity can also be limited by geometry. If a linear separator must keep all points, which lie within radius R, at a distance of at least γ from the boundary, the effective capacity is controlled by about (R/γ)², even in very high dimensions. This insight sits behind support vector machines.
For neural networks with piecewise linear activations, known VC bounds grow somewhat faster than the number of weights, with a dependence on depth. Many practical networks have far more weights than training examples and still generalize, and can also fit randomly shuffled labels. VC theory therefore does not explain their success on its own. Researchers look to the behavior of the optimizer, norms of the weights and structure in the data, and the picture is not settled. See double descent for a related surprise.
Common questions
Does a higher VC dimension mean a worse model?
No. It means more ability to fit patterns, including real ones. With plenty of data, a high-capacity class often wins. With little data, it is easier to fit noise.
Can I compute the VC dimension of my model and plug it in?
For standard families you can look it up or bound it. The resulting guarantees are usually too pessimistic for choosing sample sizes. A held-out set and a learning curve give sharper answers for a specific problem.
What about regression or many classes?
There are related measures, such as the pseudo-dimension for real-valued functions, the Natarajan dimension for multiclass labels, and Rademacher complexity, which depends on the data distribution.