What is Classification?
Classification is a supervised machine learning technique used to predict which category or class an observation belongs to.
The variables used by the model to make a prediction.
The category that we want the model to predict.
Historical observations containing features and known class labels.
The class predicted by the trained classification model.
A typical classification project follows a sequence of data preparation, model training, prediction, and evaluation.
| Classification | Regression |
|---|---|
| Predicts categories | Predicts numerical values |
| Spam / Not Spam | House Price |
| Fraud / Not Fraud | Monthly Sales |
| Pass / Fail | Employee Salary |
| Churn / Stay | Electricity Consumption |
Binary Classification
There are two possible classes.
Example: Fraud / Not Fraud
Multiclass Classification
There are more than two possible classes.
Example: Low / Medium / High
Multilabel Classification
One observation can belong to multiple labels.
Example: Article topics
Scikit-learn provides many practical classification algorithms.
pip install pandas numpy matplotlib scikit-learn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
confusion_matrix,
classification_report
)
Suppose an online company wants to predict whether a customer will purchase a product after visiting its website.
data = {
"age": [22, 25, 28, 35, 40, 45, 50, 52, 30, 38],
"visits": [2, 3, 4, 6, 7, 8, 9, 10, 5, 6],
"time_minutes": [5, 8, 10, 18, 20, 25, 30, 35, 15, 17],
"purchased": [0, 0, 0, 1, 1, 1, 1, 1, 0, 1]
}
df = pd.DataFrame(data)
print(df)
In this example:
- age → customer age
- visits → number of website visits
- time_minutes → time spent on the website
- purchased → target class
The target has two classes:
X = df[["age", "visits", "time_minutes"]]
y = df["purchased"]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
print("Training rows:", len(X_train))
print("Testing rows:", len(X_test))
Logistic regression is a commonly used classification algorithm. Despite its name, it is generally used to estimate the probability of class membership and then assign classes using a decision rule.
The output probability can be converted into a class prediction.
model = LogisticRegression()
model.fit(X_train, y_train)
print("Model trained successfully")
predictions = model.predict(X_test)
print(predictions)
Suppose a new customer is 42 years old, has visited the website 8 times, and spent 24 minutes browsing.
new_customer = pd.DataFrame({
"age": [42],
"visits": [8],
"time_minutes": [24]
})
prediction = model.predict(new_customer)
if prediction[0] == 1:
print("Customer is predicted to purchase")
else:
print("Customer is predicted not to purchase")
Classification models can often provide probabilities for the possible classes.
probability = model.predict_proba(new_customer)
print(probability)
Probabilities are useful when a business needs more information than only the final class label.
A binary classifier may convert a predicted probability into a class using a threshold. A commonly used default is 0.5, but the appropriate threshold depends on the application.
probability = model.predict_proba(new_customer)[0][1]
threshold = 0.5
if probability >= threshold:
print("Predicted class: 1")
else:
print("Predicted class: 0")
Accuracy measures the proportion of predictions that match the actual classes.
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)
A confusion matrix summarizes classification predictions by comparing actual classes with predicted classes.
| Predicted Negative | Predicted Positive | |
|---|---|---|
| Actual Negative | True Negative | False Positive |
| Actual Positive | False Negative | True Positive |
cm = confusion_matrix(y_test, predictions)
print(cm)
Precision measures how many observations predicted as positive were actually positive.
Precision can be particularly important when false positive predictions have significant consequences.
Recall measures how many of the actual positive observations were correctly identified.
Recall can be particularly important when missing a positive case has a significant consequence.
F1 score combines precision and recall using their harmonic mean.
It can be useful when both false positives and false negatives matter.
print(
classification_report(
y_test,
predictions
)
)
The classification report provides precision, recall, F1 score, and support for each class.
| Algorithm | Typical Use |
|---|---|
| Logistic Regression | Binary and multiclass classification |
| Decision Tree | Rule-based classification |
| Random Forest | Robust classification on structured data |
| K-Nearest Neighbors | Similarity-based classification |
| Support Vector Machine | Classification with separating boundaries |
| Naive Bayes | Text and probabilistic classification |
A decision tree makes predictions by applying a sequence of decision rules to the features.
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
Random Forest combines predictions from multiple decision trees. It is commonly used for structured classification datasets.
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(
n_estimators=100,
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
KNN predicts a class using the labels of nearby observations.
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
Support Vector Machines attempt to find a decision boundary that separates classes while maximizing the margin under the chosen formulation.
from sklearn.svm import SVC
model = SVC()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
Some algorithms perform better when numerical features are placed on comparable scales.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model = LogisticRegression()
model.fit(X_train_scaled, y_train)
predictions = model.predict(X_test_scaled)
print("Accuracy:", accuracy_score(y_test, predictions))
A telecom company wants to predict whether a customer is likely to leave the service.
data = {
"months": [3, 8, 12, 24, 36, 48, 60, 72],
"support_calls": [5, 4, 3, 2, 2, 1, 1, 0],
"monthly_bill": [900, 850, 800, 750, 700, 650, 600, 550],
"churn": [1, 1, 1, 0, 0, 0, 0, 0]
}
df = pd.DataFrame(data)
X = df[["months", "support_calls", "monthly_bill"]]
y = df["churn"]
model = LogisticRegression()
model.fit(X, y)
customer = pd.DataFrame({
"months": [10],
"support_calls": [4],
"monthly_bill": [820]
})
prediction = model.predict(customer)
print("Churn prediction:", prediction[0])
An email system can classify messages as spam or legitimate.
| Email Feature | Possible Signal |
|---|---|
| Number of links | Large numbers may provide useful predictive information |
| Text content | Words and phrases can be transformed into numerical features |
| Sender information | Historical sender patterns may provide signals |
| Message length | Can be included as a numerical feature |
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
emails = [
"meeting at 10 am",
"project report attached",
"win free money now",
"claim your prize today",
"team meeting tomorrow",
"free offer click now"
]
labels = [0, 0, 1, 1, 0, 1]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(emails)
model = MultinomialNB()
model.fit(X, labels)
new_email = ["claim your free prize"]
new_X = vectorizer.transform(new_email)
prediction = model.predict(new_X)
print("Spam prediction:", prediction[0])
A financial organization could classify applications into categories such as approved or not approved using historical application data, subject to applicable policies and validation requirements.
data = {
"income": [30000, 45000, 50000, 70000, 80000, 90000],
"credit_score": [580, 620, 650, 720, 750, 780],
"loan_amount": [25000, 30000, 35000, 30000, 40000, 45000],
"approved": [0, 0, 1, 1, 1, 1]
}
df = pd.DataFrame(data)
X = df[["income", "credit_score", "loan_amount"]]
y = df["approved"]
model = LogisticRegression()
model.fit(X, y)
application = pd.DataFrame({
"income": [65000],
"credit_score": [700],
"loan_amount": [30000]
})
prediction = model.predict(application)
print("Predicted class:", prediction[0])
A payment company can classify transactions as potentially fraudulent or legitimate using historical transaction data.
data = {
"amount": [500, 800, 1200, 200, 15000, 18000, 300, 700],
"transactions_today": [2, 3, 4, 1, 15, 18, 2, 3],
"international": [0, 0, 0, 0, 1, 1, 0, 0],
"fraud": [0, 0, 0, 0, 1, 1, 0, 0]
}
df = pd.DataFrame(data)
X = df[["amount", "transactions_today", "international"]]
y = df["fraud"]
model = LogisticRegression()
model.fit(X, y)
transaction = pd.DataFrame({
"amount": [12000],
"transactions_today": [10],
"international": [1]
})
prediction = model.predict(transaction)
print("Fraud prediction:", prediction[0])
Class imbalance occurs when one class has substantially more observations than another.
| Example | Class Distribution |
|---|---|
| Normal transactions | 99% |
| Fraudulent transactions | 1% |
A model could achieve high accuracy by mostly predicting the majority class, while still performing poorly at identifying the minority class.
Precision, recall, F1 score, confusion matrices, and suitable threshold analysis can provide additional insight.
Some scikit-learn classifiers support class weighting to give more importance to underrepresented classes during training.
model = LogisticRegression(
class_weight="balanced"
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))
Multiclass classification involves more than two possible classes. For example, a company might classify customers into low, medium, or high-value groups.
data = {
"visits": [2, 3, 5, 6, 8, 9, 12, 15, 18],
"spending": [500, 700, 1200, 1500, 2500, 3000, 5000, 7000, 9000],
"customer_type": [
"Low", "Low", "Low",
"Medium", "Medium", "Medium",
"High", "High", "High"
]
}
df = pd.DataFrame(data)
X = df[["visits", "spending"]]
y = df["customer_type"]
model = LogisticRegression(max_iter=1000)
model.fit(X, y)
customer = pd.DataFrame({
"visits": [10],
"spending": [3500]
})
prediction = model.predict(customer)
print("Customer type:", prediction[0])
Tree-based models can provide feature importance estimates that help analysts explore which input variables contributed to the model's decisions.
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(
n_estimators=100,
random_state=42
)
model.fit(X_train, y_train)
importance = pd.Series(
model.feature_importances_,
index=X_train.columns
)
print(importance.sort_values(ascending=False))
Cross-validation can provide a more stable estimate of classification performance than relying on one train-test split alone.
from sklearn.model_selection import cross_val_score
model = LogisticRegression()
scores = cross_val_score(
model,
X,
y,
cv=5,
scoring="accuracy"
)
print("Scores:", scores)
print("Average accuracy:", scores.mean())
An organization can analyze historical employee records to study whether an employee is likely to leave, using an appropriately designed and validated classification model.
data = {
"experience": [1, 2, 3, 5, 7, 9, 12, 15],
"satisfaction": [2, 3, 3, 4, 4, 5, 5, 5],
"overtime_hours": [20, 18, 15, 10, 8, 5, 4, 2],
"left_company": [1, 1, 1, 0, 0, 0, 0, 0]
}
df = pd.DataFrame(data)
X = df[["experience", "satisfaction", "overtime_hours"]]
y = df["left_company"]
model = LogisticRegression()
model.fit(X, y)
employee = pd.DataFrame({
"experience": [3],
"satisfaction": [3],
"overtime_hours": [17]
})
prediction = model.predict(employee)
print("Predicted class:", prediction[0])
In real data analysis projects, classification data commonly comes from CSV files, databases, APIs, or data warehouses.
df = pd.read_csv("customers.csv")
print(df.head())
X = df[
[
"age",
"visits",
"time_minutes"
]
]
y = df["purchased"]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(
classification_report(
y_test,
predictions
)
)
A pipeline combines preprocessing and modeling steps into one workflow.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
pipeline = Pipeline([
("scaler", StandardScaler()),
("model", LogisticRegression())
])
pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
For binary classification, the ROC curve examines the trade-off between true positive rate and false positive rate across classification thresholds.
from sklearn.metrics import roc_auc_score
probabilities = model.predict_proba(X_test)[:, 1]
auc = roc_auc_score(
y_test,
probabilities
)
print("ROC AUC:", auc)
An e-commerce company wants to identify visitors who may purchase during their current session.
| Feature | Example |
|---|---|
| Pages viewed | 12 |
| Time on website | 25 minutes |
| Previous purchases | 4 |
| Cart items | 3 |
| Target | Purchased / Not Purchased |
An e-commerce platform can classify products into categories such as electronics, clothing, books, or home products using product attributes and appropriately engineered text or numerical features.
data = {
"weight": [1.2, 0.8, 2.5, 0.3, 1.0, 2.8],
"price": [15000, 2500, 1200, 500, 18000, 900],
"rating": [4.5, 4.2, 4.0, 4.6, 4.4, 3.9],
"category": [
"Electronics",
"Clothing",
"Books",
"Clothing",
"Electronics",
"Books"
]
}
df = pd.DataFrame(data)
X = df[["weight", "price", "rating"]]
y = df["category"]
model = LogisticRegression(max_iter=1000)
model.fit(X, y)
product = pd.DataFrame({
"weight": [1.5],
"price": [14000],
"rating": [4.3]
})
prediction = model.predict(product)
print("Predicted category:", prediction[0])
A school analytics system can classify historical student records into categories such as Pass or Fail based on appropriate academic features.
data = {
"study_hours": [1, 2, 3, 4, 5, 6, 7, 8],
"attendance": [60, 65, 70, 75, 80, 85, 90, 95],
"assignments": [2, 3, 4, 5, 6, 7, 8, 9],
"result": [0, 0, 0, 1, 1, 1, 1, 1]
}
df = pd.DataFrame(data)
X = df[["study_hours", "attendance", "assignments"]]
y = df["result"]
model = LogisticRegression()
model.fit(X, y)
student = pd.DataFrame({
"study_hours": [5],
"attendance": [82],
"assignments": [6]
})
prediction = model.predict(student)
print("Predicted result:", prediction[0])
A company can automatically classify customer support tickets into categories such as Billing, Technical Support, Delivery, or Account.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
tickets = [
"payment failed on my card",
"invoice amount is incorrect",
"my package is late",
"delivery has not arrived",
"app is not opening",
"website shows an error",
"please update my account",
"I cannot change my password"
]
labels = [
"Billing",
"Billing",
"Delivery",
"Delivery",
"Technical",
"Technical",
"Account",
"Account"
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(tickets)
model = LogisticRegression()
model.fit(X, labels)
new_ticket = [
"my payment was declined"
]
new_X = vectorizer.transform(new_ticket)
prediction = model.predict(new_X)
print("Ticket category:", prediction[0])
Different classification algorithms may behave differently on the same dataset. A practical workflow can compare several suitable models.
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
models = {
"Logistic Regression": LogisticRegression(),
"Decision Tree": DecisionTreeClassifier(random_state=42),
"Random Forest": RandomForestClassifier(
n_estimators=100,
random_state=42
)
}
for name, model in models.items():
model.fit(X_train, y_train)
predictions = model.predict(X_test)
score = accuracy_score(
y_test,
predictions
)
print(name, ":", score)
The following example combines the major steps of a customer purchase classification workflow.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
classification_report,
confusion_matrix
)
# 1. Create customer dataset
data = {
"age": [22, 25, 28, 35, 40, 45, 50, 52, 30, 38,
27, 33, 41, 48, 55],
"visits": [2, 3, 4, 6, 7, 8, 9, 10, 5, 6,
3, 5, 7, 9, 11],
"time_minutes": [5, 8, 10, 18, 20, 25, 30, 35, 15, 17,
9, 14, 22, 29, 40],
"purchased": [0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
0, 0, 1, 1, 1]
}
df = pd.DataFrame(data)
# 2. Select features and target
X = df[
[
"age",
"visits",
"time_minutes"
]
]
y = df["purchased"]
# 3. Split data
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
# 4. Create pipeline
model = Pipeline([
("scaler", StandardScaler()),
("classifier", LogisticRegression())
])
# 5. Train
model.fit(X_train, y_train)
# 6. Predict
predictions = model.predict(X_test)
# 7. Evaluate
print(
"Accuracy:",
accuracy_score(y_test, predictions)
)
print("\nConfusion Matrix:")
print(
confusion_matrix(
y_test,
predictions
)
)
print("\nClassification Report:")
print(
classification_report(
y_test,
predictions
)
)
# 8. Predict a new customer
new_customer = pd.DataFrame({
"age": [42],
"visits": [8],
"time_minutes": [24]
})
prediction = model.predict(new_customer)
probability = model.predict_proba(new_customer)
print("\nPredicted class:", prediction[0])
print("Class probabilities:", probability)
| Metric | Question It Helps Answer |
|---|---|
| Accuracy | How many predictions were correct overall? |
| Precision | When the model predicts positive, how often is it correct? |
| Recall | How many actual positive cases did the model identify? |
| F1 Score | How well are precision and recall balanced? |
- Class imbalance: One class has many more observations than another.
- Overfitting: The model learns training-specific noise.
- Underfitting: The model is too simple to capture useful patterns.
- Data leakage: Information unavailable at prediction time enters the model.
- Incorrect metric: Accuracy may not be sufficient for every problem.
- Data quality: Incorrect labels or missing values can affect performance.
- Distribution shift: Future data may differ from historical training data.
- Define the classification problem.
- Identify the target classes.
- Collect representative historical data.
- Inspect missing values and incorrect labels.
- Explore class distribution.
- Select useful features.
- Encode categorical variables when necessary.
- Scale features when appropriate for the chosen algorithm.
- Split data into training and evaluation sets.
- Train one or more suitable classification models.
- Generate predictions and probabilities where appropriate.
- Evaluate using suitable metrics.
- Inspect the confusion matrix.
- Check for overfitting and data leakage.
- Validate performance on representative unseen data.
- Monitor the model after deployment.
- Classification predicts categories rather than continuous numerical values.
- Clearly define the target classes before training a model.
- Use historical labeled data for supervised classification.
- Keep training and evaluation data separate.
- Use pipelines to keep preprocessing and modeling consistent.
- Accuracy is not always the most informative metric.
- Use precision and recall when the costs of false positives and false negatives differ.
- Use confusion matrices to understand the types of errors being made.
- Check class imbalance before interpreting accuracy.
- Watch for data leakage and overfitting.
- Choose classification algorithms based on the dataset and problem.
- Validate the model on data representative of its future use.
- For high-impact applications, include appropriate human oversight and governance.
Python Data Analysis - Classification
Learn classification concepts through practical Python and real-world examples.