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.
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
Mediumerror 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.
- Print
r between them:and their r, to 2 decimals. - Draw one scatter per class,
firstacross, labelled by class name. - Call
label(ax), add a legend, show it.
Like this, for the strongest worst pair (r 0.82):
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life8Comparing Groups
Box Plots by GroupDifference in MeansCould It Be Chance?P-ValuesThe t-TestRecap: A/B Test11Exploring a Dataset
A First LookEvery Feature at OnceFeatures Against the LabelTwo Best FeaturesRecap: Diabetes Data3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler