Loading Datasets with Scikit-Learn
Machine learning is much easier to learn when you have a real dataset to experiment with.
Starting With a Ready-Made Dataset
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, so you do not have to find, download, or clean your own data before you start. The breast cancer dataset is one of them. In this article you will load it, run a few lines of code, and examine what comes back.
Every step in this article starts with code you can trace. Rather than trusting a description, you will run the code and compare what Python prints with what the text says it should print. That habit will help you check your understanding throughout your machine learning work.
Tracing the Load Call
The data is loaded with the load_breast_cancer function. The function is imported from the sklearn.datasets module, called once, and its result is stored in a variable named cancer. Nothing else is needed to get the dataset into your program.
- Import the loader function from the sklearn.datasets module
- Call the function once; its return value is the dataset
- Store the returned object in the name cancer
- Print the keys that the loaded object provides
Read the flow from top to bottom. The import line makes the function available, the call produces a Bunch object, and the assignment gives that object the name cancer. Every later line in this article refers to the dataset through that name.
What a Bunch 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 information about the dataset is what lets you understand the data, not just store it.
A Bunch is an object that behaves like a dictionary, with the added benefit that its values can be read using a dot. So bunch.key is an alternative to bunch['key'].
Because a Bunch is dictionary-like, calling keys() on it lists the names stored inside. The keys are what you use to find out what the object contains. The next section explains how to reach each value once you know its name.
Two Ways to Reach a Value
A key such as data names one stored value inside the Bunch. You can reach that value with dictionary-style brackets or with dot notation, and the source states that both are alternatives for the same access. Dot notation is shorter and easier to read when the key is a valid name.
- Bracket access using the string key 'data'
- Dot access to the same attribute;
cancer.datais an alternative to cancer['data']
Reading the Shape: 569 Samples by 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. So the first number is the count of data points, and the second number is the count of features.
cancer.datais the data array- .shape reports its dimensions as rows then columns
What do you think happens?
Before running it, what do you think print(cancer.data.shape) displays, and which number is the count of data points?
Reveal answer
(569, 30). The first number, 569, is the number of rows, one per data point. The second number, 30, is the number of columns, one per feature.
The source says printing cancer.data.shape shows the number of rows and then the number of columns, and the dataset has 569 data points with 30 features each.
Read the diagram as a grid. Each row is one data point, so there are 569 rows. Each column is one feature, so there are 30 columns. Rows are counted first in the shape output, which is why 569 comes before 30.
Malignant and Benign Counts
The 569 data points are split into two classes. The dataset is labeled as 212 malignant and 357 benign. These two per-class counts add up to the total number of data points, which is a useful check on your understanding.
| Class label | Number of data points |
|---|---|
| malignant | 212 |
| benign | 357 |
| total | 569 |
Per-class sample counts in the breast cancer dataset
Notice that the benign class is the larger of the two, with 357 of the 569 data points. The malignant class has 212. Keeping these numbers straight matters later, because a model's results are interpreted against how many examples each class contributes.
Decoding the 30 Feature Names
The data array has 30 columns, but the numbers in it do not explain themselves. To find out what each column 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.
feature_nameslists one name per column ofcancer.data- len counts the names, which should match the 30 feature columns
The names are not arbitrary. The source describes a repeated pattern: a small set of ten base measurements, each of which appears in three summary forms. Those three forms are the mean, the error, and the worst value. Ten base measurements times three summaries gives the 30 features.
When you read a feature name, look for the summary word first. A name that carries the mean form describes the average of a base measurement, and the error and worst forms describe the other two summaries. Each of the three groups contains the same ten base measurements, so once you have learned the ten, the other twenty names follow the same logic.
Mistakes When Reading the Bunch
Common mistake Assuming the shape output lists features first
What it looks likeprint(cancer.data.shape) and reading 30 as the number of data pointsWhy it failsThe source says the shape is printed as rows then columns, and the 569 data points are the rows.
Do this insteadRead the first number as the count of data points and the second as the count of features.
Common mistake Treating a Bunch like an ordinary list
What it looks likeIndexing cancer[0] and expecting the first data point
Why it failsA Bunch behaves like a dictionary, so its contents are reached by key names rather than by position.
Do this insteadUse a key such as cancer.data or cancer['data'] to reach a stored value.
Common mistake Believing dot notation and bracket notation give different data
What it looks likeExpecting cancer.data and cancer['data'] to return separate copies with different contents
Why it failsThe source describes dot access as an alternative to bracket access for the same key.
Do this insteadPick whichever spelling reads better in your code; both reach the same stored value.
Common mistake Assuming the feature names are unrelated words
What it looks likeTrying to memorize all 30 names one by one
Why it failsThe names follow a repeated pattern built from ten base measurements.
Do this insteadLearn the ten base measurements and the three summary forms, then combine them.
Practice: Reading the Dataset
Load the breast cancer dataset into a variable named cancer. Then predict, before running anything, what the following will show: the number of data points, the number of features, the number of malignant examples, and the total of the two class counts. Run your code and compare each result with your prediction.
Hints
- The number of data points is the first number in
cancer.data.shape - The malignant and benign counts are 212 and 357, and they should sum to the number of data points
- Use dot notation such as cancer.feature_names to reach each attribute
Explain in one or two sentences why the feature_names list has the same length as the number of columns in cancer.data, and how you would confirm it with a single line of code.
Hints
feature_namesgives one name for each column of the data array- The len function returns how many items a list holds
Key Takeaways
- The
load_breast_cancerfunction from sklearn.datasets returns the breast cancer dataset as a Bunch object, which is commonly stored in a variable such as cancer. - A Bunch behaves like a dictionary, and its values can be read with dot notation, so
cancer.datais an alternative to cancer['data']. - The data has 569 data points with 30 features each, reported as rows then columns by
cancer.data.shape, and the classes are labeled 212 malignant and 357 benign. - The
feature_namesattribute names each of the 30 features, which follow a mean, error, and worst pattern across ten base measurements.