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:
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: float64The 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
MediumWhich 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.
- For each feature, print its malignant mean divided by its benign mean, rounded to 2 decimals with
round. - 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 radiusTry 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
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