Supervised Learning Algorithms
Machine learning is much easier to learn when you have a real dataset to experiment with.
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.
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?
Reveal 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'].
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.
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.
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?
- 1Add the class counts
212 plus 357 gives 569.
- 2Compare with the row count
The first number in
cancer.data.shape is 569, which is the number of samples (rows). - 3Identify the feature count
The second number is 30, which is the number of features (columns) per sample.
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.
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.
| Question | Labeled data | Unlabeled data |
|---|---|---|
| What is known about each item? | Its correct label or class | Only its features, with no label |
| Can the output be compared with a correct answer? | Yes, against the known labels | No, there is no answer key to compare against |
| How is success usually judged? | By checking agreement with known answers | By 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.
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?
- 1Observe the output
One pile contains faces shown in profile and the other contains full-face pictures.
- 2Recognize that the split is reasonable
This is a perfectly reasonable way to divide a collection of face photographs.
- 3Compare 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.
- 4Judge by inspection
The output alone does not reveal which split you intended, so a person must check the groups against the goal.
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 likeThe algorithm returns two tidy piles of photographs, and the piles look neat.
Why it failsThe source notes the result can look orderly and still fail to be the division you cared about.
Do this insteadInspect 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 likeThinking the dataset has 30 classes because cancer.data.shape ends in 30.
Why it failsThe shape reports rows then columns. The 30 counts features, while the labels are 212 malignant and 357 benign.
Do this insteadRead 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 likeAssuming column 0 is a specific measurement because it is first.
Why it failsColumn meaning comes from
feature_names, which gives the meaning of each feature as a name.Do this insteadPrint 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
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.datareturns rows and then columns. - The second number in the shape is the number of features.
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_cancerfunction returns the dataset as a Bunch object, which behaves like a dictionary and also lets you read values with dot notation such ascancer.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_namesattribute 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.