Features, targets and labels for a machine learning dataset
Write a data definition that names the unit of observation, target, features, provenance and what is known at prediction time.
By Invicly Tech, , in AI and Machine Learning with Python. About 10 minutes to read.
In short
A dataset only makes sense once you have written down what one row means, what you are predicting, and where each column came from. This data definition names the unit of observation, the target and how it was labelled, the features, their provenance, and which facts are truly known at the moment a prediction is made. Getting it right early prevents models that look brilliant in a notebook and fail in real use.
Think of it like this: A data definition is the recipe card for your dataset: without it, nobody knows what went in or whether it can be cooked again.
Explained simply
A dataset is a table. Before you train anything, decide what one row means. Is it one customer, one order, or one customer in one month? This choice is called the unit of observation, and it shapes every later step, from how you split the data to what the model can predict.
The target is the column you want to predict, such as "did this customer cancel next month". The known values of the target for past rows are labels. Labels come from somewhere: a billing system, a human reviewer or a sensor. Each source makes mistakes, so find out how good your labels are.
Features are the input columns the model uses, like tenure or number of support tickets. For each feature, write where it came from, when it was extracted, and who owns it. This record is called provenance, and it lets someone rebuild or audit the dataset later.
The most important question is: would this value really be known at the moment the model makes its prediction? A refund flag set after a customer cancels is a perfect predictor in history and useless in real life. Using it is called leakage, and it is the most common reason models disappoint.
Press Play on a diagram to walk through it one step at a time.
- Unit of observation
- One row = one customer-month
- Unique key: customer_id + month
- Target
- churned_next_month (yes/no)
- Label source: cancellation records
- Features
- Tenure, plan, support tickets
- Each with a type and unit
- Provenance
- Source system and extract date
- Owner and licence
- Prediction time
- When the model runs
- Only data available before then
- Tenure at start of month
- Tickets in last 30 days
- Current plan type
- Days since last login
- Refund issued flag
- Account closed date
- Exit survey score
- Total spend including next month
In depth
Choosing the unit of observation
The unit of observation should match the decision. If the retention team decides once a month which customers to contact, one row per customer per month is natural. If a fraud system scores each payment, one row per payment is right. Mixing units causes subtle bugs: a customer with 40 orders counts 40 times in an order-level table, so heavy buyers dominate the model.
Give every row a unique key, such as customer_id plus month, and check it with a duplicate test. When the same entity appears in many rows, note it now; later you will need grouped splits so the same customer does not appear in both training and test data.
- Match the row to the decision being made
- Define a unique key and test for duplicates
- Note repeated entities for grouped splits later
Label sources and label quality
Labels are rarely perfect. Human reviewers disagree, systems record events late, and definitions drift. To judge label quality, take a random sample of 100 to 200 rows and check them by hand or with a second reviewer. The share of agreement tells you the best accuracy you can hope for: if reviewers agree only 85 percent of the time, a model claiming 99 percent is probably copying a quirk.
Write the exact label rule. "Churned" might mean cancelled, did not renew, or no login for 60 days. Each gives a different target. Also record the label delay: a cancellation might only appear 30 days after it happens, which affects how recent your training data can be.
What is known at prediction time
Draw a timeline. Mark the moment the prediction is made, then place every feature on the timeline by when its value is finalised. Anything to the right of the prediction point must go. Common offenders are status fields updated later, totals that include the outcome period, and IDs assigned in a later process.
Also check availability in production, not just timing. A feature computed by a nightly batch job is not ready for a prediction made at 09:00 if the job finishes at 11:00. Write each feature's source, freshness and timing into the data definition so engineers can build the same inputs later.
- Step 1Past 90 days
Logins, tickets, payments: allowed.
- Step 2Prediction point
1st of the month, 06:00 UTC.
- Step 3Outcome window
The following month: cancellations happen here.
- Step 4After outcome
Refunds, exit surveys: never use as features.
Recording provenance and licences
Provenance answers: where did this data come from, how was it changed, and may we use it this way? For each source, record the system name, query or file, extract date, owner, row count and any filters. For external data, record the licence and its terms; some public datasets forbid commercial use or require attribution.
Personal data needs extra care. Note which columns identify people, the legal basis for using them, and how long you may keep them. A short provenance table kept next to the notebook, with a hash of each data file, lets you prove later which data trained which model.
- Source system, query and extract date
- Owner and contact
- Licence or legal basis for use
- Row count and file hash
Learn it in 5 short steps
About 30m each. Track them with a timer in your plan.
- 1Define the unit of observation~30m
- 2Choose the target and its label source~30m
- 3Record data provenance~30m
- 4Judge label quality~30m
- 5List what is known at prediction time~30m
Key points
- Decide what one row represents before anything else; it controls every later step.
- The target must come from a trustworthy label source, and you should know how noisy it is.
- Record where each column came from, when it was extracted and who owns it.
- Only use features whose values exist at the moment you will make the prediction.
Key terms
Tap a card to flip it. 0/6 known
Examples
data_definition = {
"unit": "one customer per calendar month",
"key": ["customer_id", "month"],
"target": "churned_next_month",
"label_source": "billing system cancellations, checked monthly",
"prediction_time": "1st day of each month, 06:00 UTC",
"features": {
"tenure_months": "int, from CRM, known at prediction time",
"tickets_last_30d": "int, from helpdesk, known at prediction time",
"refund_issued": "bool, EXCLUDED: set after cancellation",
},
"provenance": "warehouse extract 2026-11-01, owner: data team",
}A data definition written as a plain Python dictionary and kept next to the notebook.
Writing a data definition for late delivery prediction
A courier wants to predict whether a parcel will arrive late. Step 1: unit of observation. One row per parcel at the moment it is scanned into the depot. Step 2: target. late = arrived after the promised date, from the tracking system, label known within 5 days. Step 3: features. Distance, depot, weekday, parcel weight, current depot backlog. Step 4: timing check. "Driver assigned" is set after the depot scan, so it is excluded. Step 5: provenance. Tracking warehouse extract, 2026-10-01, owner: operations data team. The result fits on one page and settles arguments before they start.
Checking the unit of observation in pandas
You receive a churn table and must confirm one row per customer per month. Step 1: count duplicates on the key. Step 2: inspect any duplicates found. Step 3: check that the label rate is sensible. If duplicates appear, find out whether they are true repeats or two different events, and fix the extract rather than silently dropping rows.
import pandas as pd
df = pd.read_csv("churn_monthly.csv")
key = ["customer_id", "month"]
dupes = df[df.duplicated(subset=key, keep=False)]
print("rows:", len(df), "duplicate key rows:", len(dupes))
print(dupes.sort_values(key).head())
print("churn rate:", df["churned_next_month"].mean().round(3))Quick reference
- Decide what one row means before anything else
- Every row needs a unique key; test it with df.duplicated(subset=key)
- Write the exact label rule and how long labels take to arrive
- Hand-check 100 to 200 labels to estimate label noise
- Drop any feature finalised after the prediction point
- Record source, extract date, owner, licence and file hash for each input
Common mistakes
- Using a column filled in after the outcome, such as a refund flag, which leaks the answer.
- Mixing units of observation, such as one row per customer and one per order in the same table.
- Trusting labels without checking how they were made or how often they are wrong.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
For a model that predicts whether a student passes a course, name the unit of observation, the target and two features known at enrolment.
- Exercise 2medium
Write a pandas check that fails loudly if a table has more than one row per order_id, and print the label rate.
- Exercise 3hard
A colleague's model predicts hospital readmission with 99 percent accuracy. List the features you would inspect for leakage and the test you would run.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between a label and a target?
- The target is the column you predict. Labels are the known values of that column for past examples, which the model learns from.
- What is data leakage in simple terms?
- It is when the model sees information during training that it would not have when making real predictions, such as a field filled in after the outcome. Scores look great in testing and collapse in use.
- How many labelled rows do I need?
- There is no fixed number. A few hundred labelled rows can show whether a pattern exists for simple tabular problems; more data usually helps, and a learning curve tells you whether adding rows would improve the score.