MACHINE LEARNING • LESSON 8

Build a Classifier With Python

Now that we understand classification and Logistic Regression, let's build a simple classification model with Python and scikit-learn.

THE GOAL

Train a model that can predict whether a student will Pass or Fail.

We will give the model examples of students and their study hours. The model will learn the pattern and then predict the result for a new student.

01

What Are We Building?

Our goal is simple:

INPUT Study Hours
MODEL Classifier
OUTPUT Pass / Fail

This is a classification problem because the output is a category rather than a continuous numerical value.

02

Step 1 — Prepare the Dataset

First, we need some training data. Each student has a number of study hours and a result.

Study Hours Result
1 Fail
2 Fail
3 Fail
5 Pass
6 Pass
7 Pass

Machine Learning models work with numbers, so we will represent the classes as:

CLASS 0 Fail
CLASS 1 Pass
03

Step 2 — Create X and y

In scikit-learn, we normally separate the input features from the target labels.

We commonly call them X and y.

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

y = [
    0,
    0,
    0,
    1,
    1,
    1
]
X Input Features

Study hours used by the model to make a prediction.

y Target Labels

The result we want the model to predict.

Think of X as "what the model sees" and y as "what we want the model to learn to predict."
04

Step 3 — Import the Classifier

We will use Logistic Regression from scikit-learn.

from sklearn.linear_model import LogisticRegression

This imports the Logistic Regression classifier that we will use to build our model.

LIBRARY scikit-learn

A popular Python library for Machine Learning.

05

Step 4 — Create the Model

Next, we create an instance of Logistic Regression.

model = LogisticRegression()

At this point, we have created the model object, but it has not learned anything yet.

BEFORE TRAINING Model has not learned the dataset
NEXT Train the model
Creating a model and training a model are two different steps.
06

Step 5 — Train the Model

Now we give the training data to the model using the fit() method.

model.fit(X, y)

This is where the model learns the relationship between study hours and the Pass/Fail labels.

TRAINING INPUT X

Study Hours

+
TRAINING LABELS y

Pass / Fail

LEARNING model.fit()

Model learns patterns

07

Step 6 — Make a Prediction

After training, we can give the model a new student's study hours.

Suppose a new student studied for 6 hours.

prediction = model.predict([[6]])

print(prediction)

The model uses what it learned from the training data and predicts the class for the new student.

NEW INPUT 6 hours
TRAINED MODEL Logistic Regression
PREDICTION Class 1 → Pass
08

Step 7 — See the Prediction Probability

We can also ask the model for the probability of each class using predict_proba().

probability = model.predict_proba([[6]])

print(probability)

The output will contain a probability for each class. For example, it might look conceptually like:

CLASS 0 Fail 0.10
CLASS 1 Pass 0.90

This means the model considers the Pass class more likely for this example.

predict() gives the class. predict_proba() gives the probability for each class.
09

The Complete Python Code

Now let's put all the steps together.

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)

# Make a prediction
prediction = model.predict([[6]])

# Get probabilities
probability = model.predict_proba([[6]])

print("Prediction:", prediction)
print("Probability:", probability)

The important part is not memorizing every line. Understand the sequence.

1 Prepare X and y
2 Create model
3 Train with fit()
4 Predict with predict()
5 Check probability
10

What Actually Happened?

Let's translate the Python code into simple Machine Learning language.

X Input features
y Correct answers
fit() Learn from examples
predict() Predict a class
predict_proba() Show class probabilities
Training Data Train Model New Data Prediction
11

One More Simple Example

The same process can be used for other binary classification problems.

EXAMPLE 1 Spam Detection

Email → Spam / Not Spam

EXAMPLE 2 Fraud Detection

Transaction → Fraud / Not Fraud

The problem changes, but the basic Machine Learning workflow remains similar:

Data → Train → Predict → Evaluate
REMEMBER THIS

Building a classifier is a simple sequence of steps.

Prepare the input data, create a model, train it, give it new data, and make a prediction.

X fit() Model predict() Class
QUICK CHECK

Check Your Understanding

What does X contain? The input features used by the model.
What does y contain? The target labels the model should learn to predict.
What does fit() do? It trains the model using the training data.
What does predict() do? It predicts the class for new input data.
NEXT TOPIC

Prediction Probabilities

Next, we will look more closely at how a classifier gives probabilities and what those probabilities mean.