MACHINE LEARNING • LESSON 9

Understand the Python Code

We already built a Decision Tree. Now let's understand exactly what each important line of the Python code does and how the pieces work together.

THE SIMPLEST IDEA

X contains the inputs, y contains the answers, fit() learns, and predict() makes a prediction.

Once you understand these four ideas, the basic Decision Tree code becomes much easier to read.

01

The Complete Code

First, look at the complete example before breaking it into individual parts.

from sklearn.tree import DecisionTreeClassifier

# Training data
X = [
    [1, 55],
    [2, 60],
    [3, 70],
    [5, 85],
    [6, 90],
    [7, 95]
]

# Labels
y = [
    "Fail",
    "Fail",
    "Fail",
    "Pass",
    "Pass",
    "Pass"
]

# Create the model
model = DecisionTreeClassifier(random_state=42)

# Train the model
model.fit(X, y)

# Make a prediction
prediction = model.predict([[5, 88]])

print(prediction)
Import Data Model fit() predict()
02

Import the Decision Tree

The first line is:

from sklearn.tree import DecisionTreeClassifier

This imports the DecisionTreeClassifier class from scikit-learn.

We need this class because it provides the Decision Tree algorithm we want to use.

IN SIMPLE WORDS "Bring the Decision Tree tool into our Python program."
03

What Is X?

Next we create the input data:

X = [
    [1, 55],
    [2, 60],
    [3, 70],
    [5, 85],
    [6, 90],
    [7, 95]
]

X contains the features the model will use to make its decisions.

FEATURE 1 Study Hours
FEATURE 2 Attendance

Look at the first row:

[1, 55]

This means:

ONE STUDENT 1 hour study + 55% attendance
04

What Is y?

Next we create the labels:

y = [
    "Fail",
    "Fail",
    "Fail",
    "Pass",
    "Pass",
    "Pass"
]

y contains the correct answer for each row in X.

X — INPUT y — ANSWER
[1, 55] Fail
[2, 60] Fail
[3, 70] Fail
[5, 85] Pass
[6, 90] Pass
[7, 95] Pass
X tells the model what it can look at. y tells the model what the correct answer was.
05

Create the Model

Now we create the Decision Tree:

model = DecisionTreeClassifier(random_state=42)

This creates a Decision Tree classifier and stores it inside the variable called model.

MODEL DecisionTreeClassifier

The model is created, but it has not learned from our training data yet.

Think of this as getting an empty decision-making tool ready for training.

06

What Does random_state=42 Mean?

You may notice this:

random_state=42

Some Machine Learning algorithms can involve randomness during their operation.

Setting random_state gives that randomness a fixed starting point, which helps make results reproducible when randomness is involved.

IMPORTANT 42 is not a special Machine Learning number.

You could use another integer such as 10 or 100. The important idea is reproducibility.

07

Train the Model With fit()

This is one of the most important lines:

model.fit(X, y)

The fit() method trains the Decision Tree using our examples.

X Inputs

Study Hours + Attendance

+
y Answers

Pass / Fail

fit() Learning

Finds useful decision rules.

During training, the tree looks for useful splits that help separate the classes.

fit() means: "Learn from these examples."
08

What Does the Tree Learn?

The model can discover useful decision rules from the training data.

For our simple dataset, one useful pattern could be:

EXAMPLE DECISION RULE Is Study Hours greater than a certain value?

The exact tree structure is determined during training.

We do not manually write the decision rule in our Python code.

The algorithm finds the splits from the training data.

09

Make a Prediction With predict()

After training, we can give the model a new student:

prediction = model.predict([[5, 88]])

The new student has:

STUDY HOURS 5
ATTENDANCE 88%

The trained tree uses the decision rules it learned during training to determine the predicted class.

NEW DATA [5, 88]
TRAINED TREE Decision Rules
PREDICTION Pass
10

What Does predict() Return?

If we print the prediction:

print(prediction)

We may see:

['Pass']

Notice that the result is inside a list. That is because predict() is designed to make predictions for one or more input rows.

RESULT ['Pass']

The model predicted the class "Pass" for the new student.

11

One Line, One Job

The easiest way to remember the code is to understand the job of each important part.

IMPORT DecisionTreeClassifier

Brings the Decision Tree algorithm into Python.

X Input Features

Contains the information used by the model.

y Target Labels

Contains the correct answers.

MODEL DecisionTreeClassifier()

Creates the Decision Tree model.

TRAIN fit(X, y)

Learns decision rules from the examples.

PREDICT predict(...)

Uses the trained tree to predict new data.

12

The Whole Code in Plain English

We can translate the Python code into simple English:

1 Import the Decision Tree.
2 Give it training examples.
3 Give it the correct answers.
4 Train the model with fit().
5 Give the trained model a new example.
6 Get the prediction with predict().
13

Two Different Things: Training vs Prediction

A common beginner mistake is thinking that fit() and predict() do the same thing.

fit() Learns

Uses known training examples and their labels.

model.fit(X, y)
predict() Predicts

Uses what the model learned to classify new data.

model.predict([[5, 88]])
REMEMBER THIS

X + y → fit() → trained model → predict() → answer

That is the basic pattern behind this Decision Tree example. The model learns from known examples first, then uses what it learned to make predictions for new examples.

X + y fit() Model predict() Pass / Fail
QUICK CHECK

Check Your Understanding

What does X contain? The input features used by the model.
What does y contain? The correct target labels for the training examples.
What does fit() do? It trains the model using X and y.
What does predict() do? It uses the trained model to predict labels for new data.
What does [5, 88] represent? One new example with 5 study hours and 88% attendance.
Does the code manually create the tree rules? No. The Decision Tree learns useful splits from the training data.
LESSON 9 COMPLETE

Decision Trees

You now understand what a Decision Tree is, how it makes decisions, how nodes and branches work, how data is split, why overfitting happens, and how to build a basic Decision Tree classifier with Python.