Module lessons (3/4)
Data, features and labels
The hook: a table is a dataset
Look at this tiny table of houses:
| size (m²) | rooms | age (years) | price (k€) |
|---|---|---|---|
| 50 | 2 | 30 | 150 |
| 80 | 3 | 10 | 260 |
| 120 | 4 | 5 | 410 |
- 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 table | Symbol | Known at prediction time? | Example |
|---|---|---|---|
| Feature column | X[:, j] | Yes, always | Rooms, signup age |
| Target label | y | No, we predict it | Churn flag, house price |
| Leak / ID column | Exclude! | No or non-generalising | Cancellation 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:
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 3Second, when a 1-D list has only one feature, reshape it into a 2-D matrix (n_samples, 1) so scikit-learn accepts it:
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:
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:
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
Warmup — Combine feature columns into X
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.
Show hint
np.column_stack([a, b]) puts each list in its own column.
Solution available after 3 attempts
Practice — Separate features X from target y
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).
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
Challenge — Unpack and validate dataset dimensions
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.
Show hint
X.shape is a tuple: (number of rows, number of columns).
Solution available after 3 attempts
Check your understanding
A dataset has 500 rows and 8 input columns plus one price column. What are the shapes of X and y?
Which of these would be a bad feature for predicting whether a patient will be readmitted to hospital within 30 days?