Product Classification
Machine Learning
Algorithms
Data Science
E-commerce

Algorithm to classify a list of products? Take 2

Master System Design with Codemia

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

Introduction

Product classification usually looks like a machine-learning problem, but in practice it works best as a pipeline. Real catalogs contain noisy titles, inconsistent vendor naming, and categories that are partly rule-driven and partly statistical.

Start With a Taxonomy and Clean Inputs

Before choosing a model, define the category tree you actually want. If categories overlap or the labeling rules are inconsistent, no algorithm will fix that later.

For each product, collect the fields that help classification most:

  • title
  • short description
  • brand
  • attributes such as color, size, or material
  • existing supplier category if available

Then normalize the text. Lowercasing, punctuation cleanup, and unit normalization remove a large amount of noise.

python
1import re
2
3
4def normalize(text):
5    text = text.lower()
6    text = re.sub(r"[^a-z0-9\s]", " ", text)
7    text = re.sub(r"\s+", " ", text).strip()
8    return text
9
10print(normalize("Men's Running Shoes, Size 10"))

That preprocessing step matters because many catalog errors are simple formatting differences rather than real semantic ambiguity.

A Strong Baseline: Rules First, Model Second

In e-commerce systems, some products can be classified deterministically. If a title contains a unique brand code or a regulated product term, a rule-based classifier is often more reliable than a model.

Example:

python
1RULES = {
2    "usb c cable": "Electronics > Cables",
3    "office chair": "Furniture > Chairs",
4    "dog food": "Pet Supplies > Food",
5}
6
7
8def classify_by_rule(text):
9    for phrase, category in RULES.items():
10        if phrase in text:
11            return category
12    return None

This is not a complete system, but it is a good first stage. Fast, high-confidence rules reduce the workload for the machine-learning layer and make the final behavior easier to explain.

Use Text Classification for the Hard Cases

For the remaining products, a text model is a practical next step. A TF-IDF vectorizer plus a linear classifier is often strong enough for catalog classification, especially when you have labeled training data.

python
1from sklearn.feature_extraction.text import TfidfVectorizer
2from sklearn.pipeline import make_pipeline
3from sklearn.svm import LinearSVC
4
5train_x = [
6    "nike running shoes men",
7    "wood office chair ergonomic",
8    "grain free dog food",
9    "usb c charging cable"
10]
11train_y = [
12    "Footwear > Athletic Shoes",
13    "Furniture > Chairs",
14    "Pet Supplies > Food",
15    "Electronics > Cables"
16]
17
18model = make_pipeline(TfidfVectorizer(), LinearSVC())
19model.fit(train_x, train_y)
20
21print(model.predict(["wireless ergonomic office chair"])[0])

This approach is easy to train, fast to deploy, and usually much easier to maintain than a complex deep-learning stack.

Handle Confidence and Human Review

A classifier should not always guess. Some products are genuinely ambiguous, especially if the input title is short or missing important attributes.

A robust pipeline usually returns one of three outcomes:

  • high-confidence category assigned automatically
  • low-confidence prediction sent to review
  • no prediction because rules and model both failed

Even if your model does not expose probabilities directly, you can derive review thresholds from decision scores, nearest-class margins, or ensemble agreement.

Use Hierarchical Categories Carefully

Many product catalogs use nested categories. You can model that hierarchy in two main ways:

  • predict the full path as one label
  • predict one level at a time

Predicting one level at a time is often easier to debug. For example, first choose Electronics, then Cables, then a subtype. It also allows you to stop early when confidence drops.

If you jump straight to the leaf category, the label space gets large and rare classes become difficult to learn.

Common Pitfalls

The biggest mistake is treating product classification as only a machine-learning problem. Bad taxonomy design and inconsistent labels usually hurt accuracy more than model choice.

Another mistake is skipping rule-based logic for obvious cases. Deterministic matches are cheap and often more accurate than statistical predictions.

A third issue is ignoring class imbalance. Rare categories may look fine in aggregate accuracy while performing badly in production.

Summary

  • Clean taxonomy design comes before algorithm choice.
  • Normalize product text so simple variations do not create fake differences.
  • Use rules for high-confidence, deterministic matches.
  • Use a text classifier such as TF-IDF plus LinearSVC for the harder cases.
  • Send low-confidence items to review instead of forcing every product into a category.

Course illustration
Course illustration

All Rights Reserved.