Skip to main content
Module 1 · Lesson 3 of 43/56 in the course~18 min
Module lessons (3/4)

Data, features and labels

The hook: a table is a dataset

Look at this tiny table of houses:

size (m²)roomsage (years)price (k€)
50230150
80310260
12045410
  • Each row is a sample (also called an example or an observation): one house.
  • Each input column is a feature: a measurable property used to predict.
  • The column we want to predict is the label (or target): the price.

By convention the features form a matrix called X with one row per sample and one column per feature, and the labels form a vector y with one value per sample. Here X has shape (3, 3) and y has shape (3,).

Feature roles at a glance

Role in tableSymbolKnown at prediction time?Example
Feature columnX[:, j]Yes, alwaysRooms, signup age
Target labelyNo, we predict itChurn flag, house price
Leak / ID columnExclude!No or non-generalisingCancellation date, UUID

Try it: tag the columns

Types of features

  • Numeric: sizes, ages, counts, prices. Models use them directly.
  • Categorical: colours, countries, plan names. They must be turned into numbers first (module 6).
  • Boolean: yes or no, which is easy to encode as 0 or 1.

In code: building X and y progressively

First, pack separate feature columns and the target into X and y:

Python
import numpy as np

X = np.array([[50, 2, 30], [80, 3, 10], [120, 4, 5]])
y = np.array([150, 260, 410])

n_samples, n_features = X.shape
print(n_samples, n_features)   # 3 3

Second, when a 1-D list has only one feature, reshape it into a 2-D matrix (n_samples, 1) so scikit-learn accepts it:

Python
sizes = np.array([50, 80, 120])
X_single = sizes.reshape(-1, 1)
print(X_single.shape)  # (3, 1)

Third, use np.column_stack to combine multiple 1-D feature lists cleanly:

Python
rooms = np.array([2, 3, 4])
ages = np.array([30, 10, 5])
X_two = np.column_stack([sizes, rooms, ages])
print(X_two.shape)  # (3, 3)

Fourth, always assert X.shape[0] == y.shape[0] and confirm no target leak slipped into X:

Python
raw = np.array([[50, 2, 150], [80, 3, 260], [120, 4, 410]])
X_clean, y_clean = raw[:, :-1], raw[:, -1]
assert X_clean.shape[0] == y_clean.shape[0]
print(X_clean.shape, y_clean.shape)  # (3, 2) (3,)

Practice

Practice Stage

Warmup — Combine feature columns into X

Exercise#machine-learning.m1.l3.e1
Attempts: 0Loading…

Build the feature matrix X (a 2-D numpy array with one row per house and the columns size, rooms) from the two lists below. Store it in X.

Loading editor…
Show hint

np.column_stack([a, b]) puts each list in its own column.

Solution available after 3 attempts

Practice Stage

Practice — Separate features X from target y

Exercise#machine-learning.m1.l3.e2
Attempts: 0Loading…

The array data has one row per house: size, rooms and price in the last column. Split it into features X (every column except the last) and the label y (the last column).

Loading editor…
Show hint

Slicing: data[:, :-1] keeps all rows and all columns except the last; data[:, -1] is the last column.

Solution available after 3 attempts

Practice Stage

Challenge — Unpack and validate dataset dimensions

Exercise#machine-learning.m1.l3.e3
Attempts: 0Loading…

Count the data. From X, store the number of samples in n_samples and the number of features in n_features, both as plain ints.

Loading editor…
Show hint

X.shape is a tuple: (number of rows, number of columns).

Solution available after 3 attempts

Check your understanding

Quiz#machine-learning.m1.l3.e4

A dataset has 500 rows and 8 input columns plus one price column. What are the shapes of X and y?

Answer options
Quiz#machine-learning.m1.l3.e5

Which of these would be a bad feature for predicting whether a patient will be readmitted to hospital within 30 days?

Answer options