Features, samples and labels
A sample is one example of the thing you want to predict. A feature is an input describing that sample. A label (or target) is the outcome you want a supervised model to learn to predict. For a shop, one sample could be one product on one day, the opening stock could be a feature, and the number of units sold by closing time could be the label. The label is known for historical examples but not when making a new prediction before opening.
Define the prediction before collecting features
Our fictional shop wants to estimate tomorrow’s observed unit sales for a particular product, before opening. Use one sample per product-day. The model should predict sales during that day, not the number of customers or total revenue. Observed sales are not necessarily true demand: if stock runs out, shoppers may leave without buying. To estimate unconstrained demand we would need additional assumptions or observations. A model trained on sales labels should not be presented as measuring all demand.
Data dictionary for this toy dataset:
weekend: 0 for weekday, 1 for weekend; known before opening. Categorical flag.opening_stock: nonnegative integer, units available when the shop opens; known at the decision time. Numerical count.units_sold: nonnegative integer, units sold by closing; known only afterward. Label.
Assume the opening stock is known, the product is fixed, and records represent different historical days. Real forecasting requires dates and product identifiers for traceability, even when these are not passed directly to a model.
Separate historical inputs from labels
The next example uses a list of dictionaries from the previous chapter. The function selects only the two fields allowed at prediction time. append adds one element to a list. The order of features is part of the input contract: weekend first, stock second.
def make_features(record): return [record["weekend"], record["opening_stock"]]
records = [ {"weekend": 0, "opening_stock": 12, "units_sold": 7}, {"weekend": 1, "opening_stock": 10, "units_sold": 10}, {"weekend": 0, "opening_stock": 8, "units_sold": 4},]features = []labels = []for record in records: features.append(make_features(record)) labels.append(record["units_sold"])print(features)print(labels)print(make_features({"weekend": 1, "opening_stock": 15}))The first output is three feature rows, the second is three corresponding outcomes, and the final row [1, 15] is an inference input with no label. No model has been trained here. A supervised training procedure would use the historical features and labels to learn a mapping; inference would apply that learned mapping to a new feature row. The two lists must stay aligned: shuffling one alone would attach wrong outcomes to samples.
Represent different kinds of data
Numerical fields describe quantities such as unit counts or temperatures; their units matter. Categorical fields name groups such as weekday or weekend. An identifier such as product code may consist of digits but is not necessarily a meaningful quantity. Text can be represented by word counts (Chapter 3), images by arrays of pixel values, and audio by sampled signals. Such representations retain some information and discard other information. A feature is chosen for a task; it need not equal a raw database column.
One-hot encoding represents a category using a fixed set of indicator positions. In this tiny example, the first position represents weekday and the second represents weekend. A 1 marks the active category. The categories and their order are fixed by the toy contract, not learned from future data.
def encode_day(day): if day not in ["weekday", "weekend"]: raise ValueError("unknown day category") if day == "weekend": return [0, 1] return [1, 0]
print(encode_day("weekday"))print(encode_day("weekend"))try: encode_day("holiday")except ValueError as error: print(error)The code deliberately rejects an unfamiliar category; a real system might instead reserve an unknown category, ask for correction or abstain. Never silently map an unknown value to a known one. Encoding is a representation step, not a prediction, and not every model needs this exact encoding.
Avoid information from the future
Data leakage happens when model development uses information that is unavailable at the intended prediction time, or improperly uses evaluation information. Adding units_sold to the feature row would expose the answer. Using closing stock would also be unavailable in this scenario. A historical field is not automatically an acceptable input just because a dataset contains it. Record when each field becomes available. Use only past information for future predictions; split and preprocessing design will be studied later.
Practice and recap
Imagine predicting deliveries that will arrive late before a truck leaves. Classify scheduled travel time, departure weather forecast, and actual arrival time as usable feature or unavailable outcome information. Assume the forecast is available before departure. The first two are candidate features, and actual arrival time is used to construct the late-delivery label, not as an input. Then explain why a random row ID need not be useful as a feature. Next: identify missing, invalid and biased observations before making a training dataset.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Features, samples and labels — understanding check
Apply data literacy concepts to original synthetic inventory examples.
What is the prediction unit (one sample) in the shop example?
Which field is the label for predicting observed daily sales?
What does make_features return for weekend 1 and opening_stock 15?
Why should closing stock be excluded from a prediction made before opening?
A shop sells all 10 units it has available. What does that observation establish about demand?