cat /en/why-was-it-returned/
MDN-119:22
SERIES Inside Ordito · Chapter 6

Why was it returned?

· Artificial Intelligence · 4 min read

When a customer returns a garment, the cashier writes the reason in a hurry: “non mi entra” (it doesn’t fit me), “colore diverso dalla foto” (colour different from the photo), “tg piccola” (size small). There are almost two hundred thousand sentences, and Ordito would like to know how many concern the size, the colour, a defect. Two colleagues read a thousand by hand; a classifier reads the others.

Bayes, word by word

Each sentence becomes a bag of words: they are counted, the order is lost. The naive Bayes classifier computes a score for each reason: the logarithm of the frequency of the reason, plus, for each word, the logarithm of the probability of that word within that reason. The reason with the highest score wins.

“Naive” because it pretends that the words are independent of each other, as if “non” (not) and “entra” (fits) had nothing to do with one another. It is a false assumption, and it works surprisingly well.

In the lab, write a sentence. On the left the probabilities of the ten reasons, on the right the contribution of each word to the three leading reasons.

DEMO Why was it returned? · about this demo · full screen ↗

Three experiments

Word by word. “la taglia non mi entra” (the size doesn’t fit me) goes to size too small with 98%: “taglia” (size) and “entra” weigh much more there than elsewhere. “non mi piace il colore” (I don’t like the colour) goes to colour with 99%.

Smoothing. A word never seen within a reason would have zero probability, and on its own would rule that reason out. The parameter α adds a small fictitious number to every count. With a small α an absent word weighs a great deal; with a large α the reasons flatten towards their frequency.

Unknown words. A word that does not appear in the thousand returns has no column, and the classifier ignores it. “arrivato rotto” (arrived broken) ends up under delay with just 50%: “rotto” (broken) was not there, and “arrivato” (arrived) speaks of deliveries.

The same experiment in Python

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

URL = "https://raw.githubusercontent.com/mid079/machine-learning-teoria-pratica/main/dati/"
resi = pd.read_parquet(URL + "resi.parquet")
motivi = pd.read_parquet(URL + "verita/motivi_resi.parquet")   # the true reason: only to simulate reading by hand
testi = resi.merge(motivi, on="id_reso").dropna(subset=["motivo"])
etichettati = testi.sample(1000, random_state=42)                # the thousand returns read by hand

cv = CountVectorizer()
nb = MultinomialNB(alpha=1.0).fit(cv.fit_transform(etichettati["motivo"]), etichettati["motivo_vero"])
frasi = ["la taglia non mi entra", "non mi piace il colore", "arrivato rotto"]
for frase, prob in zip(frasi, nb.predict_proba(cv.transform(frasi))):
    migliori = sorted(zip(nb.classes_, prob), key=lambda c: -c[1])[:2]   # the two most likely reasons
    print(frase, "→", [(str(m), round(float(q), 3)) for m, q in migliori])

In the book

This lab accompanies chapter 6, “Neighbours and words: KNN and Naive Bayes”. The first half of the chapter uses the k nearest neighbours to predict the sales of a new garment and meets the curse of dimensionality; the second reads the reasons for returns with Bayes’ theorem, bags of words and character n-grams.

COVER COMING SOON
mltp · 2026

Machine Learning. Teoria, matematica e pratica degli algoritmi

Michele di Nuzzo · in Italian

Every algorithm in two steps: first derive it, with the mathematics it needs and an example worked by hand, then write it from scratch in NumPy. Only at the end use it with scikit-learn.

[the book’s page] [shop links coming soon]

The “Inside Ordito” series