Menu
CoddyTech

Every Feature at Once

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 57 of 64.

Thirty features need thirty histograms. They read faster as one grid of small charts, like these four iris features:

Four small histograms in a 2 by 2 grid, one per iris feature: the two sepal histograms are single humps, the two petal histograms each split into a small block on the left and a wide hump on the right

A loop fills the grid, chart i with feature i:

names = iris.feature_names
fig, axs = plt.subplots(2, 2)
for i in range(4):
    ax = axs.flat[i]
    ax.hist(iris.frame[names[i]], bins=15)
    ax.set_title(names[i])
fig.tight_layout()
plt.show()

plt.subplots(2, 2) makes a table of charts, and axs.flat lines them up row by row:

A grid of 2 rows and 3 columns, its cells numbered axs.flat 0 to 5 along row 0 first, then row 1, each with its row and column written below

fig.tight_layout() keeps the titles apart. Now read: both petal histograms split in two. A gap like that often means a class sits apart, so keep that feature.

challenge icon

Challenge

Medium

See the tissue data whole. The input picks a group, mean, error or worst, and the starter puts its first six feature names in names.

  1. Make a grid of 2 rows and 3 columns.
  2. In chart i, draw a histogram of feature i with 20 bins, titled with its name.
  3. Tidy the layout, then show it.

Like this grid of six wine features:

Six small histograms in 2 rows of 3, titled alcohol, malic_acid, ash, alcalinity_of_ash, magnesium and total_phenols, each with 20 bars

Your program prints nothing: Coddy describes each chart.

Try it yourself

import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer

group = input()
cancer = load_breast_cancer(as_frame=True)
df = cancer.frame
# The first six feature names of the group: mean, error or worst
start = {"mean": 0, "error": 10, "worst": 20}[group]
names = cancer.feature_names[start:start + 6]

# 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 Statistics for ML

Practice on your own: Online Python compiler