Concepts / Loading Built-In Datasets in scikit-learn

Loading Built-In Datasets in scikit-learn

Synthetic datasets are made-up datasets, designed on purpose to highlight particular aspects of an algorithm, and they can be small or large.

  • Programming

Why Algorithms Need Test Data

When you learn a machine learning algorithm, you need data to test it on. The datasets used for this purpose come in two broad kinds. Some are small and synthetic, which means they are made up. Others are large and come from real-world applications. Both kinds have a role, and understanding why each one exists makes the rest of the algorithm material easier to follow.

What do you think happens?

The forge dataset is loaded into a variable X and you run print(X.shape). Before reading on, predict what gets printed. How many rows and how many columns do you expect?

  • (2, 26)

  • (26, 2)

  • (40, 1)

  • (26, 26)

Reveal answer
Answer:

(26, 2)

The forge dataset has 26 data points, each stored as one row of X, and 2 features, each stored as one column. X.shape reports rows first and columns second, so the output is (26, 2).

Synthetic Versus Real-World Data

Definition

A synthetic dataset is made up on purpose. Its designer chooses its shape so that it highlights one particular aspect of an algorithm. Synthetic datasets can be small or large, but their defining feature is that someone constructed them to illustrate a behavior.

A real-world dataset works the other way around. It is collected from an application that exists outside the textbook, so its structure is whatever the real process produced. Nobody arranged its features to make one algorithm look clear. The Wisconsin Breast Cancer dataset, often called cancer for short, is an example. It records clinical measurements of breast cancer tumors, and each tumor is labeled as benign, meaning harmless, or malignant, meaning cancerous. The task is to learn to predict whether a tumor is malignant from measurements of the tissue.

PropertySynthetic datasetReal-world dataset
OriginMade up on purpose by a designerCollected from a real application
ShapeChosen to highlight one behaviorWhatever the real process produced
Typical sizeCan be small or largeOften large
Example in this articleforge and waveWisconsin Breast Cancer (cancer)
Best used forBuilding understanding of a specific behaviorTesting that understanding against a real application

Synthetic and real-world datasets serve complementary roles.

Neither kind replaces the other. The synthetic sets make a specific behavior easy to see, and the real-world sets show whether that behavior appears in practice.

Reading the Shape of the Forge Dataset

The first dataset this course uses is the forge dataset. It is a synthetic two-class classification dataset with two features. The data is split into two parts. X holds the data points, and y holds the class labels. The printed shape is the fastest way to check what a dataset contains.

python
Output (expected)
(26, 2)
Notes on the code
  • X is a table: each row is one data point and each column is one feature
  • The first number is the count of rows, which is 26 data points
  • The second number is the count of columns, which is 2 features
26rows: data points2columns: features
Which number in the printed shape counts the rows, and which counts the columns?

Because the labels in y are kept separately, with one label per point, they line up with the rows of X. Row 0 of X and the first entry of y describe the same data point. Reading the shape therefore tells you the size of both parts at once: 26 points, each with 2 feature values, and 26 labels.

From Rows to Points on a Scatter Plot

Printing a shape tells you how big the dataset is, but not what it looks like. A scatter plot fills that gap. In the forge plot, the first feature sits on the horizontal axis and the second feature sits on the vertical axis. Each data point becomes one dot, placed at the coordinates given by its two feature values.

Data rowSingle pointFeature 1(horizontal axis)Feature 2 (verticalaxis)Class (color andshape)one row = one pointsets x-positionsets y-positionshows class
Each row of the table becomes one point, positioned by its two feature values and colored by its class.

The color and shape of each dot show the point's class, so you can see at a glance which points belong to Class 0 and which belong to Class 1. This is the main reason scatter plots are useful for classification data. They let you check whether the classes separate, overlap, or are mixed together. Patterns in the dots, not in the printed numbers, reveal how the features relate to the classes.

The Wave Dataset and Feature-Versus-Target Plots

