Python for Data Science — Your First Classification Model
In the previous article, we built our first Regression Model and learned how machine learning can predict numerical values such as:
- House Prices
- Revenue
- Sales
- Demand
But many real-world machine learning problems are different.
Instead of predicting numbers, we often need to predict categories.
For example:
- Spam or Not Spam
- Fraud or Not Fraud
- Customer Churn or No Churn
- Disease or No Disease
These are known as Classification Problems.
Classification is one of the most common applications of machine learning and powers many systems we interact with every day.
What Is Classification?
Classification is a type of supervised learning where the goal is to predict a category or class.
Unlike regression, which predicts numerical values:
House Price = $250,000classification predicts labels such as:
Spam
Not Spamor
Fraud
Not FraudThe output is a category rather than a number.
Real-World Examples
Classification appears everywhere.
Email Filtering
Predict:
- Spam
- Not Spam
Fraud Detection
Predict:
- Fraudulent Transaction
- Legitimate Transaction
Customer Churn
Predict:
- Customer Leaves
- Customer Stays
Medical Diagnosis
Predict:
- Disease Present
- Disease Absent
Many machine learning systems are essentially classification systems.
Understanding Classes
A class is simply a possible outcome.
Example:
Customer Churn Dataset:
Possible classes:
Yes
NoThe model learns patterns that help determine which class new observations belong to.
A Simple Example
Suppose we have customer data:
Features:
- Usage Hours
- Support Tickets
Label:
- Churn
The goal is to learn patterns connecting customer behavior to churn outcomes.
Our First Classification Algorithm
For beginners, one of the simplest classification models is:
Logistic Regression
The name can be confusing.
Despite the word “Regression,” it is actually a classification algorithm.
It is commonly used for predicting categories, especially binary outcomes.
Examples:
- Yes / No
- True / False
- Fraud / Not Fraud
Step 1: Define Features and Labels
Suppose:
X = data[["usage_hours", "support_tickets"]]Features.
And:
y = data["churn"]Label.
Step 2: Split the Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)As always:
Training data teaches the model.
Test data evaluates the model.
Step 3: Create the Model
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()At this point:
The model has not learned anything yet.
Step 4: Train the Model
model.fit(X_train, y_train)The model studies historical examples and learns relationships between:
- Features
- Churn Outcomes
Step 5: Make Predictions
predictions = model.predict(X_test)Now the model predicts:
Yes
Nofor unseen customers.
This is classification in action.
What Is the Model Actually Learning?
Suppose historical data shows:
Get Sudha Rani Maddala’s stories in your inbox
Join Medium for free to get updates from this writer.
Customers who:
- use the product very little
- create many support tickets
are more likely to leave.
The model learns these patterns automatically.
It does not memorize rules.
It discovers relationships from data.
Why Classification Matters
Many business decisions involve categories.
Examples:
Banking
Approve Loan?
Yes / NoMarketing
Will Customer Respond?
Yes / NoSecurity
Is Transaction Fraudulent?
Yes / NoHealthcare
Disease Detected?
Yes / NoClassification models help automate these decisions.
Evaluating a Classification Model
In regression, we used metrics such as:
- Mean Absolute Error
Classification requires different metrics.
The simplest is:
Accuracy
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(
y_test,
predictions
)Accuracy measures:
What percentage of predictions were correct?
Example:
Actual:
Yes
No
Yes
NoPredicted:
Yes
No
No
NoCorrect predictions:
3 out of 4Accuracy:
75%Is Accuracy Always Enough?
Not necessarily.
Imagine fraud detection.
Dataset:
99% Legitimate
1% FraudA model that predicts:
Always Legitimateachieves:
99% AccuracyBut it completely fails at detecting fraud.
This is why data scientists use additional metrics.
We’ll explore those next.
Classification vs Regression
A useful comparison:
Both are supervised learning.
The difference lies in the type of prediction.
Common Beginner Mistakes
Mistake 1: Using Regression for Classification Problems
Always consider the type of output.
Mistake 2: Focusing Only on Accuracy
High accuracy does not always mean a useful model.
Mistake 3: Ignoring Class Imbalance
Some categories may be much rarer than others.
Mistake 4: Expecting Perfect Predictions
Classification models deal with probabilities and uncertainty.
Errors are inevitable.
Real-World Applications
Classification is used extensively in:
Fraud Detection
Identifying suspicious activity.
Healthcare
Diagnosing diseases.
Cybersecurity
Detecting malicious behavior.
Customer Analytics
Predicting churn and engagement.
Marketing
Targeting likely responders.
Many machine learning systems are fundamentally classification systems.
Thinking Like a Data Scientist
Beginners often ask:
Did the model predict correctly?
Experienced practitioners ask:
How well does the model perform across different types of errors?
That question becomes increasingly important as machine learning projects become more complex.
What’s Next?
We just introduced Accuracy.
But accuracy is only one way to evaluate classification models.
In many real-world situations, it can be misleading.
To evaluate models properly, we need to understand:
- Precision
- Recall
- F1-Score
These metrics help us understand not only whether predictions are correct, but also the types of mistakes the model makes.
Key Takeaway
Classification models predict categories rather than numerical values. They are widely used in applications such as fraud detection, spam filtering, customer churn prediction, and medical diagnosis.









