linear separability
2D classification
machine learning
algorithmic analysis
pattern recognition

Determine whether the two classes are linearly separable algorithmically in 2D

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Two classes in 2D are linearly separable if there exists a single straight line that places every point of one class on one side and every point of the other class on the other side. That sounds geometric, but it can be tested algorithmically in several ways. The best method depends on whether you want a quick practical test, a mathematically exact condition, or an actual separating line as output.

What Linear Separability Means

Suppose class A and class B each contain points in the plane. The data is linearly separable if there exist weights and a bias such that every point from one class satisfies one inequality and every point from the other class satisfies the opposite inequality.

In 2D, that inequality defines a line.

A good mental model is simple: if you can slide and rotate a ruler until it separates the red points from the blue points, the classes are linearly separable.

Practical Test with a Linear Classifier

The fastest practical test is often to train a linear classifier and see whether it reaches zero training error.

python
1from sklearn.linear_model import Perceptron
2import numpy as np
3
4X = np.array([
5    [1, 1], [2, 1], [2, 2],
6    [6, 6], [7, 6], [7, 7]
7])
8y = np.array([0, 0, 0, 1, 1, 1])
9
10model = Perceptron(max_iter=1000, tol=1e-6, random_state=0)
11model.fit(X, y)
12print(model.score(X, y))
13print(model.coef_, model.intercept_)

If the training accuracy is exactly 1.0, the data may be linearly separable and the learned coefficients define a separating line. If it never reaches zero error, the data is probably not linearly separable.

This is practical, but it is still an optimization-based test rather than a direct geometric proof.

Geometric Test with Convex Hulls

In 2D, a very clean exact idea is based on convex hulls. Two point sets are linearly separable if and only if their convex hulls do not intersect.

That matters because a separating line cannot isolate the raw points if the outer envelopes of the two classes already overlap.

With SciPy, you can at least compute the hulls and inspect them programmatically:

python
1import numpy as np
2from scipy.spatial import ConvexHull
3
4class_a = np.array([[1, 1], [2, 1], [2, 2], [1, 2]])
5class_b = np.array([[5, 5], [6, 5], [6, 6], [5, 6]])
6
7hull_a = ConvexHull(class_a)
8hull_b = ConvexHull(class_b)
9
10print(hull_a.vertices)
11print(hull_b.vertices)

The full intersection test needs additional geometry logic, but the principle is important: hull overlap means no linear separation.

Linear Programming View

Another exact formulation is to ask whether there exists a line satisfying strict inequalities for the two classes. That becomes a feasibility problem.

For points in class A, require:

text
w1*x + w2*y + b >= 1

For points in class B, require:

text
w1*x + w2*y + b <= -1

If a solver can satisfy all constraints, the classes are linearly separable. This is mathematically precise and close to the logic behind hard-margin support vector machines.

Why Visual Inspection Is Not Enough

Plotting the points is useful in 2D, but it is not an algorithmic test. Human eyes can miss borderline cases, duplicate points, or points that sit almost on top of the separating boundary.

A plot is a diagnostic tool. The decision should still come from a well-defined algorithm or feasibility check.

Edge Cases Matter

Be careful with these situations:

  • identical points appear in both classes
  • one point lies exactly on the candidate boundary
  • the data is separable only within floating-point tolerance
  • one class has very few points, which can make optimization look unstable

If the question is mathematical separability, define clearly whether points on the boundary are allowed. In most classification formulations, strict separation is expected.

Common Pitfalls

  • Assuming that a visually obvious gap always proves separability without an actual test.
  • Using a non-linear model to test linear separability and then drawing the wrong conclusion.
  • Ignoring points on or near the separating boundary, where numeric tolerance matters.
  • Treating perceptron non-convergence as a proof by itself without considering training settings and implementation details.
  • Forgetting the convex-hull criterion, which gives a strong geometric interpretation in 2D.

Summary

  • Two 2D classes are linearly separable if one straight line can separate them completely.
  • A perceptron or linear SVM is the easiest practical test and can also produce the separating line.
  • The convex-hull viewpoint gives a clean geometric criterion in 2D.
  • Linear programming provides an exact feasibility formulation.
  • Use plotting for intuition, but rely on an algorithmic test for the actual decision.

Course illustration
Course illustration

All Rights Reserved.