Menu
CoddyTech

Features and Labels

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 53 of 60.

A model learns from inputs to answers, so you hand it two things: X, the features, and y, the labels, one per sample:

Six iris rows split in two: X, a grid of four measurements per row in light blue, and y beside it, one label per row (0, 0, 1, 1, 2, 2) in darker blue

A dataset loaded with as_frame=True has both ready:

iris = load_iris(as_frame=True)
X = iris.data
y = iris.target
print(X.shape, y.shape)

This prints:

(150, 4) (150,)

Row i of X is one flower and y[i] its species. The label stays out of X: a model that can read the answer learns nothing.

Then count the classes with y.value_counts(): iris has 50 of each, a balanced dataset.

challenge icon

Challenge

Medium

scikit-learn's handwritten digits are 8 by 8 pictures, each stored as 64 pixel numbers (its features) and labelled with the digit it shows:

Ten handwritten digits from load_digits, 0 to 9 in two rows of five, each a blocky 8 by 8 picture in dark ink on white

A teammate's split prints X: (1797, 65): their X still holds the answers.

  1. Fix X so it holds only the 64 pixel features.
  2. Below the ready lines, print how many samples show the digit d the program reads.

For the input 3:

X: (1797, 64)
y: (1797,)
Samples of digit 3: 183

Try it yourself

from sklearn.datasets import load_digits

digits = load_digits(as_frame=True)
d = int(input())

# The features and the labels
X = digits.frame
y = digits.target

# Don't change the lines below
print(f"X: {X.shape}")
print(f"y: {y.shape}")

# 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 Data and Plots

Practice on your own: Online Python compiler