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:
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.73A 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
MediumWhich 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.
- Rank the features by the strength of their r with
y. - Print the top
kwith their signed r, to 2 decimals. - 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.03Try 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
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