Affinity Propagation Clustering Method in ML
By Mukesh Kumar
Updated on May 06, 2025 | 22 min read | 1.34K+ views
Share:
For working professionals
For fresh graduates
More
By Mukesh Kumar
Updated on May 06, 2025 | 22 min read | 1.34K+ views
Share:
Table of Contents
Did you know? Affinity Propagation clustering was originally developed for message-passing in brain-like networks, inspired by how neurons communicate. It has become a powerful tool for discovering natural groupings in complex data without needing to predefine the number of clusters.
Affinity Propagation Clustering is a machine learning technique that groups data points based on similarity, without needing to predefine the number of clusters. Unlike k-means, which requires selecting the number of clusters upfront, it automatically determines the optimal number using a similarity matrix.
This method is particularly effective for scenarios where the underlying structure of the data is unknown and can adapt to complex datasets.
In this tutorial, we’ll explore the fundamentals of Affinity Propagation clustering in ML, covering its principles, advantages, and implementation for clustering.
Improve your machine learning skills with our online AI and ML courses — take the next step in your learning journey!
Affinity Propagation is a clustering algorithm that automatically identifies exemplars, the most representative points of a cluster, in a dataset.
Traditional clustering methods such as k-means require the number of clusters to be predefined. In contrast, Affinity Propagation does not make this assumption. This flexibility makes it particularly useful for exploratory data analysis, where the number of clusters is unknown.
The method was introduced by Frey and Dueck in 2007 and has since been applied in various fields like biology, computer vision, and social network analysis due to its ability to handle complex data structures.
Machine learning professionals skilled in techniques like affinity propagation are in high demand due to their ability to handle complex data. If you're looking to develop skills in AI and ML, here are some top-rated courses to help you get there:
Affinity Propagation uses two key matrices: responsibility and availability. These matrices are iteratively updated until the algorithm converges, and the final exemplars are selected.
Responsibility Matrix: This matrix indicates how well-suited each data point is to serve as an exemplar for another point. For example, if point A is similar to point B, the responsibility matrix will indicate that point A can potentially serve as the exemplar for point B.
Availability Matrix: This matrix shows how much each data point wants to choose another point as an exemplar. Essentially, it measures how much each point "prefers" a potential exemplar. If point A is much closer to point B than other points, point A will have a high value in the availability matrix for B
The algorithm alternates between updating these matrices:
This back-and-forth update process continues until the algorithm reaches a stable state (convergence), where points are assigned to clusters, and exemplars are determined.
Why It’s Unique? Unlike algorithms such as k-means, where the user has to decide the number of clusters upfront, Affinity Propagation automatically determines the optimal number of clusters based on the data's inherent structure.
The process of identifying exemplars and assigning points to them makes Affinity Propagation well-suited for complex datasets where traditional clustering methods may struggle to identify the true structure of the data.
Also Read: What is Clustering in Machine Learning and Different Types of Clustering Methods
Now that you know what is affinity propagation clustering, let’s understand how it works with an example.
Affinity Propagation is an iterative clustering algorithm that works by identifying exemplars (representative data points) and grouping other points based on their similarity to these exemplars. The algorithm alternates between two key processes: calculating responsibilities and updating availabilities.
Here’s a step-by-step breakdown of how Affinity Propagation works:
The first step in Affinity Propagation is to calculate the similarity matrix, which reflects how similar each pair of data points is.
How it works? We compute a similarity measure (e.g., Euclidean distance or cosine similarity) between all pairs of points. The higher the similarity, the closer the points are in the matrix.
Example: Let's say the similarity between the data points is computed as follows (higher values indicate greater similarity):
A B C D E
A 1.0 0.8 0.6 0.2 0.3
B 0.8 1.0 0.7 0.3 0.4
C 0.6 0.7 1.0 0.5 0.2
D 0.2 0.3 0.5 1.0 0.7
E 0.3 0.4 0.2 0.7 1.0
Initialize the responsibility and availability matrices. These are initially filled with random values or zeros.
How it works?
Example: Initially, both matrices are set to zero or random values:
Responsibility Matrix (r) Availability Matrix (a)
A B C D E A B C D E
A 0 0 0 0 0 A 0 0 0 0 0
B 0 0 0 0 0 B 0 0 0 0 0
C 0 0 0 0 0 C 0 0 0 0 0
D 0 0 0 0 0 D 0 0 0 0 0
E 0 0 0 0 0 E 0 0 0 0 0
The responsibility matrix is updated iteratively. It represents how much data point i should assign to point k as an exemplar.
How it works? Each entry in the responsibility matrix is updated using the formula.
This means the responsibility of point i towards k is calculated by subtracting the maximum availability of other points from the similarity score.
Example: After the first iteration, the responsibility matrix might be updated like this:
Responsibility Matrix (r)
A B C D E
A 0 0.1 0.2 -0.1 0.1
B 0 0.2 0.1 -0.2 0.05
C 0.1 0.05 0.3 -0.1 0.1
D -0.1 0.05 -0.05 0.5 0.3
E 0.2 0.1 0.05 0.2 0.4
The availability matrix is updated based on the responsibility matrix. It shows how much each data point supports other points to become an exemplar.
How it works? The availability of a point i to choose k as an exemplar is calculated using the formula:
This means the availability of a point to select another as an exemplar is updated based on the maximum responsibility values.
Example: After the first update, the availability matrix might look like this:
Availability Matrix (a)
A B C D E
A 0 0.1 0.2 0.05 0.1
B 0.1 0 0.2 0.05 0.1
C 0.2 0.1 0 0.05 0.15
D 0.05 0.1 0.05 0 0.2
E 0.1 0.2 0.1 0.15 0
The responsibility and availability matrices are updated alternately, iterating several times until the algorithm converges.
How it works? The updates are repeated, refining the responsibility and availability values after each iteration, until the changes between iterations become small, indicating convergence.
Example: After several iterations, the matrices stabilize, and points are assigned to clusters based on the final responsibility and availability values.
Once the algorithm converges, the final exemplars are selected, and the data points are assigned to the closest exemplar.
How it works? The points with the highest responsibility values for themselves are chosen as exemplars, and all other points are assigned to the cluster of the most similar exemplar.
Example: Suppose, after convergence, points A and B are identified as exemplars. Points C, D, and E will then be assigned to either A or B, based on their similarity.
The algorithm stops when the responsibility and availability matrices no longer change significantly, indicating that the clustering solution has stabilized.
How it works? Once the algorithm converges, each data point belongs to one of the clusters formed by the exemplars, and the final clustering solution is achieved.
Example: In this case, points A and B are final exemplars, and the dataset is successfully clustered into two groups based on their similarities to A and B.
Final Output: After the iterative process of updating the responsibility and availability matrices, Affinity Propagation clusters the data points into groups, each centered around one or more exemplars.
These exemplars represent the most central and influential points of each cluster. Here's what the final output looks like based on the previous example:
Exemplars: A and B are identified as the final exemplars.
Cluster Assignments:
These groups form the final clusters, where each cluster has a representative exemplar that best defines the group, and the other data points are assigned based on their similarity to the exemplars.
This output demonstrates the effectiveness of Affinity Propagation in automatically identifying the optimal number of clusters and their corresponding exemplars. It doesn’t require any user input for the number of clusters, making it ideal for exploratory data analysis where the structure of the data is unknown.
Also Read: Hierarchical Clustering in Python [Concepts and Analysis]
Now that you know how exactly Affinity Propagation Clustering works, let’s look at how you can implement it using Python.
Python is an excellent choice for implementing Affinity Propagation clustering due to its simplicity, the powerful scikit-learn library, and seamless integration with tools like numpy and matplotlib for data manipulation and visualization. These features allow for efficient implementation, easy experimentation, and effective analysis, making Python ideal for both beginners and experts in machine learning.
Let’s go through how you can implement affinity clustering it in Python:
1. Install the Necessary Libraries
Before starting, make sure you have the following libraries installed:
pip install scikit-learn numpy matplotlib
2. Import the Libraries
You need to import the necessary modules in your Python script:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AffinityPropagation
from sklearn.metrics import pairwise_distances
You can get a better understanding of Python libraries with upGrad’s Learn Python Libraries: NumPy, Matplotlib & Pandas. Learn how to manipulate data using NumPy, visualize insights with Matplotlib, and analyze datasets with Pandas.
Also Read: Python Modules: Explore 20+ Essential Modules and Best Practices
3. Prepare Your Dataset
For this example, let's create a simple 2D dataset.
# Example: 2D dataset (you can replace this with your own data)
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
If you have a dataset with more complex or higher-dimensional data, you can directly load it using pandas or other data loading methods.
4. Fit the Affinity Propagation Model
Now, we can initialize the AffinityPropagation model and fit it to our data:
# Initialize the Affinity Propagation model
affinity_propagation = AffinityPropagation()
# Fit the model to the data
affinity_propagation.fit(X)
# Get cluster centers (exemplars) and labels
cluster_centers_indices = affinity_propagation.cluster_centers_indices_
labels = affinity_propagation.labels_
5. Visualize the Clusters
To visualize the clusters, let's use matplotlib to plot the data points and the cluster centers (exemplars):
# Plot data points
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o', edgecolors='k')
# Mark the exemplars (cluster centers)
plt.scatter(X[cluster_centers_indices, 0], X[cluster_centers_indices, 1], c='red', marker='x', label='Exemplars')
# Show the plot
plt.title('Affinity Propagation Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.show()
This code will display a scatter plot of your data points, with different colors representing different clusters. The red 'X' marks indicate the exemplars, which are the center of each cluster.
Here is the output of the Affinity Propagation Clustering visualization. The plot shows the data points in two different clusters, with the exemplars marked in red.
Also Read: Matplotlib in Python: Explained Various Plots with Examples
6. Tips for Running Affinity Propagation Effectively
Choice of Similarity Measure: Affinity Propagation requires a similarity matrix. By default, scikit-learn uses the negative squared Euclidean distance as the similarity measure. If your data requires a different similarity measure, you can provide a custom one using the affinity parameter in AffinityPropagation().
# Using 'precomputed' affinity if you already have a similarity matrix
similarity_matrix = pairwise_distances(X, metric='euclidean')
affinity_propagation = AffinityPropagation(affinity='precomputed')
affinity_propagation.fit(similarity_matrix)
Preference Parameter: The preference parameter determines how likely points are to become exemplars. Setting a high value for preference may result in fewer clusters (more points chosen as exemplars), while lower values lead to more clusters. Experiment with this parameter for optimal results.
affinity_propagation = AffinityPropagation(preference=-50)
Convergence: If you’re working with large datasets, Affinity Propagation can take a long time to converge. You can speed up the process by adjusting the damping parameter (default is 0.5). A lower damping value will make the algorithm converge faster, but too low a value may reduce stability
affinity_propagation = AffinityPropagation(damping=0.9)
7. Handling Large Datasets
Affinity Propagation can be computationally expensive for large datasets, as it requires the computation of a full similarity matrix, which can be memory-intensive. If you have a very large dataset, consider:
This step-by-step guide provides an easy way to implement Affinity Propagation clustering in Python, leveraging scikit-learn’s built-in functions.
Feel free to experiment with different datasets and fine-tune the algorithm to get the best results for your specific problem!
Also Read: Top 10 Dimensionality Reduction Techniques for Machine Learning(ML) in 2025
Now that you have a better understanding of how to implement affinity clustering in Python, let’s look at some of its advantages and drawbacks.
Affinity Propagation excels in scenarios where the number of clusters is not known beforehand. It automatically identifies the optimal number of clusters based on the data’s inherent structure.
This makes it particularly useful for exploratory data analysis and complex datasets where traditional methods like k-means might require trial and error. It shines in applications such as image segmentation, gene expression analysis, and customer segmentation.
However, it falls short when dealing with large datasets, as the algorithm requires a similarity matrix that grows quadratically with the number of data points, leading to high computational costs and memory requirements.
Here are some of its key benefits and limitations:
Benefits |
Limitations |
Affinity Propagation automatically determines the number of clusters based on the data’s inherent structure, eliminating the need for the user to specify it in advance. | The algorithm can be computationally expensive, especially when working with large datasets. The similarity matrix grows quadratically, leading to increased memory and processing requirements. |
Since it uses a similarity matrix, it can handle complex data distributions and relationships that may be difficult for other clustering algorithms to capture. | The need to compute and store the similarity matrix for all pairs of data points makes the algorithm memory-intensive. This is a bottleneck for large datasets. |
It works well with both sparse and dense datasets and can handle outliers and noise effectively by adjusting the preference parameter. | The iterative message-passing process can slow down, particularly in large datasets, resulting in longer training times. |
Unlike algorithms like k-means, which may struggle with clusters of varying sizes and densities, Affinity Propagation can naturally form clusters of varying shapes and sizes. | The performance of the algorithm is highly sensitive to the choice of the preference parameter, which controls how likely points are to become exemplars. |
To make the most out of Affinity Propagation, here are some best practices:
By following these best practices, you can ensure more effective use of Affinity Propagation and obtain meaningful insights from your data.
Also Read: What is Cluster Analysis in Data Mining? Methods, Benefits, and More
Next, let’s look at some of the real-life applications of affinity clustering across different industries.
Affinity Propagation stands out for its ability to automatically determine the number of clusters based on the data's inherent structure, without the need for the user to specify it in advance. This flexibility makes it particularly valuable in industries dealing with complex, high-dimensional, or dynamic datasets, where the optimal number of clusters is not obvious or may change over time.
These industries benefit from Affinity Propagation’s ability to reveal the true underlying structure of data, providing more accurate, data-driven insights.
Below are five real-life examples where Affinity Propagation has been successfully applied.
1. Image Segmentation in Computer Vision
In fields like medical imaging or satellite imagery, it is crucial to segment images into meaningful regions for detailed analysis. Traditional methods often require users to specify the number of segments, which may not be obvious or consistent.
Outcome: This allows for more accurate image segmentation, which is essential in applications like detecting tumors in medical scans or differentiating landforms in satellite imagery. The method’s ability to handle complex and large images without predefining clusters makes it especially valuable in these fields.
Also Read: Image Segmentation Techniques [Step By Step Implementation]
2. Recommendation Systems in E-Commerce
E-commerce platforms aim to recommend products to users based on their preferences, but defining the number of product categories or clusters manually can be difficult and inefficient.
Outcome: By automatically determining clusters, Affinity Propagation helps generate highly personalized recommendations. This improves user experience, increases conversion rates, and boosts customer satisfaction. The flexibility in clustering enables platforms to adapt to evolving customer preferences.
Also Read: Simple Guide to Build Recommendation System Machine Learning
3. Customer Segmentation in Marketing
Marketers need to segment their customer base to tailor campaigns effectively. Predefining clusters can be limiting, as customer behaviors and preferences evolve over time.
Outcome: The method’s ability to automatically identify meaningful customer segments helps marketers craft more targeted and effective campaigns, leading to higher engagement rates, better resource allocation, and improved ROI. Its flexibility makes it ideal for fast-paced industries where customer preferences can shift rapidly.
4. Anomaly Detection in Cybersecurity
Detecting anomalies in network traffic, such as potential cyberattacks, is challenging. Traditional methods require setting predefined thresholds, but the patterns of attack can vary greatly.
Outcome: The ability to automatically adapt and identify abnormal behavior in real-time without predefined limits helps enhance the security of IT systems. The flexibility of Affinity Propagation makes it especially useful in dynamic environments like cybersecurity, where attack patterns constantly evolve.
If you need a better understanding of cybersecurity, upGrad’s free Fundamentals of Cybersecurity course can help you. You will learn key concepts, current challenges, and important terminology to protect systems and data.
Also Read: Anomaly Detection With Machine Learning: What You Need To Know?
5. Genomics and Bioinformatics
In genomics, grouping genes based on their expression patterns or sequence similarity is a complex task, especially when the number of gene groups is unknown.
Outcome: This application leads to more accurate and insightful gene clustering, enabling researchers to identify genes associated with specific diseases or biological processes. The flexibility to discover the optimal number of clusters based on data patterns is invaluable for researchers in genomics and bioinformatics.
This flexibility, combined with its robust performance in varied real-world applications, makes it a popular choice for tackling clustering problems in numerous fields.
Also Read: Explanatory Guide to Clustering in Data Mining - Definition, Applications & Algorithms
To solidify your understanding of the Affinity Propagation Clustering in machine learning, test your knowledge with a quiz. It’ll help reinforce the concepts discussed throughout the tutorial and ensure you're ready to apply them in your projects.
Assess your understanding of Affinity Propagation, its components, advantages, limitations, and best practices by answering the following multiple-choice questions. Test your knowledge now!
1. What is the key feature of Affinity Propagation clustering?
a) It requires the number of clusters to be specified in advance
b) It automatically determines the number of clusters based on the data
c) It only works for numerical data
d) It cannot handle complex datasets
2. What mechanism does Affinity Propagation use to identify exemplars (representative data points) in a dataset?
a) It randomly selects exemplars from the dataset
b) It uses a similarity matrix to find points that are most representative of each cluster
c) It assigns exemplars based on the highest distance between points
d) It selects points that have the highest value in the similarity matrix
3. Which of the following is an advantage of using Affinity Propagation?
a) It can handle large datasets with low computational cost
b) It does not require a predefined number of clusters
c) It always produces the same number of clusters, regardless of the data
d) It is faster than traditional clustering methods like k-means
4. What is a limitation of Affinity Propagation?
a) It cannot handle continuous data
b) It is computationally expensive, especially for large datasets
c) It cannot handle datasets with more than 100 data points
d) It only works for datasets with linear relationships
5. Which matrix is crucial in Affinity Propagation for determining how well points fit as exemplars?
a) Responsibility matrix
b) Distance matrix
c) Similarity matrix
d) Preference matrix
6. What does the ‘preference’ parameter in Affinity Propagation control?
a) The maximum number of clusters formed
b) The likelihood of a point being selected as an exemplar
c) The number of iterations the algorithm runs
d) The maximum similarity value between points
7. In which scenario would Affinity Propagation be most beneficial?
a) When the number of clusters is known in advance
b) When the data has a fixed, uniform distribution
c) When the number of clusters is unknown and needs to be determined from the data
d) When dealing with low-dimensional datasets only
8. How does Affinity Propagation handle data points that do not fit well into any cluster?
a) It discards points that do not fit well
b) It assigns them to the nearest exemplar, even if the similarity is low
c) It automatically removes outliers from the dataset
d) It assigns them a zero value in the similarity matrix
9. Which of the following is a best practice when implementing Affinity Propagation clustering?
a) Always set the preference parameter to the average similarity value
b) Tune the preference parameter to control the number of clusters
c) Use Affinity Propagation only for small datasets
d) Ignore the similarity matrix and rely on pre-existing labels
10. What type of data is Affinity Propagation particularly useful for?
a) Datasets with a known number of clusters
b) Datasets with high dimensionality and unknown cluster structure
c) Datasets where all points are equally important
d) Datasets where clusters are linearly separable
This quiz will help you evaluate your understanding of the Affinity Propagation clustering method, its strengths, limitations, and how it operates in various applications.
Also Read: 5 Breakthrough Applications of Machine Learning
You can also continue expanding your skills in machine learning with upGrad, which will help you deepen your understanding of advanced ML concepts and real-world applications.
upGrad’s courses provide expert training in machine learning, with a focus on anonymous classes, their practical applications, and best practices. Learn how to optimize your machine learning models for different scenarios.
While the course covered in the tutorial can significantly improve your knowledge, here are some free courses to facilitate your continued learning:
You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today!
Similar Reads:
Expand your expertise with the best resources available. Browse the programs below to find your ideal fit in Best Machine Learning and AI Courses Online.
Discover in-demand Machine Learning skills to expand your expertise. Explore the programs below to find the perfect fit for your goals.
Discover popular AI and ML blogs and free courses to deepen your expertise. Explore the programs below to find your perfect fit.
298 articles published
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources