Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 40 ยท Getting Started

Datasets and Data Formats

Learn the three families of scikit-learn datasets (load, fetch, make), the Bunch object, how to use DataFrames, and what shape X and y must have.

Before you can model anything you need data in the right shape. scikit-learn expects a feature matrix X with shape (n_samples, n_features) and, for supervised learning, a target vector y with shape (n_samples,). This lesson shows the built-in datasets, the generators that create synthetic data, and how pandas fits in.

Three families of dataset functions

Prefix What it does Example
load_* Small datasets bundled with the library load_iris, load_wine, load_diabetes
fetch_* Larger datasets downloaded on first use fetch_california_housing
make_* Synthetic data generated on the fly make_classification, make_blobs

The fetch_ functions need a network connection, so in this course we use load_ and make_ datasets.

The Bunch object

load_* functions return a Bunch, which is a dictionary whose keys also work as attributes.

Python Inspect a Bunch

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.

DESCR is a built-in description of the dataset, including its source and meaning of each column.

Shortcut: return_X_y

Most of the time you only want the arrays:

Python
X, y = load_wine(return_X_y=True)

And if you prefer a pandas DataFrame, pass as_frame=True.

Python Datasets as pandas objects

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.

This loads a regression dataset: ten body measurements, with a numeric disease-progression score as the target.

Generating synthetic data

When you want to experiment with an algorithm or demonstrate a point, generators give you data with exactly the properties you choose. Always pass random_state so the results are repeatable.

Python make_classification and make_blobs

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.

  • make_classification builds labelled classes, with some useful features and some noise.
  • make_blobs makes clusters, ideal for clustering demos.
  • make_regression creates a numeric target from a linear combination of features plus noise.
  • Others include make_moons and make_circles for non-linear class boundaries.

Using your own data

Real projects start with a CSV, a database or an Excel sheet. Read it with pandas, then split it into features and target. scikit-learn accepts DataFrames and Series directly.

Python DataFrame in, predictions out

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.

When you fit on a DataFrame, the model remembers the column names (feature_names_in_) and warns you if you later predict with different ones. That catches a common class of bugs.

Common shape problems

X must be 2-D even with a single feature. If you have a 1-D array x, use x.reshape(-1, 1) or x[:, None], or you will get an error that begins "Expected 2D array, got 1D array instead".

Python Fixing a 1-D feature

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.

Other rules: all features must be numeric (text categories are encoded in Encoding Categorical Features), and missing values must be handled first (Missing Values), except in a few models that tolerate them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What shape should X have for 100 samples with 5 features?

    Rows are samples and columns are features, so (n_samples, n_features) = (100, 5).
  2. 2. Which function creates synthetic cluster data?

    Functions that generate synthetic data start with make_.
  3. 3. What does as_frame=True do in load_diabetes(as_frame=True)?

    The Bunch gains .frame (a DataFrame), and data and target become pandas objects.
  4. 4. You have a 1-D array x with one feature. How do you pass it as X?

    Reshaping to (n_samples, 1) makes it the 2-D matrix scikit-learn expects.

Summary

  • X is (n_samples, n_features); y is (n_samples,).
  • load_* ships with the library, fetch_* downloads, make_* generates data.
  • return_X_y=True gives plain arrays and as_frame=True gives pandas objects.
  • Fit on a DataFrame and the model remembers feature names.
  • Reshape single-feature arrays with reshape(-1, 1).

Next: The Estimator API