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:
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
Mediumscikit-learn's handwritten digits are 8 by 8 pictures, each stored as 64 pixel numbers (its features) and labelled with the digit it shows:
A teammate's split prints X: (1797, 65): their X still holds the answers.
- Fix
Xso it holds only the 64 pixel features. - Below the ready lines, print how many samples show the digit
dthe program reads.
For the input 3:
X: (1797, 64)
y: (1797,)
Samples of digit 3: 183Try 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature Grid5Line and Scatter Plots
Labels and TitlesScatter PlotsSeveral Lines and LegendsColors and MarkersFigures and AxesRecap: Weekly Steps11Datasets for ML
Built-in DatasetsFeatures and LabelsExploring by ClassPlotting the ClassesRecap: Wine DatasetPractice on your own: Online Python compiler