CHAPTER 03 OF 50

Classification: predicting a category

Recognize a fruit from its picture by predicting a category.

Sachin Gupta4 min read
Portrait of Sachin Gupta rendered in binary

You see a picture of a fruit and ask, “What fruit is this?” An answer might be Apple, Banana, or Orange. Predicting which named group the picture belongs to is a classification task.

A category is a named group, also called a class. Classification means predicting the class for an input. A model that does this is called a classifier.

Why do we need classification?

Recognizing one apple is easy for a person. Sorting thousands of new pictures or messages takes time. A trained classifier can suggest a category for each new item, helping a system sort it or send it to the right place.

Here are three practical uses, with example predictions.

  • Email filtering. Read an incoming email and predict Spam or Not spam. The email service can use that category to sort unwanted messages into the spam folder.
  • Support routing. Read a customer’s message and predict Billing, Delivery, or Returns. The category helps send the request to the matching support team.
  • Language detection. Read a sentence and predict its language, such as English, Hindi, or Spanish. This can help a translation tool choose the input language.

This is useful when people can provide examples with known categories, but writing a dependable rule for every new item would be difficult. The prediction supplies a category that a system can act on.

Learn from pictures with known names

Training examples pair fruit pictures with their known names: an apple picture with Apple, a banana picture with Banana, and an orange picture with Orange. Each known name is the label.

The model learns patterns from those labeled examples. Later, we give it a new picture without the name. It predicts a category, such as Apple. The drawing below illustrates this task; an image model is not running on the page.

What fruit is in this picture?This is classification: the answer names one of the possible fruit categories.SUPERVISED LEARNING / 03What fruit is in this picture?Portrait of Sachin GuptaIdentify a fruitINPUTA new fruit pictureEXAMPLE PREDICTIONAppleA fruit categoryKnown fruit names in trainingAppleBananaOrangeCLASSIFICATIONA category nameSachin Gupta · guptasachinn.comIllustrative answers · no live model
This is classification: the answer names one of the possible fruit categories.

The new picture matters

An apple picture and a banana picture should lead to different category predictions. The purpose is to recognize the new input using what was learned from examples.

A classifier can make mistakes. If it predicts Orange for a banana, compare that prediction with the known label, Banana, to see the mistake. The methods used to learn these patterns receive their own later chapters.

Choose the fruit category

Try the recognition task yourself. Pick a picture, then choose its category.

Illustrated fruit: appleThis drawing supplies the subject of the practice question. Choose the kind of answer that the question asks for.

What fruit is in this picture?

Choose one category.

This activity checks your answer against the known label. It does not run a trained image model.

A category name can be a digit

Suppose the input is a picture of a handwritten 7. The task is to identify which digit was written. The possible category names are 0 through 9.

A prediction of 7 names the digit in the picture. It is still classification. Category names can be words or numeric codes.

Classification in one infographic

Labeled pictures, a new input, and a predicted category.

ClassificationLearn from labeled examples to predict a category. Six illustrated panels explain this task using fictional examples.SUPERVISED LEARNING / CHAPTER 03ClassificationLearn from labeled examples to predict a category.Portrait of Sachin Gupta1Ask for a categoryAppleWhat fruit is in this picture?The answer names a group, or class.2Train on labeled picturesFruit picture + known category nameAppleBananaOrange3The new picture mattersBananaA different input can need a different label.Use patterns learned from examples.4The possible categoriesAppleBananaOrangeThis example has three possible classes.5Category names can be digitsDigit 7Which digit was written?The possible labels are 0 through 9.The prediction names one digit.6Check the predicted labelA wrong prediction can happen.Predicted: OrangeKnown: BananaThese labels do not match.The model made a mistake.Classification predicts a category.Illustrative examples and predictions · no live trained model on this pageSachin Gupta · guptasachinn.com

Practice code and sources

Run the recognition practice

Optional: Node.js 20 or newer, no extra packages. Copy the full source into classification-example.mjs. This checks a practice choice against its known answer, without training an image model.

node classification-example.mjs
Correct category.
Known answer: Banana

Change pictureId and myAnswer near the bottom, then run again.

Full source
// A recognition practice activity, not a trained image model.
export const FRUIT_CATEGORIES = Object.freeze(["Apple", "Banana", "Orange"]);
export const FRUIT_PICTURES = Object.freeze([
  { id: "apple", label: "Apple picture", answer: "Apple" },
  { id: "banana", label: "Banana picture", answer: "Banana" },
  { id: "orange", label: "Orange picture", answer: "Orange" },
]);
export function checkFruitChoice(pictureId, answer) {
  const picture = FRUIT_PICTURES.find(item => item.id === pictureId);
  if (!picture || !FRUIT_CATEGORIES.includes(answer)) throw new RangeError("Choose an available picture and category.");
  return { correct: answer === picture.answer, answer: picture.answer };
}



// This checks a practice answer. It does not train or run an image model.
// Try apple, banana, or orange, then choose Apple, Banana, or Orange.
const pictureId = "banana";
const myAnswer = "Banana";
const result = checkFruitChoice(pictureId, myAnswer);
console.log(result.correct ? "Correct category." : "Try another category.");
console.log("Known answer: " + result.answer);
Sources and teaching notes

The definition follows Google’s classification definition. Google’s supervised-learning introduction explains input-label pairs and prediction on new inputs. The fruit drawings and predictions are illustrative. The practice activity uses a fixed answer key.

Anthropic’s support-ticket routing guide describes using classification to route customer requests. The practical examples above use illustrative category predictions.

← Chapter 2: Regression
Next: inputs, features, and known answers. Chapter 4 is planned.

Related

Tagged