The forge dataset answers a classification question: which class does a point belong to? For regression algorithms, the book uses a different synthetic dataset called wave. It has a single input feature and a continuous target variable, which is the response the algorithm tries to model. The code creates 40 points, and the plot puts the feature on the horizontal axis and the target on the vertical axis.

The target is plotted as a continuous quantity, not as a set of colored classes. Each dot still corresponds to one row: its horizontal position is its feature value, and its vertical position is its target value. Because the target is numeric, the plot shows how the response changes as the feature changes.

Worked example

Predicting the shape of the wave data

The wave dataset has 40 points, one input feature, and one continuous target. What shape would X have, and what would y contain?

  1. 1
    Count the data points

    The dataset is created with 40 points, so X has 40 rows.

  2. 2
    Count the features

    The wave dataset has a single input feature, so X has 1 column.

  3. 3
    Describe the target

    y holds one continuous numeric value per point, so it also has 40 entries, one per row of X.

Result

X has shape (40, 1), and y holds 40 continuous target values. Compared with the forge shape (26, 2), the wave data has more points but only one feature.

Both datasets come from the same mglearn module and both are generated by a make function, but their roles differ. The forge data asks which class a point belongs to. The wave data asks what numeric value goes with each feature value.

Why Small Datasets Can Mislead

Simple datasets are chosen because they are easy to see. A printed page has two dimensions, so a dataset with two features can be drawn directly, with one feature per axis. Data with many more features is hard to show at all. Datasets with few features are called low-dimensional, and datasets with many features are called high-dimensional.

2 featuresMany featuresScatter plotNo direct drawingVisible structureIntuition may nothold
Low-dimensional data can be drawn directly, while high-dimensional data cannot, so intuition built on the drawings may not transfer.

Mistakes Learners Make with Dataset Structure

  • Common mistake

    Reading the shape numbers in the wrong order

    What it looks like

    Reading (26, 2) as 26 features and 2 data points.

    Why it fails

    X.shape reports rows first. Rows are data points and columns are features.

    Do this instead

    Read the first number as rows (data points) and the second as columns (features).

  • Common mistake

    Thinking the labels are stored as extra columns of X

    What it looks like

    Expecting the forge shape to include a third column for the class.

    Why it fails

    The labels in y are kept separately, one per point, so X keeps only the feature columns.

    Do this instead

    Match each row of X with the same position in y to find its class.

  • Common mistake

    Treating a scatter plot's colors as features

    What it looks like

    Assuming the color of each dot is a third feature.

    Why it fails

    In the forge plot, color and shape show the class. The axes carry the two feature values.

    Do this instead

    Use the horizontal and vertical positions for features and the color or shape for the class.

  • Common mistake

    Assuming a 2D picture generalizes to any dataset

    What it looks like

    Concluding that an algorithm will behave the same way on a dataset with fifty features because it looked clean on two.

    Why it fails

    Intuition from low-dimensional data may not hold in high-dimensional data.

    Do this instead

    Treat small-dataset lessons as a starting point and test them on data with more features.

Practice Reading a Dataset Before Plotting It

EASY

A dataset loads with X.shape equal to (40, 1) and y containing continuous numbers. Without running any plot, describe how many points the dataset has, how many features each point has, what kind of target it has, and whether it suits a classification or a regression algorithm. Then say what a feature-versus-target scatter plot of this data would put on each axis.

Hints
  • The first number in the shape counts rows, and rows are data points.
  • The second number counts columns, and columns are features.
  • A continuous target means a numeric response, not a set of colored classes.

Key Takeaways

  • Synthetic datasets are made up on purpose so that they highlight a particular aspect of an algorithm, while real-world datasets are collected from applications and test whether a behavior appears in practice.
  • X.shape reports rows as data points and columns as features; the forge dataset's shape (26, 2) means 26 points with 2 features.
  • Scatter plots show each data point as one dot positioned by its feature values, and colors or shapes reveal class membership; feature-versus-target plots show how a continuous target changes with a feature.
  • Intuition built on low-dimensional datasets, which can be drawn directly, may not hold for high-dimensional datasets, so small examples should be used with that limit in mind.

Related concepts

More concepts starting with L