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:
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
MediumDo 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.
- Replace that scatter with one per class:
mean radiusacross,mean concave pointsup. - Label each with its class name from
target_names.
Keep the ready lines. Yours should look like this one, drawn from two other measurements:
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()
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