Exercise: Bakery Data Check

Clone the course's first exercise, make its virtual environment, install the pinned toolkit, read how Sunrise Bakery's data was made, and finish three functions that report the installed versions, load the bakery's 120 days and count the table's rows and columns, then let pytest check them and hand the work in with Git.

Apprentice · AI Basics · Python · Updated

By

Before a model can learn anything, two things have to be right: the toolkit, installed at the versions the course uses, and the data, loaded the way you expect. Sunrise Bakery's owner has 120 days of bread sales, the data the next chapters guess tomorrow's loaves from. You'll write three small functions that check both: one that reports the version of each package you installed, one that loads the bakery's days into a table, and one that counts the table's rows and columns. Tests call each function and check what it gives back. This is the course's first exercise, so this lesson walks through every step, from getting the code to handing it in.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-ml-bakery-data-check, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-ml-bakery-data-check.git

cd moves the terminal into your course folder, and git clone copies the repository into a new folder there, exercise-ml-bakery-data-check. Get the Exercise Code with Git walks through cloning step by step. If you can't clone it, click Code on the repository's page, then Download ZIP, and unzip it into your python-course folder instead.

Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The first command makes the virtual environment, in a folder named .venv. The second turns it on, so the prompt starts with (.venv). The third installs the six pinned packages into it, which takes a few minutes. Virtual Environments and pip explains all three, and Installing the Machine Learning Toolkit shows what each package is for. If PowerShell says running scripts is disabled, the exercise's README has the one-line fix. Then check the install, as that lesson does:

python -c "import sklearn; sklearn.show_versions()"

The folder holds:

  • bakery_days.csv: the 120 days, our own made-up data: day, weekday, high_temp_f, is_holiday and loaves_sold.
  • seed_data.py: the script that made the CSV. Open it and read its opening comment: it says exactly how the loaves were made up (90 on a weekday, 135 on a Saturday, 120 on a Sunday, 25 more on a holiday, fewer on very hot days, plus a random wobble). A real dataset's pattern is what you're trying to find; this one is planted, so later lessons can show whether a guess found it. You don't need to run it: the committed CSV is what keeps everyone's numbers the same.
  • check.py: the three functions, for you to finish.
  • explore.ipynb: a notebook for looking at the days and trying your code. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: runs your three functions and prints what each one gave. Ready to use.
  • tests/: the tests, which call each function and check what comes back, and conftest.py, which gives a test a fresh copy of the days. You don't need to change them.
  • requirements.txt and pytest.ini: the packages at exact versions, and where pytest finds the tests and your code.

What to do

Every function in check.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The imports at the top are everything the three functions need. The docstring under each one says what it should do, with examples:

def count_days(days):
    """Return how many days (rows) and columns the table holds, as a dictionary.

    days is a table like the one load_days gives you. The keys are "days" and
    "columns", and each value is a whole number (int).

    With the 120 days in bakery_days.csv:
    count_days(days) -> {"days": 120, "columns": 5}
    """
    raise NotImplementedError("Not built yet.")

In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:

  1. toolkit_versions() returns a dictionary with five keys, the names in requirements.txt: "scikit-learn", "pandas", "numpy", "matplotlib" and "ipykernel". Each value is that package's __version__ as a string: toolkit_versions()["scikit-learn"] is "1.9.1" in this exercise's virtual environment.
  2. load_days(path) reads the CSV file at path into a pandas table and returns it. load_days("bakery_days.csv") gives a table of 120 rows and 5 columns, whose first row is day 1, a Monday that sold 74 loaves. Read the file at the path you're given: a test calls it with a small file of its own, 3 rows and 2 columns.
  3. count_days(days) returns {"days": <the number of rows>, "columns": <the number of columns>} for the table it's given: {"days": 120, "columns": 5} for the bakery's days, and {"days": 3, "columns": 2} for a table of 3 rows and 2 columns.

Check your work

Run the tests from the exercise folder, with the virtual environment on:

python -m pytest

At first all 10 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example count_days(the 120 bakery days) should return {'days': 120, 'columns': 5}, but it returned {'days': 5, 'columns': 120}.

Four tests compare toolkit_versions() with the versions requirements.txt pins, so they pass only in this folder's virtual environment. If one fails after you've written the function, check that the prompt starts with (.venv) and run pip install -r requirements.txt again.

To see your functions at work, run python report.py.

Hints

  • Look before you write. Run the notebook's first cells: days.shape and days.head(10) show what load_days and count_days should find.
  • One import name differs. Each package's version is <package>.__version__, and scikit-learn's is sklearn.__version__, as Installing the Machine Learning Toolkit shows.
  • Use the path you're given. pd.read_csv(path) reads whichever file path names.
  • A shape is two numbers. A table's .shape is (rows, columns), in that order: the first number is the days, the second the columns.
  • A dictionary of answers. Both toolkit_versions and count_days return a dictionary, written {"key": value, ...}, with exactly the keys the docstring names.

When you're done

Every test is green: pytest's last line says 10 passed. And python report.py prints:

1) scikit-learn: 1.9.1
2) pandas: 3.1.1
3) numpy: 2.5.5
4) matplotlib: 3.11.2
5) ipykernel: 7.4.0
6) days: 120
7) columns: 5
8) column names: ['day', 'weekday', 'high_temp_f', 'is_holiday', 'loaves_sold']
9) loaves sold on the first three days: [74, 96, 93]

Now compare line 9 with seed_data.py: days 1 to 3 were a Monday, a Tuesday and a Wednesday, so each started at 90 loaves before the weather and the wobble. How far was each day from 90? In the notebook, find day 6, the first Saturday: how close did it come to the 135 the script planted?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-ml-bakery-data-check), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. If you cloned the starter, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-ml-bakery-data-check.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    

    If you downloaded the ZIP instead:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-ml-bakery-data-check.git
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-ml-bakery-data-check, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

The right versions and the right data, checked by tests before anything learns: every model in this course starts from a toolkit and a table you've confirmed, and you've now done both.

Key terms

Related