Menu
CoddyTech

Features Against the Label

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

Which features move with the label? Correlate each one with it, 0 for malignant and 1 for benign, and rank them by strength:

Horizontal bars of r for the ten mean features of the tissue data, strongest on top: mean concave points -0.78, mean perimeter -0.74, mean radius -0.73, down to mean fractal dimension at 0.01

X.corrwith(y) gives every feature's r at once:

X = cancer.data[cancer.feature_names[:10]]
r = X.corrwith(cancer.target)
strength = np.abs(r).sort_values(ascending=False)
for name in strength.index[:3]:
    print(name, np.round(r[name], 2))

This prints:

mean concave points -0.78
mean perimeter -0.74
mean radius -0.73

A negative r means the feature is larger in class 0: malignant cells have more concave points. np.abs ranks by strength, whatever the sign.

Near 0, like fractal dimension, a feature barely differs between the classes: a candidate to leave out.

challenge icon

Challenge

Medium

Which tissue features should a model try first? The inputs are a group, error or worst, and k; the starter puts the group's features in X.

  1. Rank the features by the strength of their r with y.
  2. Print the top k with their signed r, to 2 decimals.
  3. Check the sign: print the strongest feature's class means.

For the mean group and k = 2:

Top 2 mean features:
mean concave points: -0.78
mean perimeter: -0.74
mean concave points by class: malignant 0.09, benign 0.03

Try it yourself

import numpy as np
from sklearn.datasets import load_breast_cancer

group = input()
k = int(input())
cancer = load_breast_cancer(as_frame=True)
df = cancer.frame
y = cancer.target
# The ten features of the group: mean, error or worst
start = {"mean": 0, "error": 10, "worst": 20}[group]
X = cancer.data[cancer.feature_names[start:start + 10]]

# 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