Concepts / Loading Datasets with Scikit-Learn

Loading Datasets with Scikit-Learn

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

  • Programming

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.

python
Notes on the code
  • 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
importload_breast_cancerfrom sklearn.datasetsload_breast_cancer()Bunch objectdataset plus informationabout itcancercall oncereturnsassigned to
What happens when load_breast_cancer is called, and how the returned Bunch flows into the variable you then inspect?

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.

Definition

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.

python
Notes on the code
  • Bracket access using the string key 'data'
  • Dot access to the same attribute; cancer.data is an alternative to cancer['data']
cancerBunch objectdatathe actual data arrayfeature_namesname for each of the 30featuresinformation about thedatasetcancer.data or cancer['data']cancer.feature_namesstored alongside the data
What does the Bunch object contain, and how do the attributes data and feature_names relate to the object and to each other?

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.

python
Notes on the code
  • cancer.data is 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
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.

row 0first data pointcolumn 0one featurerow 1second data pointcolumn 1one featurerow 568last of 569 data pointscolumn 29last of 30 featuresrow holds 30 valuesone value per column
Which dimension of the data array is samples and which is features, and how each row and each column is used?

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.

Formula
Class labelNumber of data points
malignant212
benign357
total569

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.

python
Notes on the code
  • feature_names lists one name per column of cancer.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.

30 feature namesmean10 base measurementserror10 base measurementsworst10 base measurementssummary formsummary formsummary form
How are the 30 feature names grouped, and how do the ten base measurements combine with the mean, error, and worst summaries to produce them?

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 like
    print(cancer.data.shape) and reading 30 as the number of data points
    Why it fails

    The source says the shape is printed as rows then columns, and the 569 data points are the rows.

    Do this instead

    Read 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 like

    Indexing cancer[0] and expecting the first data point

    Why it fails

    A Bunch behaves like a dictionary, so its contents are reached by key names rather than by position.

    Do this instead

    Use 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 like

    Expecting cancer.data and cancer['data'] to return separate copies with different contents

    Why it fails

    The source describes dot access as an alternative to bracket access for the same key.

    Do this instead

    Pick 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 like

    Trying to memorize all 30 names one by one

    Why it fails

    The names follow a repeated pattern built from ten base measurements.

    Do this instead

    Learn the ten base measurements and the three summary forms, then combine them.

Practice: Reading the Dataset

EASY

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
MEDIUM

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_names gives one name for each column of the data array
  • The len function returns how many items a list holds

Key Takeaways

  • The load_breast_cancer function 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.data is 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_names attribute names each of the 30 features, which follow a mean, error, and worst pattern across ten base measurements.

Related concepts

More concepts starting with L