Lessons · Coding · beginner · Programming with Mosh

Python Machine Learning Tutorial (Data Science)

The video “Python Machine Learning Tutorial (Data Science)” by Programming with Mosh as 10 lessons, with the idea explained, the code from the screen, steps to try in the practice terminal, practice problems with solutions and a quiz that leads back to the moment.

10lessons
16pieces of code
24exercises
31questions
50min of video
Open the lessons →Lesson 1 is free · Beyond Pass opens all
Python Machine Learning Tutorial (Data Science), video by Programming with Mosh▶ 49:42 · Programming with Mosh
12345678910
0:0010 lessons across the whole video49:42

You will be able to

  • Build a machine learning model with scikit-learn that predicts a category from tabular data
  • Load, inspect, and split a CSV dataset into input and output sets using pandas
  • Train a decision tree classifier and use it to make predictions
  • Measure model accuracy with a train/test split and accuracy_score
  • Save a trained model to disk with joblib and load it later for predictions
  • Export a decision tree as a graph and read its rules to explain predictions

Good to know first

  • Know Python fairly well (imports, functions, lists, basic syntax)
  • Be able to install software and run commands in a terminal
  • Have a basic understanding of what a CSV file is

Syllabus

10 lessons and a final project

  1. 1

    What machine learning is and why it helps Free

    0:00–2:523 min of video

    This lesson explains what machine learning is, and why it is useful for problems that are too complex to solve with hand-written rules. You will see the difference between traditional programming and machine learning using the cat-or-dog example.

    2 exercises4 questions
  2. 2

    The machine learning project workflow Beyond Pass

    2:52–5:453 min of video

    A machine learning project follows a repeatable sequence of seven steps, from importing data to evaluating and improving the model. Knowing this order tells you what to do next at every stage of a project.

    2 exercises3 questions
  3. 3

    Installing Anaconda and running your first Jupyter notebook Beyond Pass

    5:45–10:405 min of video

    You learn what the main machine learning libraries do and why data scientists use Jupyter notebooks instead of plain code editors. You install Anaconda, start Jupyter from the terminal, and run your first Python notebook cell.

    1 code6 build steps2 exercises3 questions
  4. 4

    Loading a CSV with pandas and inspecting a DataFrame Beyond Pass

    10:40–17:066 min of video

    You will load a real CSV file (video game sales) into a pandas DataFrame with read_csv, then inspect it with df, shape, describe() and values. You will learn what each of these tells you about a dataset before you start machine learning.

    4 code6 build steps3 exercises3 questions
  5. 5

    Essential Jupyter shortcuts and cell behavior Beyond Pass

    17:06–23:006 min of video

    You learn the two modes of a Jupyter cell (edit and command), the keys that add, delete and comment cells, and how to use Tab and Shift+Tab for help while typing. You also see why running one cell does not re-run the others.

    3 code6 build steps2 exercises4 questions
  6. 6

    Preparing the music dataset into input and output sets Beyond Pass

    23:00–29:106 min of video

    You load the music CSV with pandas and check it for problems that need cleaning. Then you split the table into an input set X (age and gender) and an output set y (genre), the two pieces every model needs for training.

    3 code5 build steps3 exercises3 questions
  7. 7

    Training a Decision Tree and Making Predictions Beyond Pass

    29:10–33:204 min of video

    You will learn how to create a decision tree classifier with scikit-learn, train it on input and output data with fit(), and ask it to predict new outcomes with predict(). You will also see why we check the model's predictions against our own expectations before trusting it.

    1 code4 build steps2 exercises3 questions
  8. 8

    Measuring accuracy with a train/test split Beyond Pass

    33:20–39:406 min of video

    You will learn why a model must be tested on data it never saw during training, and how to use train_test_split and accuracy_score from scikit-learn to measure accuracy. You will also see why accuracy changes from run to run and how the amount of data affects it.

    1 code5 build steps3 exercises2 questions
  9. 9

    Saving and loading a trained model with joblib Beyond Pass

    39:40–42:553 min of video

    You will learn why training a model should not be repeated every time you need predictions, and how to save a trained model to a file with joblib.dump and load it back with joblib.load. You will also see how a loaded model makes predictions without any retraining.

    2 code5 build steps3 exercises3 questions
  10. 10

    Visualizing the decision tree and reading its rules Beyond Pass

    42:55–49:427 min of video

    You will export a trained decision tree to a Graphviz .dot file and render it as a picture in VS Code. You will then learn to read each node's condition, gini, samples, value and class, and follow a prediction from the root down to a leaf.

    1 code3 build steps2 exercises3 questions

Final project

Build, test, save and explain a music recommender

Build an end-to-end music recommender with scikit-learn that predicts a favourite genre from age and gender. You will run the whole workflow from the course: load and prepare data, train, measure, save, reload and explain the model. The aim is to get a model you can measure and explain, not just one that runs.

Its 7 tasks open with Beyond Pass.

Free preview · Lesson 1 in full

What machine learning is and why it helps

This lesson explains what machine learning is, and why it is useful for problems that are too complex to solve with hand-written rules. You will see the difference between traditional programming and machine learning using the cat-or-dog example.

0:00 to 2:52 in the video

Scratchpad

Draw and write next to the video.

The idea

Machine learning is a subset of artificial intelligence (AI). Instead of writing every rule yourself, you build a model, give it lots of data, and let it find patterns in that data by itself.

