Your First Supervised Learning Model in Python

Machine learning can sometimes feel abstract. To make it easier, let’s start with a very simple, fun dataset: ages of people and whether they like cartoons or not.
We’ll use this to demonstrate how supervised learning works in practice.
To summarise all we know about supervised learning…
Supervised learning is like having a teacher.
You give the algorithm examples (inputs) and the correct answers (outputs).
The model learns the relationship.
Later, when you give it a new input, it can predict the answer.\
In our case:
Input (X): Age of a person.
Output (y): Do they like cartoons? (Yes = 1, No = 0).
It’s also good to remember that machines only understand numbers (ultimately 1’s and 0’s). That means your output labels cannot be words like “likes cartoons” or “doesn’t like cartoons.” Instead, we encode them as numbers:
1 usually stands for the positive case (e.g., likes cartoons).
0 usually stands for the negative case (e.g., does not like cartoons).
This process is called label encoding , converting categories (words) into numbers so the model can work with them.
From the previous blog, we learnt that for machine learning, the first major step is to gather the data. For this step we are going to create an artificial set (completely made up).
As you can see, the inputs are ages, and the outputs are just 1’s and 0’s, where 1 is for liking cartoons and 0 is for the opposite. Now I want you to take a moment to look for the pattern inside this dataset. The pattern is younger kids like cartoons, adults generally don’t. A better answer would be that if your age is below or equal to 10 then you like cartoons.
Because of the small nature of our dataset, we can easily load it into Python using lists. In supervised learning, we always have two parts:
Input (features) → what we know.
Output (labels/targets) → what we want to predict.
In our case:
The input is the person’s age.
The output is whether they like cartoons (
1 = Yes
,0 = No
).
This is how we load it into python
ages = [[5], [7], [10], [13], [15], [18], [21], [25], [30], [35]]
likes_cartoons = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
Notice two things:
ages
is written as a list of lists (e.g.,[[5], [7], ...]
). That’s because scikit-learn expects a 2D structure: rows = examples, columns = features. Even though we only have one feature (age), we still wrap it in a list.likes_cartoons
is a simple list of numbers (not nested). That’s because each example only has one label.
Training the Model
Now, let’s create a Decision Tree Classifier. This is a supervised learning algorithm that splits the data into decision rules (like “If age ≤ 10 → likes cartoons, otherwise doesn’t”).
from sklearn.tree import DecisionTreeClassifier
# Create the model
model = DecisionTreeClassifier()
# Train (fit) the model on our data
model.fit(ages, likes_cartoons)
Here, the model is essentially “studying” our data. It looks at the relationship between age and cartoon preference, then builds a rule.
Making Predictions
Once trained, we can ask the model questions it has never seen before.
print("Age 8 →", model.predict([[8]])[0]) # Expect 1 (likes cartoons)
print("Age 20 →", model.predict([[20]])[0]) # Expect 0 (does not like cartoons)
The predict
method always returns a list of answers, so we use [0]
to grab the first (and only) prediction.
Why This Works
The model found a pattern:
If age ≤ 10.5 → predict 1 (likes cartoons)
If age > 10.5 → predict 0 (does not like cartoons)
That’s supervised learning in action: we gave it labeled examples, and now it can make decisions for new, unseen data.
This is the whole code in play;
from sklearn.tree import DecisionTreeClassifier
#Dataset (inputs and outputs):ages = [[5], [7], [10], [13], [15], [18], [21], [25], [30], [35]]
#inputlikes_cartoons = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0] #output
#creating and training a modelmodel = DecisionTreeClassifier()
model.fit
(ages, likes_cartoons)
print("Age 8 →", model.predict([[8]])[0])print("Age 20 →", model.predict([[20]])[0])
Subscribe to my newsletter
Read articles from Cal Afun directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Cal Afun
Cal Afun
Hi, I’m Cal, a Computer Engineering student passionate about software development and learning by teaching. My goal is to break down concepts in a simple, beginner-friendly way.