Build Confidence with Python and Data
Create twelve labelled synthetic study notes and validate their IDs, text, and category counts before modelling.
What You Will Learn
- Represent labelled examples with stable IDs and validate their structure.
- Inspect class counts before choosing a model.
Understand the Idea
Classification means assigning an input to one of a defined set of categories. Our input is a short fictional study-note title; the categories are web and security. A label is the category assigned to an example. The label is training data, not an unquestionable truth: a note about HTTP can reasonably relate to both fields. This exercise deliberately uses a single-label policy and assigns HTTP notes to security. Write that policy down so later disagreements are interpreted consistently.
A Python dictionary stores named fields such as id, text, and label. A list stores the collection of records. The code below creates twelve records from a fixed list, then checks that every ID is unique, every text is nonempty, and every label belongs to the allowed set. These checks catch simple mistakes before they become mysterious model behaviour. Raising an error is better than silently dropping a record, because dropping records changes the dataset you think you are evaluating.
The dataset is tiny, balanced, and intentionally easy. Balanced means the two categories have the same number of examples. Real study notes would include mixed topics, misspellings, different writing styles, and subjects outside the allowed categories. Keep a limitation note from the beginning rather than waiting until the model produces an impressive score. The immediate goal is a reproducible data pipeline you can explain. Training a sophisticated model on poorly understood records would only make the mistakes harder to inspect.
WORKED EXAMPLE
Create and Inspect Twelve Notes
Save as data.py and run python data.py. The file contains the complete synthetic dataset and uses only the Python standard library.
from collections import Counter
PAIRS = [
("html form", "web"), ("css layout", "web"),
("javascript button", "web"), ("http cookie", "security"),
("linux permission", "security"), ("network log", "security"),
("html button", "web"), ("css form", "web"),
("linux log", "security"), ("network permission", "security"),
("javascript layout", "web"), ("http permission", "security")
]
NOTES = [{"id": f"n{i}", "text": text, "label": label}
for i, (text, label) in enumerate(PAIRS, 1)]
def check_notes(rows):
seen = set()
for row in rows:
if not row["text"].strip() or row["label"] not in {"web", "security"}:
raise ValueError("Invalid note")
if row["id"] in seen:
raise ValueError("Duplicate note ID")
seen.add(row["id"])
return rows
if __name__ == "__main__":
rows = check_notes(NOTES)
print(len(rows))
print(dict(Counter(row["label"] for row in rows)))- enumerate starts IDs at n1 and preserves the written example order.
- Counter counts the labels independently of any classifier.
- The main block prints only when the file is run directly; later files can import NOTES without producing extra output.
Expected Result
The first line is 12. The second is {'web': 6, 'security': 6}.
PROJECT STAGE 01 / 8
Establish the Data Contract
Builds on: Start with the project folder and the written single-label policy.
- Save data.py exactly as shown and run it to verify the twelve-record summary.
- Create README.md describing the web/security labels and the policy for HTTP notes.
- Record that every example is synthetic and that the same keywords make this an unusually easy dataset.
- On a disposable copy, introduce a duplicate ID and confirm check_notes raises an error.
- Restore the dataset and list three kinds of real note that these examples do not represent.
Keep This Deliverable
A validated data.py and a README containing the labelling policy and dataset limitations.
Check Your Build
- The summary reports twelve notes with six in each category.
- A duplicate ID is rejected, and the original dataset is restored before continuing.
Common Mistakes and Fixes
- Treating the provided label as a universal definition of a topic.
- State the project’s labelling policy and record ambiguous cases for human review.
- Training before checking the input records.
- Inspect counts, missing values, duplicates, and scope first.
Check Your Understanding
Why might an HTTP note receive a different label in another project?
Reveal the explanation
Categories depend on the task and labelling policy. HTTP appears in both web development and security; this exercise makes a specific simplifying choice rather than defining a universal taxonomy.
Ready to Move On?
The dataset contains twelve unique notes, six per category, and the README states the HTTP labelling policy and synthetic-data limits.
Open the learning resource
