Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 111 ยท Getting Started

Working with Datasets

Load scikit-learn toy datasets, generate synthetic data with make_classification, make_regression, make_blobs and make_moons, and convert pandas to arrays.

You cannot learn machine learning without data, and early on you do not need big or messy data. scikit-learn ships a few small real datasets and a family of generators that create synthetic data on demand. Both are ideal for learning because they load instantly, need no files, and let you see exactly what an algorithm does.

This lesson shows how to get data, what shape it comes in, how to move between pandas and arrays, and why generated data with a fixed seed is such a good teaching tool. The same habits transfer straight to your own datasets.

Built-in toy datasets

The sklearn.datasets module has loaders such as load_iris, load_wine, load_breast_cancer, load_digits and load_diabetes. They come bundled with the library, so there is no download. Each loader returns a Bunch, a dictionary-like object with attributes.

Python Opening a toy dataset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two forms are worth remembering. return_X_y=True gives you the pair you pass to fit. as_frame=True gives a pandas DataFrame, which is better for looking around. The DESCR attribute holds a text description of where the data came from.

Toy datasets are clean, small and well known. Real data is usually messier, larger and less friendly, so treat these as practice material and not as proof that a method will work on your own problem.

Shapes: X and y

By convention, features go in X with shape (n_samples, n_features) and labels go in y with shape (n_samples,). A quick shape check catches many mistakes before they turn into confusing errors.

When your data starts life as a pandas DataFrame, split it into the feature columns and the target column. scikit-learn accepts DataFrames directly, and it also remembers the column names.

Python From a DataFrame to X and y

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the double brackets in df[["area", "rooms"]]. A single pair of brackets would give a one-dimensional Series, which scikit-learn rejects as X. If you need more on selecting columns, see columns and rows in pandas.

Generating synthetic data

When you want a problem with a known shape, you generate it. The make_* functions create data whose structure you control. Because you decide how hard the problem is, you can watch how a method reacts when the data is noisy, imbalanced or non-linear.

Function What it makes
make_classification A classification problem with informative and noisy features
make_regression A regression problem with a known linear signal plus noise
make_blobs Round clusters, ideal for clustering and simple classification
make_moons Two interleaving half circles, a classic non-linear problem
Python Four generators, four shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Useful knobs: n_informative is how many features really carry signal, weights makes classes imbalanced, noise and cluster_std control how blurry things are, and flip_y randomly flips labels to simulate label errors. Later lessons use these to show overfitting, imbalance and regularization on demand.

Fixed seeds and reproducibility

Random generators produce different data on every run unless you fix the seed. The random_state argument does that. The same seed always gives the same data, which means your results, and the numbers quoted in a tutorial, can be reproduced exactly.

Python random_state makes data repeatable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Use a fixed seed while learning and debugging. When you evaluate a real model, try several seeds to see how much the result depends on luck; cross-validation formalizes that idea.

Why synthetic data is so useful

Synthetic data has one big advantage: you know the truth. If you generate y = 3x + 5 plus noise, you can check whether a model recovers 3 and 5. If you generate two clusters, you know the correct answer to a clustering algorithm. This lets you test your understanding and your code in a way real data never can, because with real data the true pattern is unknown.

It also has a limit: synthetic data is usually simpler and cleaner than reality. A model that does well on make_moons has proven little about your customers. Use generated data to learn how methods behave, then confirm on real data.

Where data can come from

Toy datasets Small, real and bundled.
make_ generators Synthetic data you control.
Your own tables Messy, real, and what matters.

Start synthetic to learn, finish real to prove it.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What shape should X have for 200 samples and 4 features?

    X is (n_samples, n_features), so 200 rows and 4 columns.
  2. 2. What does load_iris(return_X_y=True) return?

    return_X_y=True gives the pair (X, y) ready to pass to fit.
  3. 3. Why pass random_state to make_blobs?

    A fixed seed makes the generator deterministic, so results can be reproduced.
  4. 4. Which generator creates two interleaving half circles that a straight line cannot separate?

    make_moons produces the classic non-linear two-class problem.

Summary

  • sklearn.datasets has bundled toy datasets (load_*) and generators (make_*); use return_X_y=True or as_frame=True as needed.
  • X is (n_samples, n_features) and y is (n_samples,); select feature columns with double brackets in pandas.
  • make_classification, make_regression, make_blobs and make_moons create data whose difficulty you control.
  • A fixed random_state makes data and results reproducible.
  • Synthetic data lets you know the truth, but it is cleaner than reality, so confirm on real data.

Next: Train, Validation and Test Sets