MACHINE LEARNING • LESSON 8

Understand the Python Code

We have already built a classifier. Now let's understand what every important line of the Python code does and how all the pieces work together.

THE MAIN IDEA

Don't memorize the code. Understand what each line is doing.

A classification program follows a simple process: prepare the data, create the model, train it, give it new data, and get a prediction.

01

The Complete Code

First, look at the complete program before we break it into individual parts.

from sklearn.linear_model import LogisticRegression

# Training data
X = [
    [1],
    [2],
    [3],
    [5],
    [6],
    [7]
]

# Target labels
y = [
    0,
    0,
    0,
    1,
    1,
    1
]

# Create the model
model = LogisticRegression()

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

# New data
new_student = [[6]]

# Make a prediction
prediction = model.predict(new_student)

# Get probabilities
probability = model.predict_proba(new_student)

print("Classes:", model.classes_)
print("Prediction:", prediction)
print("Probabilities:", probability)
This entire program can be understood as: Data → Model → Training → New Data → Prediction.
02

Line 1 — Import Logistic Regression

from sklearn.linear_model import LogisticRegression

This line imports the LogisticRegression class from scikit-learn.

We need this because scikit-learn provides the Machine Learning algorithm for us.

sklearn The scikit-learn Machine Learning library.
linear_model The part of scikit-learn containing linear models.
LogisticRegression The classifier we want to use.
Importing does not train the model. It only makes the Logistic Regression class available to our Python program.
03

The X Variable — Input Features

X = [
    [1],
    [2],
    [3],
    [5],
    [6],
    [7]
]

X contains the input features that the model will use to learn.

In our example, X represents the number of hours each student studied.

X VALUE [1]

Student studied for 1 hour.

X VALUE [6]

Student studied for 6 hours.

Notice that every value is inside another pair of brackets:

[1]
[2]
[3]

This is because scikit-learn expects X to be a two-dimensional collection:

NUMBER OF ROWS 6 students × NUMBER OF FEATURES 1 feature
04

The y Variable — Target Labels

y = [
    0,
    0,
    0,
    1,
    1,
    1
]

y contains the correct answers associated with the training data.

0 Fail
1 Pass

So the training examples can be understood as:

Study Hours y Meaning
1 0 Fail
2 0 Fail
5 1 Pass
7 1 Pass
X tells the model what information it has. y tells the model what the correct answer was.
05

Create the Model

model = LogisticRegression()

This creates a Logistic Regression model object and stores it inside the variable called model.

Think of it as creating an empty learner. It exists, but it has not learned from our data yet.

BEFORE fit() Model created

Has not learned from our training examples yet.

AFTER fit() Model trained

Has learned patterns from the training data.

06

Train the Model With fit()

model.fit(X, y)

This is one of the most important lines in the entire program.

fit() tells the model to learn from the training examples.

X Input Features

Study hours

+
y Correct Labels

Pass / Fail

fit() Learn Patterns

The model learns from the examples.

No prediction happens here. The model is learning from known examples.
07

Give the Model New Data

new_student = [[6]]

Now we create a new input that the model has to classify.

In this example:

NEW STUDENT 6 hours of study

The important point is that we are not giving the correct answer to the model.

We only provide the input:

[[6]]

The model must decide whether this belongs to Class 0 or Class 1.

08

Make the Prediction

prediction = model.predict(new_student)

The predict() method asks the trained model to choose the most likely class for the new data.

NEW DATA [[6]]
predict() Classifier
RESULT [1]

Because we defined Class 1 as Pass:

[1] → Pass
09

Get the Prediction Probabilities

probability = model.predict_proba(new_student)

Instead of asking only which class the model chooses, we can ask for the probability of every class.

For example:

[[0.10 0.90]]
CLASS 0 0.10 10% → Fail
CLASS 1 0.90 90% → Pass

So the model predicts Class 1 because its probability is higher.

10

Understand classes_

print("Classes:", model.classes_)

The classes_ attribute tells us the order of the classes used by the model.

For our example:

Classes: [0 1]
POSITION 1 Class 0 → Fail
POSITION 2 Class 1 → Pass

Therefore, if the probability output is:

[[0.10 0.90]]

we know:

0.10 Class 0 → Fail
0.90 Class 1 → Pass
11

The print() Statements

print("Classes:", model.classes_)
print("Prediction:", prediction)
print("Probabilities:", probability)

These lines simply display the results in the terminal.

Classes Shows the class order.
Prediction Shows the predicted class.
Probabilities Shows the probability of each class.

For example, the terminal might show:

Classes: [0 1]
Prediction: [1]
Probabilities: [[0.10 0.90]]
12

Read the Output Like a Human

Let's translate the output into normal language.

PYTHON OUTPUT Prediction: [1]
MEANING Student is predicted to Pass

And:

PYTHON OUTPUT [0.10, 0.90]
MEANING 10% Fail / 90% Pass
Learning to translate model output into plain language is more important than memorizing Python syntax.
13

The Whole Program in Simple Words

1 Import the algorithm

Get Logistic Regression from scikit-learn.

2 Prepare X

Store the input features.

3 Prepare y

Store the correct class labels.

4 Create the model

Create Logistic Regression.

5 Train

Use fit(X, y).

6 Predict

Use predict() with new data.

7 Check probabilities

Use predict_proba().

14

One Important Thing to Remember

The model does not memorize the answer for the new student.

We trained it using examples such as:

1 hour Fail
3 hours Fail
5 hours Pass
7 hours Pass

Then we gave it a new input:

NEW DATA 6 hours

The model uses the pattern it learned from the training data to make a prediction.

This is the fundamental idea of Machine Learning: learn patterns from examples and use those patterns on new data.
REMEMBER THIS

Understand the workflow, not just the syntax.

If you understand what X, y, fit(), predict(), predict_proba(), and classes_ mean, you understand the basic Python code behind this classifier.

X y fit() predict() predict_proba()
QUICK CHECK

Check Your Understanding

What is X? The input features used by the model.
What is y? The target labels the model learns to predict.
What does fit(X, y) do? It trains the model using the input features and known labels.
What does predict() do? It predicts the class for new data.
What does predict_proba() do? It returns the probability for each class.
Why is classes_ useful? It tells us the order of the classes in the probability output.
LESSON 8 COMPLETE

You can now read a basic classification program.

You understand the data, the model, training, prediction, and prediction probabilities. The next lesson moves to another important classification algorithm: Decision Trees.