Menu
CoddyTech

Plotting the Classes

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

Do the classes form separate clouds? Draw one scatter per class, each named from target_names:

Iris flowers by sepal length and width, one color per species: setosa forms its own cloud at the upper left, while versicolor and virginica overlap in the middle and right

The code behind it:

df = iris.frame
fig, ax = plt.subplots()
for i in range(3):
    group = df[df["target"] == i]
    ax.scatter(group["sepal length (cm)"], group["sepal width (cm)"], label=iris.target_names[i])
ax.set_title("Iris flowers by sepal size")
ax.set_xlabel("sepal length (cm)")
ax.set_ylabel("sepal width (cm)")
ax.legend()
plt.show()

Each call gets its own color, and label=iris.target_names[i] turns 0, 1 and 2 into names for the legend.

Setosa stands apart; versicolor and virginica overlap. Where clouds overlap is where a model will make its mistakes.

challenge icon

Challenge

Medium

Do malignant and benign samples form separate clouds? A teammate's chart cannot tell: it draws all of load_breast_cancer with one scatter, in one color.

  1. Replace that scatter with one per class: mean radius across, mean concave points up.
  2. Label each with its class name from target_names.

Keep the ready lines. Yours should look like this one, drawn from two other measurements:

A scatter of breast tissue samples by mean texture and mean smoothness, malignant in blue and benign in orange, with a legend naming both classes

Your program prints nothing: Coddy checks the chart and describes it.

Try it yourself

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

cancer = load_breast_cancer(as_frame=True)
df = cancer.frame

# Don't change the line below
fig, ax = plt.subplots()

# Fix the code here
ax.scatter(df["mean radius"], df["mean concave points"])

# Don't change the lines below
ax.set_title("Cells by Diagnosis")
ax.set_xlabel("mean radius")
ax.set_ylabel("mean concave points")
ax.legend()
plt.show()
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