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.
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.
What Are We Building?
Our goal is simple:
This is a classification problem because the output is a category rather than a continuous numerical value.
Step 1 — Prepare the Dataset
First, we need some training data. Each student has a number of study hours and a result.
Machine Learning models work with numbers, so we will represent the classes as:
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
]
Study hours used by the model to make a prediction.
The result we want the model to predict.
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.
A popular Python library for Machine Learning.
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.
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.
Study Hours
Pass / Fail
Model learns patterns
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.
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:
This means the model considers the Pass class more likely for this example.
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.
What Actually Happened?
Let's translate the Python code into simple Machine Learning language.
One More Simple Example
The same process can be used for other binary classification problems.
Email → Spam / Not Spam
Transaction → Fraud / Not Fraud
The problem changes, but the basic Machine Learning workflow remains similar:
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.