2Foundational Skills 2.1Python Basics
2.1.3 Simple Python Problems
Practise the fundamentals on small problems, including eight data problems you will meet again in machine learning.
Why practise with small problems?
Watching someone code feels easy. Writing it yourself, from a blank cell, is where you find out what you really know. Small problems are the fastest way to get there: each one takes a few minutes, tells you straight away whether you got it right, and uses only the ideas from Python Fundamentals.
This module has two parts. First, eight problems we wrote for you. They look like everyday Python, but each one is secretly a machine learning idea you will meet again later, so solving them now makes those modules much easier. Then, problem sets from HackerRank, CS50P and CodeChef with hundreds more, at the bottom of the page.
You do not need to do everything. If the first four of our problems and a couple of the Easy sets below feel comfortable, move on.
How to solve a problem
Most beginners get stuck because they start typing too early. Try this instead:
- Work one example by hand. Before any code, write down what the answer should be for a small input. If you cannot do it by hand, the code will not help.
- Write the simplest version that works. Loops and
ifare fine. Clever one-liners can come later. - Test it. Run it on your example and check the answer.
assertdoes the checking for you: it does nothing when the test passes and stops with an error when it fails. - Try an awkward input. An empty list, a list with one item, all values the same. These are where bugs hide.
Here is step 3 in practice:
def mean(values):
return sum(values) / len(values)
assert mean([2, 4, 9]) == 5.0
assert mean([7]) == 7.0
print("all tests passed")
# all tests passed
If a test fails, you can add a message saying what you expected, and Python prints it: AssertionError: mean([1, 2]) should be 1.5.
Eight problems from machine learning
Try each one before opening the answer. There is no single correct solution; if yours gives the same results, it is right. Two tools will help:
zip(a, b)walks through two lists side by side:zip(["cat", "dog"], ["cat", "cat"])gives the pairs("cat", "cat")and("dog", "cat").counts.get(key, 0)reads a value from a dictionary, or gives 0 if the key is not there yet (you saw this in Python Fundamentals).
1. Accuracy (Easy)
You will meet this again in Model Evaluation Metrics. It is the most common score in classification tasks.
2. The most common label (Easy)
Always predicting the most common label is called a majority baseline. It gets 60% here without learning anything, so any real model must beat 60% to be worth using. See Terminology.
3. Mean squared error (Easy)
This is the loss that Linear Regression minimises, and Loss Functions compares it with others.
4. Scaling to 0–1 (Medium)
Many models work better when every feature is on a similar scale. See Scaling in Scikit-learn Basics and why K-NN needs it.
5. Train and test split (Medium)
Why a test set at all? A model must be scored on data it never saw while training, or the score means nothing. See Training, validation and test sets.
6. Confusion counts (Medium)
These four counts make up the confusion matrix, and almost every classification score is built from them.
7. The nearest neighbour (Medium)
Congratulations: you just wrote a 1-nearest-neighbour classifier. K-NN does the same with several neighbours and several features.
8. The most common words (Medium)
Counting words is the first step of Bag of Words, one of the simplest ways to turn text into numbers a model can use.
More practice
The table below lists problem sets from HackerRank, CS50P and CodeChef, easiest first. Tick a set when you finish it; your ticks are saved on this device, and on every device if you log in.
- Start with the Easy sets. HackerRank's Introduction and Basic Data Types problems are short and are checked as soon as you submit. CS50P problem sets 0 and 1 and the problems in CodeChef's Learn Python course are the same level.
- Medium when Easy feels boring. CS50P problem sets 2 to 4, HackerRank's Strings and Errors and Exceptions, and CodeChef's Practice Python problems need a bit more thought.
- Stuck for more than 20 minutes? Look at a hint, or ask in the SOTA Discord. Then come back to the problem a day later and solve it again without help. That second solve is where it sticks.
What comes next
- Jupyter Notebook Interface and Basics: the notebook format every IOAI task uses.
- Pandas: the library that does most of this counting and splitting for you, on whole tables at once.
Resources
| Source | Title | Why read it |
|---|---|---|
| HackerRank | Python practice | Short problems sorted by topic, checked as soon as you submit. Start with Introduction and Basic Data Types. |
| CodeChef | Learn Python | A beginner course with practice problems after each lesson. |
| CodeChef | Practice Python | 195+ Python problems, for when you want more after the course. |
| Harvard | CS50P problem sets | Small programs with automatic checking. Sets 0 to 4 match the fundamentals. |
Practice problems
| Solved | Source | Problem | Difficulty | Tags |
|---|---|---|---|---|
| HackerRank | Python: Introduction | Easy | basics | |
| HackerRank | Python: Basic Data Types | Easy | lists, strings | |
| CS50P | Problem Set 0 | Easy | functions | |
| CS50P | Problem Set 1 | Easy | conditions | |
| CodeChef | Learn Python: practice problems | Easy | basics, loops, lists | |
| HackerRank | Python: Strings | Medium | strings | |
| CS50P | Problem Set 2 | Medium | loops, strings | |
| CS50P | Problem Set 3 | Medium | exceptions, dictionaries | |
| CS50P | Problem Set 4 | Medium | libraries | |
| HackerRank | Python: Errors and Exceptions | Medium | exceptions | |
| CodeChef | Practice Python | Medium | mixed |
Finished this module? Mark it done.
Comments
Stuck on something, spotted a mistake, or found a resource that helped you? Tell us below.
Loading comments