Menu
CoddyTech

Exploring by Class

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

Which measurement gives a species away? Put every flower on a line by its petal length, one row per species:

Iris flowers placed by petal length, one row per species: setosa clusters near 1.5 cm, versicolor near 4.3 cm and virginica near 5.6 cm, each class mean marked with a black bar

groupby on the label computes those means:

df = iris.frame
means = df.groupby("target")["petal length (cm)"].mean()
print(means)

This prints:

target
0    1.462
1    4.260
2    5.552
Name: petal length (cm), dtype: float64

The class means sit far apart, so petal length separates the classes: a model will lean on it.

A ratio says how far: means[2] / means[1] is about 1.3. The further from 1, the better the feature tells those two classes apart.

challenge icon

Challenge

Medium

Which cell measurement gives a malignant sample away? In load_breast_cancer, class 0 is malignant and class 1 benign. The starter reads some feature names into features.

  1. For each feature, print its malignant mean divided by its benign mean, rounded to 2 decimals with round.
  2. After the loop, name the feature with the largest ratio.

For the features worst radius and worst texture:

worst radius: malignant is 1.58 times benign
worst texture: malignant is 1.25 times benign
Best separator: worst radius

Try it yourself

from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer(as_frame=True)

# The features to compare
n = int(input())
features = []
for i in range(n):
    features.append(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