Search

Search papers and lessons

Linear Classifier

Level 2 · 10 min read

A linear classifier labels an input by checking which side of a straight line, plane, or hyperplane it falls on, using one weighted sum of its features.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Linear algebra
  • Classical models
  • Learning basics
  • ReadReady to readNot yet

TL;DR

A linear classifier answers a yes-or-no or which-class question by computing one weighted sum of an input's features, adding a bias, and checking the sign. The rule draws a flat boundary, a line in two dimensions and a plane or hyperplane in more, that splits the input space into two regions. It is the simplest useful classifier, and it is the building block of many larger models.

Builds on: Dot Product, Supervised Learning · Leads to: Support Vector Machine, Logistic Regression, Hinge Loss

Where it's used

Email spam filtering. Early spam filters counted features of each message, such as how often words like "free" appear and how many links it has. A weighted sum of those counts, compared against a threshold, gave a fast spam decision that could run on every message that arrived.

Medical screening scores. A clinic may combine a few measured values into one weighted score and flag patients above a cut-off for follow-up. Such scores are linear in their inputs, so they are linear classifiers. This is an illustration of the method, not medical advice, and any real score is validated on clinical data.

Fast first-pass tests in large systems. When a model must decide millions of times per second, a linear rule costs only one multiplication per feature. Teams often use it as a baseline that a heavier model must beat.

How it works

Take a single feature xx, a weight w=2w = 2, and a bias b=−4b = -4. The rule is: predict "positive" when the score is above zero, and "negative" otherwise.

  1. Take the input xx. Suppose x=3x = 3.
  2. Multiply by the weight: 2⋅3=62 \cdot 3 = 6. The weight sets how strongly the feature pushes toward "positive".
  3. Add the bias: 6+(−4)=26 + (-4) = 2. The bias shifts the cut-off away from zero.
  4. Check the sign of the score. The score 22 is positive, so predict "positive".
  5. Try x=1x = 1. The score is 2⋅1−4=−22 \cdot 1 - 4 = -2. It is negative, so predict "negative".
  6. Find where the rule changes its answer. The score is zero at 2x−4=02x - 4 = 0, so x=2x = 2. Inputs above 2 are positive and inputs below 2 are negative. The single point x=2x = 2 is the boundary, the place where the score is zero.
−1012345−6−4−20246xscore
  • score = 2x - 4
  • zero line
The class flips where the score line crosses zero, at x = 2. That crossing point is the decision boundary.

The same pipeline works with many features. The input is a list of numbers x\mathbf{x}, the weights are a list w\mathbf{w} of the same length, and the bias is one number bb.

flowchart LR
  A["Features x"] --> B["Weighted sum w dot x"]
  B --> C["Add bias b"]
  C --> D{"Sign of score"}
  D -- "positive" --> E["Class +1"]
  D -- "negative" --> F["Class -1"]

The math

Definition: Linear classifier

A linear classifier predicts a class from a score ss with

s=w⋅x+b,y^=sign⁡(s),s = \mathbf{w} \cdot \mathbf{x} + b, \qquad \hat{y} = \operatorname{sign}(s),

where w\mathbf{w} is the weight vector, bb is the bias, w⋅x\mathbf{w} \cdot \mathbf{x} is the dot product of the weights and the features, and sign⁡(s)\operatorname{sign}(s) is +1+1 when ss is positive and −1-1 when ss is negative.

Step 2 in symbols: the weighted sum

The dot product multiplies matching entries and adds them up. With w=(w1,…,wd)\mathbf{w} = (w_1, \dots, w_d) and x=(x1,…,xd)\mathbf{x} = (x_1, \dots, x_d):

w⋅x=w1x1+w2x2+⋯+wdxd.\mathbf{w} \cdot \mathbf{x} = w_1 x_1 + w_2 x_2 + \cdots + w_d x_d.

A large positive wjw_j means that a large xjx_j pushes the score up.

Step 6 in symbols: the boundary

The boundary is the set where the score is exactly zero:

w⋅x+b=0.\mathbf{w} \cdot \mathbf{x} + b = 0.

