Menu
CoddyTech

Built-in Datasets

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 52 of 60.

Soon you will train models, and this chapter gets their data ready. scikit-learn, the ML library, ships classic datasets with it, like these iris flowers:

Six rows of iris.frame as a table: four measurement columns in light blue, a target column holding 0, 1 or 2 in darker blue, and each number's species name beside it

One line loads them. from ... import takes a single function from a module, so you call it by its bare name:

from sklearn.datasets import load_iris

iris = load_iris(as_frame=True)
print(iris.frame.shape)
print(iris.target_names)

This prints:

(150, 5)
['setosa' 'versicolor' 'virginica']

as_frame=True asks for pandas, so iris.frame is a DataFrame and iris.feature_names lists its measurements. target holds numbers, which models need: iris.target_names[2] names class 2.

challenge icon

Challenge

Medium

Doctors measured cells in breast tissue samples and labelled each one malignant or benign. scikit-learn ships them as load_breast_cancer.

The program reads n, a feature's position in feature_names.

  1. Load the dataset with as_frame=True.
  2. Print how many samples (rows of the frame) and features it has, and its class names.
  3. Print feature n's name and its mean, rounded to 2 decimals.

For the input 3:

Samples: 569
Features: 30
Classes: ['malignant' 'benign']
mean area: 654.89

Try it yourself

n = int(input())

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler