Mastering Machine Learning — Part 1: Start from Binary to Multi-Class Classification in Python

Connie Zhou
3 min readJan 15, 2024

--

multi-class classification

In the vast landscape of machine learning, classification is a fundamental task that involves categorizing data into different classes or groups. This blog post will take you on a journey from the basics of binary classification to the complexities of multi-class classification, providing Python code examples along the way. Let’s dive in!

Binary Classification:

Understanding the Basics:

Binary classification involves distinguishing between two classes, typically denoted as positive and negative. Common examples include spam detection, fraud detection, and medical diagnosis.

# Importing necessary libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns

# Loading the Iris dataset
iris = load_iris()
X, y = iris.data, iris.target

# For simplicity, let's consider only the first two features (sepal length and sepal width)
X_binary = X[:, :2]

# Mapping the classes: setosa (class 0) or not setosa (classes 1 and 2)
y_binary = (y == 0).astype(int)

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_binary, y_binary, test_size=0.2, random_state=42)

# Initializing the Logistic Regression model
model = LogisticRegression()

# Training the model
model.fit(X_train, y_train)

# Making predictions on the test set
predictions = model.predict(X_test)

# Evaluating the model
accuracy = accuracy_score(y_test, predictions)
conf_matrix = confusion_matrix(y_test, predictions)

# Visualizing the Confusion Matrix
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, cmap='Blues', fmt='g')
plt.title('Confusion Matrix for Binary Classification')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
binary classification

Multi-Class Classification:

Handling Multiple Classes:

In multi-class classification, the task is to classify instances into more than two classes. This can be seen in applications like image recognition, where objects belong to various categories.

# Importing additional libraries
from sklearn.datasets import load_iris
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC

# Loading the Iris dataset (an example of a multi-class problem)
iris = load_iris()
X, y = iris.data, iris.target

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initializing the Support Vector Classifier (SVC) for multi-class
model_multi = OneVsRestClassifier(SVC())

# Training the model
model_multi.fit(X_train, y_train)

# Making predictions on the test set
predictions_multi = model_multi.predict(X_test)

# Evaluating the model
accuracy_multi = accuracy_score(y_test, predictions_multi)
conf_matrix_multi = confusion_matrix(y_test, predictions_multi)

# Visualizing the Confusion Matrix for Multi-Class
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix_multi, annot=True, cmap='Greens', fmt='g')
plt.title('Confusion Matrix for Multi-Class Classification')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
multi-class classification

Congratulations! You’ve just explored the journey from binary to multi-class classification in machine learning. The provided Python code examples, along with visualizations, should serve as a valuable resource as you venture into the exciting world of classification algorithms. Feel free to experiment with different datasets and models to deepen your understanding and hone your skills in applied machine learning. Happy coding!

--

--

Connie Zhou

Proficient in analyzing, modeling, and deploying ML solutions, I publish one tech blog every week.