liveBook · Manning

Precision

As explained in LLMs in Production

Precision is the proportion of predicted items that are correct among all predicted items.

Precision measures how many predicted items are correct among all predicted items. For text evaluation, it can reward short predictions too generously: predicting “cat hat” against “the cat in the hat” gives perfect precision despite missing most of the target. section 4.2.1

BLEU adapts precision for machine translation by adding a brevity penalty and modified N-gram precision. These adjustments penalize predictions that are too short and support comparison against multiple targets. section 4.2.1

A misleading perfect score

With target “the cat in the hat” and prediction “cat hat,” standard precision is 1.0 because both predicted words occur in the target, while recall is only 0.4. BLEU addresses this imbalance. section 4.2.1

Listing 4.2 Using BLEU
import nltk.translate.bleu_score as bleu

target = [
    "The game 'The Legend of Zelda' follows the adventures of the \
    hero Link in the magical world of Hyrule.".split(),
    "Link goes on awesome quests and battles evil forces to \
    save Princess Zelda and restore peace to Hyrule.".split(),
]
prediction = "Link embarks on epic quests and battles evil forces to \
    save Princess Zelda and restore peace in the land of Hyrule.".split()


score = bleu.sentence_bleu(target, prediction)
print(score)
# 0.6187934993051339

Run a minimal NLTK BLEU example comparing one prediction with two Zelda-related target sentences. section 4.2.1

How LLMs in Production covers it

How deep it goes
Explains standard precision, demonstrates its short-output failure, and introduces BLEU’s brevity penalty and modified N-gram precision without deriving them.
Examples
Uses the “cat hat” counterexample and an NLTK BLEU comparison involving Zelda-related sentences.
What you can do after
Evaluate text predictions with NLTK’s BLEU implementation and recognize when standard precision overstates quality.
How it connects
Connects precision with recall, ROUGE, BLEU, N-grams, brevity penalty, tokenization, and SacreBLEU.

Also explained in

Compare how 20 books explain precision →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.