Menu
CoddyTech

Two Best Features

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

Here is the picture this chapter promised: the two strongest mean features in one scatter, one color per class.

Tissue samples by mean concave points across and mean perimeter up: benign samples crowd the lower left, malignant ones spread up and to the right, the two clouds meeting in a narrow band

Benign samples crowd the lower left, malignant ones spread up and right. Where the clouds meet, a model using this pair will make its mistakes.

One more check: are the two near copies?

first = strength.index[0]
second = strength.index[1]
print(np.round(df[first].corr(df[second]), 2))

This prints 0.85: related, not copies. At 0.99 the second feature would repeat the first and add almost nothing new.

challenge icon

Challenge

Medium

error features measure how much a measurement varied across one sample's cells. Can the strongest two tell the classes apart? The starter ranks them into first and second; label(ax) names the chart.

  1. Print r between them: and their r, to 2 decimals.
  2. Draw one scatter per class, first across, labelled by class name.
  3. Call label(ax), add a legend, show it.

Like this, for the strongest worst pair (r 0.82):

A scatter titled Worst Features, worst concave points across and worst perimeter up, malignant samples in blue up and to the right, benign in orange at the lower left

Then judge your chart: separate clouds? And with your r, copies?

Try it yourself

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

cancer = load_breast_cancer(as_frame=True)
df = cancer.frame
y = cancer.target
# The ten error features: how much each measurement varied across the cells of one sample
X = cancer.data[cancer.feature_names[10:20]]

# Ranked by strength: first and second are the two strongest
strength = np.abs(X.corrwith(y)).sort_values(ascending=False)
first = strength.index[0]
second = strength.index[1]


# Sets the chart's title and names the axes after the pair
def label(ax):
    ax.set_title("Error Features")
    ax.set_xlabel(first)
    ax.set_ylabel(second)


# 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