In two dimensions this is a line. In three it is a plane. In dd dimensions it is a hyperplane, a flat set with one dimension fewer than the space it lives in. The weight vector w\mathbf{w} is perpendicular to the boundary, so it points toward the positive side.

The distance from the boundary (beyond the walkthrough)

The distance from a point x\mathbf{x} to the boundary is

distance=∣w⋅x+b∣∥w∥,\text{distance} = \frac{|\mathbf{w} \cdot \mathbf{x} + b|}{\lVert \mathbf{w} \rVert},

where ∥w∥\lVert \mathbf{w} \rVert is the length of the weight vector. A point with a large absolute score that is far from the boundary is classified with more confidence. This distance is the quantity that a support vector machine tries to make as large as possible.

Worked examples

Example
1. Tiny: one feature, one number

Use s=2x−4s = 2x - 4 with x=3x = 3 and x=1x = 1.

  1. For x=3x = 3: s=2⋅3−4=6−4=2s = 2 \cdot 3 - 4 = 6 - 4 = 2.
  2. The score is positive, so the class is +1+1.
  3. For x=1x = 1: s=2⋅1−4=2−4=−2s = 2 \cdot 1 - 4 = 2 - 4 = -2.
  4. The score is negative, so the class is −1-1.
  5. The boundary is at x=2x = 2, halfway between 1 and 3. Both points sit on opposite sides of it, as expected.
Example
2. Standard: two features on a plane

Use w=(1,1)\mathbf{w} = (1, 1) and b=−3b = -3, so the boundary is x1+x2=3x_1 + x_2 = 3. Classify three points.

  1. Point (2,2)(2, 2): s=1⋅2+1⋅2−3=1s = 1 \cdot 2 + 1 \cdot 2 - 3 = 1. Positive, so class +1+1.
  2. Point (1,1)(1, 1): s=1+1−3=−1s = 1 + 1 - 3 = -1. Negative, so class −1-1.
  3. Point (3,0)(3, 0): s=3+0−3=0s = 3 + 0 - 3 = 0. The score is exactly zero, so the point lies on the boundary and the rule must pick a convention.
  4. Distance of (2,2)(2, 2) from the boundary: ∣s∣/∥w∥=1/2≈0.71|s| / \lVert \mathbf{w} \rVert = 1 / \sqrt{2} \approx 0.71.
  5. Interpretation: the rule is a diagonal line. Points above it are one class and points below it the other, and the point (2,2)(2, 2) is comfortably on the positive side.
Example
3. Realistic: flagging spam from two counts

Suppose a spam filter has learned weights w=(0.5,1.0)\mathbf{w} = (0.5, 1.0) and bias b=−2b = -2 for two features. The first feature is the count of the word "free", and the second is the number of links. Positive scores mean "spam".

  1. Email A: free =4= 4, links =1= 1. Score =0.5⋅4+1.0⋅1−2=2+1−2=1= 0.5 \cdot 4 + 1.0 \cdot 1 - 2 = 2 + 1 - 2 = 1. Positive, so spam.
  2. Email B: free =1= 1, links =0= 0. Score =0.5⋅1+0−2=−1.5= 0.5 \cdot 1 + 0 - 2 = -1.5. Negative, so not spam.
  3. Email C: free =2= 2, links =1= 1. Score =1+1−2=0= 1 + 1 - 2 = 0. The score is zero, so it sits exactly on the boundary.
  4. Interpretation: the link count is worth twice as much as one use of the word "free" in this filter. Email C is the borderline case. A filter would send it to review, or use a tie-break rule.
  5. Note that the weights were chosen by training on labelled emails. Changing the cut-off by changing bb moves the boundary parallel to itself.

Try it: a classifier has w=(2,−1)\mathbf{w} = (2, -1) and b=0b = 0. Which class does x=(1,3)\mathbf{x} = (1, 3) receive? Answer: s=2⋅1+(−1)⋅3+0=2−3=−1s = 2 \cdot 1 + (-1) \cdot 3 + 0 = 2 - 3 = -1. The score is negative, so the class is −1-1.

Common mistakes and tradeoffs

