liveBook · Manning

Classifier

As explained in Data Analysis with LLMs

Classification is the assignment of one or more predefined categories to an input item.

A classifier assigns an input to a predefined category. For text, that might mean labeling a movie review Positive or Negative, sorting an email into a custom folder, or deciding whether a comment is offensive. The categories must be clear before classification begins. section 4.2

A general-purpose language model needs more than the text alone. Give it the text to classify, the question it should answer, and the allowed labels. Relevant context and a few labeled examples can help clarify the task. Unlike a specialized classifier, an untuned model may respond with an analysis instead of a label if given only a review. section 1.3.2 section 9.10

Listing 1.1 Prompt for classifying a laptop review
We are considering product reviews for laptops.  #1
For each review, output "satisfied" or "dissatisfied", 
depending on whether the customer is satisfied 
with the product or not.  #2
Examples:
This is a great laptop! I recommend everyone to buy it! 
satisfied  #3
This laptop did not work. I had to return it.
dissatisfied  #4
The screen is too small and it takes too long to start.  #5

The laptop-review prompt names the context, limits the output to “satisfied” or “dissatisfied,” shows two labeled examples, and ends with the review awaiting a label. section 1.3.2

To classify a collection of reviews with Python:

  • Read a CSV file whose text column contains the reviews. section 4.2.5
  • For each review, fill a prompt template and send the prompt to a language model. Treat its reply as a proposed class label. section 4.2.1 section 4.2.5
  • Save the labels in a new class column, count the responses, and compare labels with known sentiments when available. section 4.2.5 section 4.2.6

Movie-review sentiment

A prompt can end with: “Is the sentiment positive or negative? Answer ("pos"/"neg"):”. Run it on each review rather than assuming every reply is correct. In a sample with known sentiments, comparing generated labels against ground truth reveals mistakes even when most labels agree. section 9.2 section 4.2.6

The same pattern can classify images: ask whether each fruit image shows a banana and retain those answered “Yes.” For text, a LangChain chain can connect the prompt template, chat model, and output parser. Fine-tuning offers another route: a model trained on review labels can classify from review text alone, though its results still need checking. section 6.2.3 section 10.2.3 section 9.10

How Data Analysis with LLMs covers it

How deep it goes
Works through prompt contents and a CSV-to-label workflow, then checks results against known labels; does not explain model internals.
Examples
Uses laptop and movie reviews, email categories, offensive-comment filtering, and yes-or-no fruit-image questions.
What it assumes
Assumes access to a language model and, for the batch workflow, a CSV file with a text column; introduces prompt requirements explicitly.
What you can do after
Build it from scratch with Python and OpenAI’s library to label reviews in a CSV, then compare results with known sentiments.
How it connects
Connects classification to sentiment analysis, prompt templates, chat completion, fine-tuning, and visual question answering.

Also explained in

Compare how 31 books explain classifier →

Related concepts

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