Imagine you want a program that looks at a picture and says whether it shows a cat or a dog. With traditional programming, you would have to write rules for curves, edges and colours, and decide which combinations mean 'cat' or 'dog'. This gets very complex. A black and white photo, or a photo taken from an unusual angle, can break your rules, so you keep rewriting them. If you later add horses, you have to rewrite the rules again.

Machine learning handles this differently. You give the model thousands or tens of thousands of labelled pictures of cats and dogs. The model finds and learns the patterns in the input data. Then you show it a new picture it has never seen and ask: is it a cat, a dog or a horse? It answers with a certain level of accuracy.

The more input data you give the model, the more accurate it tends to become. Adding a new animal means adding new examples to learn from, not rewriting the rules.

Machine learning is used in many areas: self-driving cars, robotics, language processing, vision processing, forecasting (such as stock market trends and the weather), and games.

Key points

  • Traditional programming means you write the rules; machine learning means the model learns the patterns from data. 1:26
  • Hand-written rules for recognising images break easily with new lighting, colours or angles. 1:00
  • A machine learning model is built, then fed lots of data so it can find patterns in it. 1:51
  • Given a new input it has never seen, the model predicts an answer with a certain level of accuracy. 2:15
  • More input data usually means a more accurate model. 2:24
  • Extending to new categories (like horses) means adding data, not rewriting rules. 1:58

Words to know

Artificial intelligence (AI)
The broad field of making computers do tasks that seem to need human intelligence.
Machine learning
A subset of AI where a model learns patterns from data instead of following rules written by hand.
Model
The engine that learns patterns from input data and then makes predictions on new data.
Pattern
A regularity in the data, such as the shapes and colours that often appear in pictures of cats.
Accuracy
How often the model's predictions are correct, given as a level such as a percentage.

Watch out

  • Machine learning is not magic: the model's answers come with a level of accuracy, not certainty. 2:15
  • Machine learning is a subset of AI, not the same thing as all of AI. 1:03
  • Machine learning does not remove the need for data; the more (good) data, the better the model. 2:24

Practice

Explain Solution checked

In your own words, explain the difference between traditional programming and machine learning using the cat-or-dog example.

Solution
  1. Traditional programming: you write rules (curves, edges, colours) that decide cat or dog.
  2. Machine learning: you give the model many labelled pictures and it learns the patterns itself.
  3. When new cases appear (black and white photos, new angles, a third animal), the traditional rules break and must be rewritten, while the model improves by learning from more data.

Traditional programming relies on hand-written rules that must be rewritten as cases change; machine learning builds a model that learns patterns from lots of data and can handle new cases, including new categories, by learning from more examples.

Do it Solution checked

Choose two everyday problems (for example, sorting emails into spam or not spam, or recognising handwritten digits). For each, write one sentence on whether hand-written rules would be hard to maintain and what kind of data a machine learning model would need.

Solution
  1. Pick a problem, such as spam detection.
  2. Think: spam wording keeps changing, so fixed rules break; a model can learn from many emails labelled spam or not spam.
  3. Repeat for a second problem and write one sentence for each.

Each problem answered with one sentence on why rules are hard to maintain and what labelled data the model needs.

On your scratchpad

On your scratchpad, draw two boxes side by side. Label the left one 'Traditional programming' with arrows Rules + Data → Answer. Label the right one 'Machine learning' with arrows Data + Answers → Model (learned patterns), then New data → P

Quiz

  1. In the video, why do hand-written rules for recognising cats and dogs break down?

    1. Computers cannot store pictures
    2. Black and white photos or new angles can break the rules, so they must be rewritten
    3. Rules are always faster than models
    4. Dogs and cats look exactly the same
    Show the answer

    B. Black and white photos or new angles can break the rules, so they must be rewritten · The speaker says a black and white photo or a picture from a different angle can break the rules, so they have to be rewritten.

    1:00 watch the moment
  2. What does a machine learning model do with the thousands of labelled pictures?

    1. It stores them and copies them when asked
    2. It learns patterns in the input data
    3. It asks a programmer to write rules
    4. It deletes the pictures that are too dark
    Show the answer

    B. It learns patterns in the input data · The model finds and learns patterns in the input data, which lets it predict on pictures it has not seen.

    1:26 watch the moment
  3. What happens when you want the program to also recognise horses?

    1. You must rewrite all the rules again
    2. You add horse examples to the data instead of rewriting rules
    3. The model cannot be extended
    4. You switch to traditional programming
    Show the answer

    B. You add horse examples to the data instead of rewriting rules · The speaker says that rewriting all the rules will not work; the machine learning approach extends by adding data.

    1:58 watch the moment
  4. What generally happens to a model's accuracy as you give it more input data?

    1. It becomes less accurate
    2. It stays the same no matter what
    3. It tends to become more accurate
    4. It becomes accurate only after one hour
    Show the answer

    C. It tends to become more accurate · The speaker says the more input data we give it, the more accurate the model is going to be.

    2:24 watch the moment

Open lesson 1, with the video →

Lessons 2–10 and the final project

They open with Beyond Pass: every lesson of every course, in full. One payment: $14.99 for 90 days or $29 for a year. No subscription.

Made by Beyond from the video “Python Machine Learning Tutorial (Data Science)” by Programming with Mosh. The video plays in YouTube's own player. Watch it on YouTube · All lessons