"A linear classifier can separate any data if I train it long enough." It cannot. The classic counterexample is XOR: points (0,0)(0, 0) and (1,1)(1, 1) belong to one class, while (1,0)(1, 0) and (0,1)(0, 1) belong to the other. No single straight line puts the two pairs on opposite sides. Only data that is linearly separable, meaning a hyperplane exists that splits the classes exactly, can be perfectly classified this way. The 1969 book Perceptrons by Minsky and Papert made this limit famous.

"The weight vector is the boundary." It is not. The weight vector is perpendicular to the boundary. The boundary is the set of points where the score is zero, which is a flat set through the input space.

"A bigger score means a more certain answer." Only as a rough guide. The raw score grows with the length of w\mathbf{w}, so scores are comparable only when w\mathbf{w} is normalised. The distance formula in step 6 is the clean measure.

Tradeoff: simple and fast against limited shape. A linear classifier has only d+1d + 1 parameters, trains quickly, and is easy to read: each weight says how much a feature matters. The cost is a flat boundary. When classes form curved or interlocking regions, the classifier underfits, meaning it is too simple to capture the pattern even on its training data. Alternatives include decision trees for axis-aligned rules, kernel methods that bend the boundary, and multilayer networks that stack many linear pieces with non-linear functions between them.

When to use it. Use a linear classifier as a strong baseline, when the features are informative, when speed matters, or when you need to explain each decision. When not to use it. Avoid it when the classes interact in ways a straight cut cannot capture, such as XOR-like patterns in raw features.

History

In 1936 the statistician Ronald Fisher introduced a linear rule for separating two groups of measurements, now called linear discriminant analysis. In 1957 Frank Rosenblatt built the perceptron, an online learning rule that adjusts the weights after each mistake, and it was among the first models that learned a classifier from examples. Enthusiasm dropped after the 1969 critique of its limits to linearly separable data. The method returned as a core part of machine learning in the 1990s, when margin-based methods such as the support vector machine made linear classifiers both principled and powerful on high-dimensional data.

Recap

  • A linear classifier computes a weighted sum of the features plus a bias and predicts the sign of that score.
  • The boundary is where the score is zero: a line in two dimensions, a plane or hyperplane in more.
  • The weight vector is perpendicular to the boundary, and the distance of a point from it is the absolute score divided by the length of the weights.
  • A linear classifier works only when a hyperplane can separate the classes, which fails for XOR-like data.
  • Read next: Support Vector Machine, which chooses the boundary with the widest margin; Logistic Regression, which turns the score into a probability; and Hinge Loss, the loss used to train margin-based linear classifiers.

Check yourself

QuestionRecall: what is the decision boundary of a linear classifier with weights w and bias b?Show answer
The set of inputs x where the score w dot x plus b equals zero. In two dimensions it is a line, in three a plane, and in general a hyperplane that splits the input space into the two predicted classes.
QuestionComputation: a linear classifier has w = (3, -2) and b = 1. What is the score for x = (2, 4), and which class is predicted?Show answer
The score is 3 times 2 plus (-2) times 4 plus 1, which equals 6 minus 8 plus 1, so the score is -1. The score is negative, so the predicted class is the negative class.
QuestionWhy: why can no linear classifier solve XOR, and what does that tell you about the data needed for a linear model to work?Show answer
XOR needs the two points (0, 0) and (1, 1) on one side of a line and (1, 0) and (0, 1) on the other. Any straight line that separates the first pair leaves the second pair on the same side, so no line works. A linear classifier can be perfect only when the classes are linearly separable, so an engineer must check that property or add features that make it hold.
QuestionApplied: a fraud model scores each transaction with w = (0.8, 1.5) and b = -3 for two features, amount in hundreds of dollars and number of foreign countries in the last day. A transaction has amount 2 and 1 foreign country. What is its score, and what does a score of exactly zero mean for the team?Show answer
The score is 0.8 times 2 plus 1.5 times 1 minus 3, which is 1.6 plus 1.5 minus 3, giving 0.1. The score is positive, so it is flagged. A score of exactly zero means the transaction sits on the boundary, so a team should define a rule for borderline cases, such as sending it to manual review, rather than let an arbitrary tie-break decide.

Sources