Concepts / Supervised Learning Algorithms

Supervised Learning Algorithms

Machine learning is much easier to learn when you have a real dataset to experiment with.

  • Programming

A Clustering Result With No Answer Key

Machine learning is much easier to learn when you have a real dataset to experiment with. Scikit-learn ships with several datasets that are ready to use, and the breast cancer dataset is one of them. This article loads it, runs a few lines of code, and examines what comes back. Each step starts with code you can trace, so you can check your understanding against what Python actually prints.

The article then turns to a harder question that appears once the data is no longer labeled. Imagine you run a clustering algorithm on a large collection of photographs and it returns several groups of pictures. Your first question is simple: did it work? In many machine learning settings that question has a straightforward answer, because you can compare the output against a known correct result. In unsupervised learning that comparison is usually impossible. Keep this scenario in mind, because the rest of the article returns to it.

Loading the Dataset and Listing Its Keys

The data is loaded with the load_breast_cancer function from scikit-learn. The function is imported from the sklearn.datasets module, called once, and its result is stored in a variable named cancer. The program below loads the dataset and prints the keys that the loaded object provides.

python

What do you think happens?

Before running the code, what kind of object do you expect cancer to be, and which keys do you expect to see?

  • A plain Python list of numbers

  • An object whose keys include data, target, and feature_names

  • A pandas DataFrame with no keys

Reveal answer
Answer:

An object whose keys include data, target, and feature_names

The source describes the loaded result as a Bunch object that holds information about the dataset together with the actual data, and it names the data, target, and feature_names attributes. The full printed list of keys is what you should check against when you run the code yourself.

What a Bunch Object Holds

Datasets that come with scikit-learn are usually stored as Bunch objects. A Bunch object holds some information about the dataset together with the actual data. The source describes what you need to know about it in one sentence: a Bunch behaves like a dictionary, with the added benefit that you can access values using a dot. So bunch.key is an alternative to bunch['key'].

cancerBunch objectdata569 by 30 array ofmeasurementstargetclass value for each samplefeature_nameslist of 30 namescancer.datacancer.targetcancer.feature_names
Each key in the Bunch maps to one value. Dot notation such as cancer.data reads the value stored under the key data, the same as dictionary-style lookup.

Because the Bunch behaves like a dictionary, the dot form and the bracket form refer to the same stored value. Writing cancer.data and cancer['data'] both reach the array of measurements. The dot form is simply shorter and easier to read in a line of code. The stored values are the same either way.

Reading the Shape: 569 Samples and 30 Features

The breast cancer dataset consists of 569 data points, with 30 features each. The shape attribute of the data array reports this directly. Printing cancer.data.shape shows the number of rows and the number of columns, in that order. In this layout each row is one data point, which the source calls a sample, and each column is one feature measured on that sample.

python
rows569 samplescell [0, 0]first feature of the firstsamplecolumns30 featuresrow 0column 0
The first number in the shape is the row count, one row per sample. The second number is the column count, one column per feature. A single cell is found by its row position and its column position.

The source also gives the per-class sample counts. The dataset is labeled as 212 malignant and 357 benign. These two counts add up to the 569 samples, so every row belongs to exactly one of the two classes. The 30 in the shape is a different kind of number: it counts features, not classes. Keeping the samples dimension and the features dimension separate is the key to reading the shape correctly.

Worked example

Checking the counts against the shape

The dataset has 212 malignant samples and 357 benign samples. Does this agree with the shape of cancer.data?

  1. 1
    Add the class counts

    212 plus 357 gives 569.

  2. 2
    Compare with the row count

    The first number in cancer.data.shape is 569, which is the number of samples (rows).

  3. 3
    Identify the feature count

    The second number is 30, which is the number of features (columns) per sample.

Result

The class counts agree with the 569 rows, and the 30 is the column count. The class counts and the feature count describe different things.

Organizing the 30 Feature Names

To find out what each column in the data represents, look at the feature_names attribute. It gives the semantic meaning of each feature as a name. Printing it produces a list of 30 names. The names are not arbitrary. They follow a repeated pattern built from a small set of base measurements.

python
30 feature namesone name per columnmeanten base measurementserrorten base measurementsworstten base measurementsgroup 1group 2group 3
The 30 feature names split into three groups: mean, error, and worst. Each group is built from the same ten base measurements, so the list repeats one set of ten ideas three times.

The mean, error, and worst pattern across ten base measurements accounts for all 30 features: three versions of ten measurements gives 30. When you read the list, you can locate any feature by its group and its base measurement. This structure matters because it tells you what each column means before you ever run an algorithm on it. A column's position in the array is meaningless until you match it to a name from feature_names.

Why Unlabeled Data Has No Answer Key

