ML Notes
Complete introduction to classification in machine learning covering binary and multi-class problems, decision boundaries, and algorithm selection strategies.
Classification is one of the most fundamental and widely used tasks in machine learning. If you have ever wondered how your email app knows which messages are spam, how Netflix recommends shows you might enjoy, or how a bank decides whether to approve a loan — the answer often lies in classification algorithms. In this lesson, we will build a solid understanding of what classification is, why it matters, and how different types of classification problems work in practice.
What Exactly Is Classification?
At its core, classification is a supervised learning task where the goal is to assign input data points to one of several predefined categories or classes. Think of it like sorting — you are teaching the computer to look at something and say "this belongs to category A" or "this belongs to category B."
Here is the key insight: unlike regression (which predicts a continuous number like house price), classification predicts a discrete label. The output is a category, not a quantity. When a doctor looks at a patient's blood test results and says "diabetic" or "not diabetic," that is classification in action.
The model learns patterns from labeled training data (where we already know the correct category) and then uses those patterns to classify new, unseen data points. The training process is essentially the model discovering which features are most indicative of each class.
Types of Classification Problems
Classification problems come in several flavors, and understanding these distinctions is crucial because it determines which algorithms and evaluation metrics you should use.
Binary Classification
This is the simplest and most common form — there are exactly two possible outcomes. The model draws a decision boundary that separates data into two groups. You can think of it as answering a yes/no question about each data point.
Common examples include spam versus not-spam email detection, fraudulent versus legitimate credit card transactions, positive versus negative sentiment in product reviews, and disease present versus absent in medical screening. Most classification algorithms are designed for binary problems first, and then extended to handle multiple classes through strategies like one-versus-rest or one-versus-one.
Multi-Class Classification
Here, the model must choose from three or more mutually exclusive classes. For instance, classifying a handwritten digit as 0 through 9 is a 10-class problem. Other examples include identifying animal species from photos, detecting which language a text is written in, and categorizing customer support tickets into departments.
The intuition behind multi-class is that instead of drawing one boundary line, the model draws multiple boundaries that carve the feature space into distinct regions, one per class. Each region represents the territory of one class.
Multi-Label Classification
This is slightly different from multi-class — each data point can belong to multiple classes simultaneously. A movie can be both "Action" and "Comedy" at the same time. A news article can be tagged with "Politics," "Economy," and "International" all at once. Here the model outputs multiple labels rather than choosing just one. Technically, multi-label classification can be decomposed into multiple independent binary classification problems.
| ├── Binary (2 classes) | Spam / Not Spam |
| ├── Multi-class (>2, pick exactly one) | Digit recognition (0-9) |
| └── Multi-label (pick multiple) | Movie genres (Action + Comedy + Sci-Fi) |
Decision Boundaries — The Core Geometric Concept
The intuition behind classification is beautifully geometric. Imagine plotting your data points on a graph where each axis represents a feature. Points from different classes form clusters in different regions of this space. The classifier's job is to find a boundary (a line, curve, or hyperplane) that separates these clusters as cleanly as possible.
A linear classifier draws a straight line (or flat plane in higher dimensions). A non-linear classifier can draw curved, complex boundaries that wrap around clusters. The choice depends on how your data is distributed — if classes overlap in complicated ways, you need a more flexible boundary. However, more flexible boundaries also risk overfitting to noise in the training data.
Think of it like drawing borders on a map. Simple, straight borders are easy to define and generalize well, but sometimes the geography demands curvy, complex borders to properly separate regions.
Building Your First Classifier in Python
Let us put theory into practice with a complete example using scikit-learn:
This simple example demonstrates the entire classification workflow: generate data, split into train/test sets, train the model, make predictions, and evaluate performance.
How Classification Algorithms Learn
Every classification algorithm follows the same high-level process during training, though the internal mechanics differ dramatically:
- Receive labeled data — examples where we already know the correct class
- Learn patterns — discover relationships between input features and class labels
- Build an internal model — encode those patterns into a mathematical function or structure
- Optimize — adjust parameters to minimize classification errors on training data
- Predict new data — apply the learned function to unseen examples
The differences between algorithms lie in HOW they learn patterns. Logistic regression fits a probability curve. Decision trees split data using if-then rules. KNN memorizes examples and classifies by proximity. SVMs find the widest possible margin between classes. Neural networks learn hierarchical feature representations.
Choosing the Right Algorithm
There is no single "best" classification algorithm — the right choice depends on your specific data characteristics and requirements:
| Algorithm | Best When | Key Weakness |
|---|---|---|
| Logistic Regression | Linearly separable data, need interpretability | Cannot capture complex non-linear patterns |
| Decision Trees | Need human-readable rules, mixed feature types | Prone to overfitting on noisy data |
| Random Forest | Want reliable accuracy without much tuning | Slower prediction, less interpretable |
| SVM | High-dimensional data, clear class margins | Slow training on large datasets |
| KNN | Small datasets, irregular decision boundaries | Very slow at prediction time with large data |
| XGBoost | Competition-level accuracy, tabular data | Requires careful hyperparameter tuning |
A practical strategy is to start with logistic regression as a baseline (fast, interpretable), then try random forest or gradient boosting for better accuracy, and finally experiment with SVMs or neural networks for complex problems.
Evaluating Classification Models
Classification has its own evaluation metrics beyond simple accuracy. If 95 percent of your emails are not spam, a model that always predicts "not spam" achieves 95 percent accuracy but catches zero actual spam — clearly useless.
Key metrics include precision (of all predicted positives, how many are truly positive), recall (of all actual positives, how many did we correctly identify), F1-score (the harmonic mean balancing precision and recall), and the ROC-AUC curve (measuring the model's ability to discriminate between classes across all threshold settings).
Real-World Applications
Classification powers countless applications you interact with daily: healthcare diagnosis from medical images, credit card fraud detection processing millions of transactions in real time, face recognition unlocking your phone, voice assistants understanding your commands, content moderation on social media platforms, customer churn prediction for subscription businesses, and sentiment analysis for brand monitoring.
Key Takeaways
Classification is your go-to technique whenever the output is a category rather than a number. Start with simpler models to establish a baseline, evaluate with appropriate metrics beyond accuracy, and remember that data quality and feature engineering often matter more than algorithm choice. The following lessons will dive deep into each algorithm with mathematical foundations and hands-on implementations.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Introduction to Classification.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, classification, introduction, introduction to classification
Related Machine Learning Topics