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:
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
MediumDoctors 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.
- Load the dataset with
as_frame=True. - Print how many samples (rows of the frame) and features it has, and its class names.
- 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.89Try it yourself
n = int(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature Grid5Line and Scatter Plots
Labels and TitlesScatter PlotsSeveral Lines and LegendsColors and MarkersFigures and AxesRecap: Weekly Steps11Datasets for ML
Built-in DatasetsFeatures and LabelsExploring by ClassPlotting the ClassesRecap: Wine DatasetPractice on your own: Online Python compiler