Python Introduction to Machine Learning
Table of Contents + −
In the last lesson, Python in AI, you saw that machine learning is the engine behind most modern AI. So you know it matters. But what is it, actually? How does a computer “learn” anything? That sounds like magic. It is not. This lesson breaks machine learning down into a simple idea you can hold in your head, shows how it is different from normal programming, and lays out the steps every project follows. After this, the hands-on lessons will click into place.
🤔 Normal programming vs machine learning
To get machine learning, first look at how normal programming works. In normal code, you write the rules yourself. The computer just follows them.
Say you want to flag emails as spam. In normal programming you write rules by hand:
# Normal programming: YOU write the rulesdef is_spam(email): if "free money" in email: return True if "you won a prize" in email: return True return FalseThis works for the cases you thought of. But spammers change their words every week, and you cannot write a rule for every trick. You would be updating this forever.
Machine learning flips it around. Instead of you writing the rules, you give the computer thousands of emails already marked “spam” or “not spam”, and it figures out the rules itself by spotting patterns.
# Machine learning: the computer LEARNS the rules from examples# You give it: 10,000 emails, each labelled spam or not spam# It finds the patterns on its ownmodel.fit(emails, labels)So the big shift is this: in normal programming you write rules to get answers. In machine learning you give answers (examples) and the computer writes the rules. That is the whole idea in one line.
🧠 What is machine learning?
Machine learning is teaching a computer to find patterns in data and make predictions, without being told the exact rules. You show it many examples, and it learns the connection between the input and the answer.
Here is an everyday way to picture it. Think about how a child learns what a cat is. You do not give the child a list of rules (“four legs, fur, whiskers, pointy ears”). You just point at cats and say “cat” many times. After enough examples, the child can spot a cat they have never seen before. Machine learning works the same way, with data instead of pointing.
Two words you will hear constantly:
- A feature is an input fact the model learns from, like the size of a house or the words in an email. Features are what the model looks at.
- A label is the answer you want it to predict, like the price of the house or “spam / not spam”. The label is what the model tries to get right.
So the model studies the features to predict the label. Size of house (feature) predicts price (label). Words in email (feature) predict spam or not (label).
🗂️ The main types of machine learning
Machine learning comes in a few flavors, depending on what kind of data you have and what you want. The two you will meet first are the most common.
- Supervised learning is when your data already has the answers (labels), and the model learns to predict them. Spam detection is supervised, because each training email is already marked spam or not. This is by far the most common type, and what we use in the next lessons.
- Unsupervised learning is when your data has no labels, and the model finds groupings on its own. For example, splitting customers into groups by their shopping habits, without anyone telling it what the groups are.
There is also reinforcement learning, where a program learns by trial and error with rewards, like an AI learning to play a game. You do not need it now, but it is good to know the name.
| Type | Does the data have answers? | Example |
|---|---|---|
| Supervised | Yes, every example is labelled | Predict house price from its size |
| Unsupervised | No labels at all | Group customers by behavior |
| Reinforcement | Learns from rewards over time | An AI learning to play chess |
🪜 The steps of a machine learning project
Every machine learning project, big or small, follows the same path. Knowing these steps means you always know where you are.
- Collect the data. Gather lots of examples, with both the features and the labels.
- Clean the data. Fix blanks, duplicates, and wrong types, exactly the cleaning you learned with Pandas.
- Split the data. Keep most of it for teaching the model (the training set) and hold some back to test it on data it has never seen (the test set).
- Train the model. Show it the training data so it learns the patterns. This step is called fitting.
- Test the model. Check its predictions on the held-back test data, to see how well it really does.
- Use the model. Once you trust it, give it brand new data and let it predict.
The reason we hold back a test set is important. If you only check the model on data it already studied, of course it does well, it has seen the answers. The real question is whether it works on data it has never seen. That is what the test set checks.
Note
You already know the early steps. Collecting and cleaning data is the Pandas work from the last module. Machine learning adds the training and testing steps on top. So you are not starting from zero.
💡 A tiny example to picture it
Let’s make it concrete with the simplest case. Say you have data about houses: their size and their price. You want to predict the price of a new house from its size.
This is what the data might look like as features and labels:
# Feature: size in square meters Label: pricesizes = [50, 70, 90, 110, 130] # the inputs (features)prices = [150, 200, 260, 310, 360] # the answers (labels)The pattern is easy to see here: bigger houses cost more, roughly 3 thousand per square meter. A machine learning model would study these pairs and learn that relationship on its own. Then for a new house of, say, 100 square meters, it would predict a price near 290.
You will build exactly this kind of model in the coming lessons with scikit-learn. For now, the point is the shape of it: features in, the model learns, a prediction comes out.
⚠️ Common Misunderstandings
A few myths to clear up:
- “The model memorizes the data.” A good model learns the general pattern, not the exact examples. If it just memorizes, it fails on new data. That problem even has a name, overfitting, which you will meet later.
- “More data is always useless after a point.” Usually the opposite. More good examples almost always help the model learn better patterns.
- “Machine learning gives perfect answers.” It gives predictions, not certainties. A model can be wrong, which is exactly why we test it before trusting it.
✅ Best Practices
Good habits from the very start:
- Always split your data into training and test sets. Never judge a model only on data it has already seen.
- Clean your data before training. Garbage in still means garbage out, even with the smartest model.
- Start with a simple model you understand. You can always try fancier ones later.
- Be clear about your features and your label before you write any code. Knowing what predicts what keeps you on track.
🧩 What You’ve Learned
✅ In normal programming you write the rules; in machine learning the computer learns the rules from examples.
✅ A feature is an input the model learns from, and a label is the answer it tries to predict.
✅ Supervised learning uses labelled data, unsupervised learning finds groups in unlabelled data, and reinforcement learning learns from rewards.
✅ Every project follows the same steps: collect, clean, split, train (fit), test, then use.
✅ We hold back a test set so we can check the model on data it has never seen, which is the only fair test.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the core difference between normal programming and machine learning?
Why: In normal programming you hand-write the rules. In machine learning you provide labelled examples and the computer figures out the rules itself.
- 2
In machine learning, what is a 'label'?
Why: A label is the answer, like the house price or spam/not spam. Features are the inputs; the label is what the model tries to get right.
- 3
Spam detection, where each training email is already marked spam or not, is an example of which type?
Why: Because the training data already has the answers (labels), it is supervised learning, the most common type.
- 4
Why do we hold back a separate test set?
Why: A model always does well on data it already studied. The test set checks whether it really learned the pattern by trying it on unseen data.
🚀 What’s Next?
You now understand what machine learning is and the steps every project follows. Time to stop talking and start building. Next we meet scikit-learn, the Python library that lets you train a real model in just a few lines.