Part I · Getting started
How to use this guide
By the end of this guide you'll be able to write the Python that machine learning actually uses: load a dataset, clean it, explore it with plots, and hand it to a model. It assumes you have never programmed before. If you have, skim Chapters 1 to 6 and slow down at Chapter 7.
This is the companion to Machine Learning Foundations. That guide teaches the ideas behind ML. This one teaches the language you'll write them in. Finish this one first, or at least through Chapter 9, and the code in the ML guide will read like plain English.
What you'll be able to do
- Store and change data with variables, lists, dictionaries, tuples, and sets
- Control what your program does with
ifstatements and loops - Write your own functions and classes, and read other people's
- Work with arrays of numbers in NumPy and tables of data in pandas
- Make clear plots with matplotlib and train a first model with scikit-learn
How each chapter works
Every chapter starts with the one idea you need to walk away with. Then it builds that idea up with short code examples. Each example shows its output in a comment, so you can check your result line by line. Each chapter ends with a Check yourself list. If you can answer every question without looking back, move on.
Running the code
Use Google Colab. It's free, runs in your browser, and already has NumPy, pandas, matplotlib, and scikit-learn installed. If you'd rather work on your own computer, install Python with Anaconda, which bundles the same libraries and Jupyter Notebook.
Type the code yourself instead of copying it. Typing makes you notice every bracket and comma, and fixing your own typos is how you learn to read error messages. Change the numbers and run it again to see what happens.
A suggested pace
| Week | Chapters | Goal |
|---|---|---|
| 1 | 1 to 3 | Run code, use variables and strings, and work with lists |
| 2 | 4 to 5 | Pick the right container for your data and control your program's flow |
| 3 | 6 to 7 | Write reusable functions and understand classes |
| 4 | 8 | Do math on whole arrays at once with NumPy |
| 5 | 9 to 10 | Load, clean, and plot a real table of data |
| 6 | 11 to 12 | Finish a small project and fix the errors you'll hit most |
Go at whatever speed works for you. Six weeks of a few hours each is plenty. Some students finish in two.
Part I · Getting started · Chapter 1
Getting set up
Python is the language almost all machine learning is written in, and you can run it in your browser in under a minute. It's popular for ML because it reads close to English, and because thousands of free libraries already do the hard math for you.
Your first notebook
Open Google Colab and choose New notebook. A notebook is a page made of cells. A code cell holds Python. A text cell holds notes. Click into a code cell, type the line below, and press Shift + Enter to run it.
print("Hello, Vector!")
# Hello, Vector!
print() shows a value on the screen. In a notebook, the last line of a cell is also shown automatically, so 2 + 3 on its own prints 5. Cells remember what earlier cells did, as long as you ran them. If something seems wrong, use Runtime > Restart and run all to run every cell fresh from the top.
Comments
A # starts a comment. Python ignores everything after it on that line. Use comments to explain why your code does something, not to repeat what it does.
# Convert grams to kilograms so the units match the other column
mass_kg = 4250 / 1000
For a longer note, people often put text inside triple quotes ("""..."""). Python reads it as a string and throws it away. You'll see this inside functions as a docstring in Chapter 6.
Indentation is part of the code
Most languages use curly braces to group lines. Python uses indentation. Lines that belong inside an if, a loop, or a function are pushed in by 4 spaces, and the line above them ends in a colon.
score = 92
if score >= 90:
print("A") # inside the if
print("Nice work") # also inside
print("Done") # outside: always runs
If the spacing is off, Python stops with an IndentationError. Colab indents for you after a colon. Don't mix tabs and spaces.
The Zen of Python
Python has a short list of design ideals built in. Run import this to read all 19. Three of them matter most for a beginner:
- Readability counts. Code is read far more often than it's written, including by you next month.
- Simple is better than complex. If a plain loop works, you don't need a clever one-liner.
- Errors should never pass silently. When something breaks, Python tells you. Read the message.
Reading an error message
An error is called an exception, and the report Python prints is a traceback. Read it from the bottom up. The last line names the type of error and what went wrong. The lines above it point to the exact line of your code.
print("Total:" + 5)
# TypeError: can only concatenate str (not "int") to str
This says you tried to glue a string and a number together with +. Chapter 2 shows the fix. Chapter 12 lists the errors you'll see most and what each one means.
Unlock the rest of this guide
You've read the first chapter. The other 11 chapters are free for every Vector Research applicant. Enter your access code to open them, and every other guide on this site.
What you'll unlock
- §02Variables, numbers, and strings
- §03Lists
- §04Dictionaries, tuples, and sets
- §05Making decisions and repeating work
- §06Functions
- §07Object-oriented programming
- §08NumPy
- §09pandas
- §10Matplotlib
- §11Putting it together
- §12Errors, practice, and next steps