Exercise: Fruit Sorter
Write four functions for Three Rivers Fruit Packers, a straight-line distance and a nearest-neighbors search in plain Python, then a decision tree and a nearest neighbors classifier with the settings you choose, and let pytest check the distances, the neighbors' order and the models' settings.
Journeyman · AI Basics · Python · Updated
Apples, pears and plums roll down one belt at Three Rivers Fruit Packers, and the packers want a machine that sorts them. You'll write the four functions behind it: two in plain Python that measure how far apart two fruits are and find the nearest ones, the idea inside nearest neighbors, and two that make the chapter's two models with the settings you pick. Tests call each function and check what it gives back, and a notebook comes with the exercise for looking at the fruit, and at your tree, while you work.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-ml-fruit-sorter, 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-fruit-sorter.git
Git makes a folder, exercise-ml-fruit-sorter, with the exercise code inside. 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 prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.
The folder holds:
fruit.csv: the 450 fruits, our own made-up data, the same file the chapter's lessons use.seed_data.py: the script that made the CSV, with the pattern it planted. You don't need to run it: the committed CSV is what keeps everyone's numbers the same.fruit_sorter.py: the four functions, for you to finish, withFEATURES(the columns the models look at:weight_g,diameter_cm,sugar_brixandskin_redness) andLABEL(fruit), ready to use.explore.ipynb: a notebook for looking at the fruit and trying your code, with a cell that draws your tree. Choose the.venvkernel with Select Kernel (top right) before you run it. The tests don't read it.report.py: splits the fruit, measures a distance, finds the new fruit's neighbors and trains both models with your four functions, and prints what each step gave. Ready to use.tests/: the tests, which call each function and check what comes back, andconftest.py, which gives each test a fresh copy of the fruit. You don't need to change them.requirements.txtandpytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.
What to do
Every function in fruit_sorter.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should do, with examples:
def nearest_labels(examples, point, k):
"""Return the labels of the k examples nearest to point, nearest first, as a list.
examples is a list of (measurements, label) pairs, such as
[((0, 0), "pear"), ((5, 0), "apple"), ((1, 1), "plum")]
and point is the measurements of one new example, such as (0, 0).
Measure each example's distance to point with distance().
Examples at the same distance keep the order they have in examples.
If k is more than the number of examples, return every example's label.
With the three examples above:
nearest_labels(examples, (0, 0), 2) -> ["pear", "plum"]
nearest_labels(examples, (4, 0), 1) -> ["apple"]
"""
raise NotImplementedError("Not built yet.")
In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:
distance(a, b)returns the straight-line distance between two points of any number of features, as Distance Between Examples works it out:5.0for(0, 0)and(3, 4),2.0for(1, 1, 1, 1)and(2, 2, 2, 2). Plain Python;math.distis fine.nearest_labels(examples, point, k)measures each example's distance topointwith yourdistance, and returns the labels of theknearest, nearest first, as a list. With the three examples above and the point(0, 0): the pear is 0 away, the plum about 1.41 and the apple 5, sok = 2gives["pear", "plum"]. Equally near examples keep the order they came in; akbigger than the list gives every label.build_tree(max_depth, seed)returns a newDecisionTreeClassifier, not fitted, withmax_depthandseedpassed on asrandom_state, every other setting left at its default, as in Decision Trees.build_neighbors(k)returns a newKNeighborsClassifier, not fitted, that letskneighbors vote, as in Nearest Neighbors.
distance and nearest_labels are plain Python on purpose: they're the whole idea inside KNeighborsClassifier, and one test checks that your nearest_labels finds the same five training fruits that scikit-learn does.
Check your work
Run the tests from the exercise folder, with the virtual environment on:
python -m pytest
At first all 19 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 nearest_labels(EXAMPLES, (0, 0), 2) should return ['pear', 'plum'], but it returned ['plum', 'pear']. No test checks how many fruits a model gets right, which depends on the data: the tests check the distances, the order of the neighbors and the models' settings.
To see your functions at work on the whole file, run python report.py.
Hints
- Look before you write. Run the notebook's first cells:
fruit.head(10)andfruit.describe()show the columns and their ranges, and how much bigger the weights are than everything else. - Pythagoras in any number of directions. Square each pair's difference, add them up, take the square root, or let
math.dist(a, b)do all three. - Sort, then slice.
sorted(examples, key=...)with akeythat works out an example's distance topointputs the examples in order, nearest first. Akeyis a small function thatsortedcalls on each item and sorts by its answer;lambda example: distance(example[0], point)writes one in a line (lambda, then the item's name, a colon, and what to give back).sortedkeeps equally near examples in the order they came in.[:k]keeps the firstk, even when there are fewer. - Make, don't fit.
build_treeandbuild_neighborseach need one line: make the model with its settings and return it.report.pyand the tests callfit.
When you're done
Every test is green: pytest's last line says 19 passed. And python report.py prints:
1) fruits: 450
2) training fruits: 360
3) test fruits: 90
4) distance from the new fruit to the first training fruit: 10.06
5) the new fruit's 5 nearest training fruits: ['apple', 'apple', 'apple', 'apple', 'apple']
6) tree, max_depth 3, test fruits right: 82 of 90
7) nearest neighbors, k = 5, test fruits right: 78 of 90
8) the tree's guess for the new fruit: apple
9) the neighbors' guess for the new fruit: apple
Lines 6 and 7 count right answers on 90 test fruits; Accuracy, in the next chapter, turns that count into a score. Then explore in the notebook: draw your tree, then try build_tree(1, 0) and build_neighbors(1). Which fruits does each get wrong? And print the new fruit's five nearest training fruits with their weights: what do you notice?
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.
On GitHub, click +, then New repository. Name it (for example
exercise-ml-fruit-sorter), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-ml-fruit-sorter.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-ml-fruit-sorter, 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
A distance, a sorted list and two models built with settings you chose: that's the inside of nearest neighbors and the outside of a decision tree, each small enough to test.