The cancer dataset carries labels, which is what makes it useful for checking a supervised model. Unsupervised learning algorithms are usually applied to data with no label information, so the correct output is not known. This absence is the root of the evaluation problem. If nobody has written down what the right answer should be, there is nothing to measure the algorithm's output against.

QuestionLabeled dataUnlabeled data
What is known about each item?Its correct label or classOnly its features, with no label
Can the output be compared with a correct answer?Yes, against the known labelsNo, there is no answer key to compare against
How is success usually judged?By checking agreement with known answersBy a person inspecting whether the result serves the purpose

Labels provide the known correct answer that unsupervised data lacks.

The result can look orderly and still fail to be the division you cared about. Because of this, the source states plainly that it is very hard to say whether a model did well. A clean-looking output is not evidence of a correct output.

From Unlabeled Photographs to a Judged Result

It helps to follow the path that data takes through an unsupervised workflow. The input is a collection of unlabeled items. The algorithm organizes them into groups. Those groups are then the only evidence you have, and whether they are good has to be judged by a person looking at them. The diagram below shows this path. Notice that the final step does not come from the algorithm at all.

Unlabeled itemsfeatures only, no labelsClustering algorithmorganizes the itemsGroupsthe only evidence producedHuman inspectiondoes the grouping fit thepurpose?Judged resultaccepted or rejectedfed inproducesinspected by a personverdict
Unlabeled items enter the algorithm, which returns groups. The judgment step is performed by a person who inspects the groups against the purpose at hand, because no answer key exists.
Worked example

Two valid splits of face photographs

A clustering algorithm sorts a collection of face photographs into two piles. How do you decide whether the division is the one you wanted?

  1. 1
    Observe the output

    One pile contains faces shown in profile and the other contains full-face pictures.

  2. 2
    Recognize that the split is reasonable

    This is a perfectly reasonable way to divide a collection of face photographs.

  3. 3
    Compare with the purpose

    The trouble is that it may not be the division you were looking for. Perhaps you wanted a grouping by a different feature entirely.

  4. 4
    Judge by inspection

    The output alone does not reveal which split you intended, so a person must check the groups against the goal.

Result

Both the profile split and any other reasonable split are valid outputs from the algorithm's point of view. Only a human inspecting the result against the purpose can decide whether it is useful.

The important point is that the algorithm cannot be told which division is wanted. There is no instruction you can give it that says group by the feature I care about. This is why evaluation in unsupervised learning often shifts from comparing against an answer key to asking whether the groups make sense for the purpose at hand. The judgment depends on the person doing the inspection, which is why manual inspection is often the only practical way to evaluate the result.

Mistakes Beginners Make With This Workflow

  • Common mistake

    Assuming an orderly output is a correct output

    What it looks like

    The algorithm returns two tidy piles of photographs, and the piles look neat.

    Why it fails

    The source notes the result can look orderly and still fail to be the division you cared about.

    Do this instead

    Inspect the groups against the purpose you have in mind before accepting them.

  • Common mistake

    Reading the 30 in the shape as a class count

    What it looks like

    Thinking the dataset has 30 classes because cancer.data.shape ends in 30.

    Why it fails

    The shape reports rows then columns. The 30 counts features, while the labels are 212 malignant and 357 benign.

    Do this instead

    Read the first number as samples and the second as features, and use the class counts separately.

  • Common mistake

    Treating a column position as meaningful without its name

    What it looks like

    Assuming column 0 is a specific measurement because it is first.

    Why it fails

    Column meaning comes from feature_names, which gives the meaning of each feature as a name.

    Do this instead

    Print feature_names and match each column index to its name before interpreting it.

  • Common mistake

    Expecting the algorithm to accept an instruction about which grouping you want

Checking Your Understanding of the Dataset

EASY

Using the cancer object from this article, write the code that prints the number of features in the dataset, then explain which element of the shape it comes from and how it relates to the 30 names in feature_names.

Hints
  • The shape attribute of cancer.data returns rows and then columns.
  • The second number in the shape is the number of features.
MEDIUM

A colleague runs a clustering algorithm on unlabeled photographs and says the result is correct because the groups look clean. Describe the steps you would take to judge that claim, and explain why the lack of an answer key makes the claim hard to check.

Hints
  • Think about what evidence the algorithm produces and what evidence it does not.
  • Consider what the judgment step depends on in an unsupervised workflow.

Key Takeaways

  • The load_breast_cancer function returns the dataset as a Bunch object, which behaves like a dictionary and also lets you read values with dot notation such as cancer.data.
  • The data holds 569 samples with 30 features each, labeled as 212 malignant and 357 benign, and the shape reports rows then columns.
  • The feature_names attribute gives the meaning of each column, and its 30 names follow a mean, error, and worst pattern across ten base measurements.
  • Unlabeled data removes the known correct answer, so an unsupervised algorithm's result cannot be compared against an answer key and must be judged, often by manual inspection against the purpose at hand.

Related concepts

More concepts starting with S