The Wayback Machine - https://web.archive.org/web/20250428205133/https://www.geeksforgeeks.org/affinity-propagation/
Open In App

Affinity Propagation

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Affinity Propagation is a clustering algorithm that is commonly used in Machine Learning and data analysis. Unlike other traditional clustering algorithms which require specifying the number of clusters before implementation. It automatically discovers cluster centers and assigns data points to these clusters. In this article we will explore Affinity Propagation, its parameters and its implementation.

Getting Started with Affinity Propagation

Affinity Propagation is based on the concept of "message-passing" between data points to identify cluster centers called exemplars. Each exemplar is a typical data point that represents other points within the same cluster. The algorithm aims to find the most representative exemplars and use them to cluster the data into compatible groups. This makes it particularly useful when the number of clusters is unknown or when the data exhibits complex, non-spherical or non-convex cluster shapes

Think of each data point in your dataset as a person. These people (data points) need to form groups but instead of directly deciding number of groups they communicate with each other to figure it out.

  • Exemplars: These are the "leaders" of the groups. Each group will have one representative data point (the exemplar) and other data points will be assigned to that group based on similarity.
  • Message-passing: Each person (data point) sends messages to others to decide who should be the leader (exemplar) of their group. There are two types of messages that we will discuss in its working.

These messages are passed back and forth and over time the data points agree on who the best leaders (exemplars) are. Once the leaders are chosen everyone else joins the group with most suitable leader (centroid).

Key Steps of Affinity Propagation

Affinity Propagation clustering algorithm has several step which are discussed below

  1. Similarity Computation: This algorithm first calculates a similarity (or dissimilarity) matrix which quantifies the similarity between pairs of data points. The choice of similarity metric depends on the data and the problem what we're working on. There are some common metrics like Euclidean distance, negative squared Euclidean distance etc.
  2. Responsibility Update: Then this algorithm initializes the responsibility matrix (R), where R(i, k) represents the responsibility of data point i to be the exemplar for data point k. We have already discussed about it previously in this article. Here data points compete to be exemplars based on their similarity and the responsibility which they receive from other data points.
  3. Availability Update: Then it initializes the availability matrix (A), where A(i, k) represents the availability of data point k to choose data point i as its exemplar. This step is also already discussed in this article. Here data points consider the availability of other data points to be their exemplars. If a data point has high availability from other data points then it is more likely to be chosen as an exemplar.
  4. Update Responsibility and Availability: Then it updates the responsibility and availability matrices iteratively until convergence which is reached when the matrices no longer change significantly between iterations.
  5. Net Responsibility Calculation: Then it calculates the net responsibility for each data point by summing its responsibility and availability.
  6. Exemplar Selection: Then a crucial task is to identify exemplars as data points with high net responsibility. These exemplars represent the cluster centers.
  7. Cluster Assignment: Finally it assigns each data point to the nearest exemplar (based on similarity) to form clusters.

For mathematical understanding of Affinity Propagation please refer this article: Affinity Propagation in ML

Working Principals of Affinity Propagation

The algorithm works by treating each data point as a potential exemplar. Through message-passing between data points the algorithm iteratively updates the responsibility and availability matrices until a set of high-quality exemplars is identified.

  • Responsibility Messages are sent from data point i to candidate exemplar k and indicate how well-suited point k is to serve as the exemplar for point i.
  • Availability Messages are sent from candidate exemplar k to data point i and indicate how appropriate it is for point i to choose point k as its exemplar.

These messages are passed back and forth until a set of exemplars is selected, and clusters are formed around them.

Benefits of using Affinity Propagation

Using Affinity Propagation for clustering has several benefits which are listed below:

  1. Automatic Cluster Center Detection: It automatically identifies cluster centers and total number of clusters which makes it suitable for datasets with an unknown or variable number of clusters and useful for large complex real-world datasets.
  2. Robust to Noise and Outliers: It is relatively robust to noise and outliers in the data which makes it useful in various applications like image segmentation, document clustering and biological dataset analysis etc.
  3. Handles Non-Spherical Clusters: Unlike some traditional clustering algorithms it can handle clusters of various shapes and sizes.
  4. Scalability: It can be applied to large datasets although it may require optimization for computational efficiency.

Step-by-step implementation

1. Importing required libraries

At first, we will import all required Python libraries like NumPy, Matplotlib, Seaborn, Pandas and SKlearn.

Python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import seaborn as sns

2. Dataset loading and pre-processing

Now we load the dataset for clustering. After that we will use to Standard Scaler to prepare the dataset for Affinity propagation.

Python
data = pd.read_csv('Mall_Customers.csv').dropna().drop('CustomerID', axis=1)

features = ['Annual Income (k$)', 'Spending Score (1-100)']
X = data[features]

scaler = StandardScaler()
X_std = scaler.fit_transform(X)

3. Exploratory Data Analysis

Exploratory Data Analysis(EDA) helps us to gain deeper insights about the dataset which is very crucial in clustering algorithm implementation.

  • Correlation matrix: Visualizing correlation matrix will help us to understand how the features are correlated to each other. From here we can understand that only which features can be chosen for clustering.
Python
# EDA 1: Correlation Matrix
correlation_matrix = data.corr(numeric_only=True)
plt.figure(figsize=(7, 7))
sns.heatmap(correlation_matrix, annot=True, cmap="coolwarm", fmt=".2f")
plt.title("Correlation Matrix")
plt.show()

Output:


Correlation Matrix-Geeksforgeeks
Correlation Matrix


So, from it we can see that we can only select these three features. And as you can see we have only selected two features in our code of Data loading subsection.

4. Pairwise Feature Scatter Plots
It provides a grid of scatter plots and histograms that visualize relationships between pairs of features in our dataset which is a valuable tool for understanding the distribution and interaction of variables as in a single plot we can visualize distribution of features, pairwise relationships between features and density estimation.

Python
sns.pairplot(data, palette="Set1", hue="Gender", diag_kind="kde", height=2)
plt.suptitle("Pairwise Feature Scatter Plots", y=1.02)
plt.show()

Output:

aff-(1)
Pair-wise feature distribution plot

5. Affinity Propagation Clustering and performance evaluation

Now we will use Affinity Propagation by specifying required parameters of it which are discussed below:

  1. Preference: The preference parameter plays an important role and represents the input that indicates how likely a data point is to be chosen as an exemplar. It influences the number of clusters that will be formed and the data points that will be assigned as exemplars
  2. max_iter: This parameter determines the maximum number of iterations (passes over the data).
  3. convergence_iter: This parameter controls the number of iterations with no change in the cluster assignments which must occur for the algorithm to consider the solution as converged.
  4. random_state: This parameter handles the randomness which ensures the initialization of cluster centers and the order of processing data points remains same between different runs.
  5. damping: It controls the convergence speed of the algorithm which it set to 0.9(close to 1) means that algorithm converges more slowly.
Python
Preference = [-50, -40, -30,-20, -10]
silhouette_scores = []
for preference in Preference:
    model = AffinityPropagation(preference=preference, random_state=42)
    model.fit(X_std)
    
    silhouette_scores.append(metrics.silhouette_score(X_std , model.labels_))
silhouette_scores

# Create line plot of `Preference` vs `silhouette_scores`
plt.plot(Preference , silhouette_scores )
plt.grid()
plt.show()

Output:

Preference vs silhouette_scores-Geeksforgeeks
Preference vs silhouette_scores

Here, -30, resulting in higher value of preference parameter is the optimal preference.

6. Apply Affinity Propagation Clustering

Python
af = AffinityPropagation(preference= -30, max_iter=50, damping=0.7, random_state=42, convergence_iter=20).fit(X_std)

cluster_labels = af.labels_
cluster_labels

Output:

array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 1, 2, 2,
0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 3, 4, 3, 4, 3, 4, 3, 4,
3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
3, 4])

7. After that, we will evaluate its performance using Silhouette Score.

Python
silhouette_score = metrics.silhouette_score(X_std, cluster_labels)
print(f"Silhouette Score: {silhouette_score}")

Output:

Silhouette Score: 0.5529643053885619

Silhouette Score of 0.5529 represents a reasonably good degree of separation between the clusters. The Silhouette score is >0 which depicts that the algorithm has created clusters well and no overlap encountered.

8. Visualizing Clusters

We will create a plot where clustering effect can be visualized.

Python
pca = PCA(n_components=2)
X_r = pca.fit_transform(X_std)

unique_clusters = np.unique(cluster_labels)
colors = plt.cm.tab20(unique_clusters)
lw = 2

for cluster_id, color in zip(unique_clusters, colors):
    plt.scatter(X_r[cluster_labels == cluster_id, 0], X_r[cluster_labels == cluster_id, 1],
                color=color, alpha=0.8, lw=lw, label=f'Cluster {cluster_id}')

plt.legend(loc='best', shadow=False, scatterpoints=1, title='Clusters')
plt.title('Affinity Propagation Clustering of Mall Customers')
plt.xlabel('PCA Dimension 1')
plt.ylabel('PCA Dimension 2')

plt.show()

Output:

Number of clusters-Affinity Propogation
Effect of Affinity Propagation

This shows that the algorithm performs well in various domains but parameter tuning like adjusting the preference value is crucial for optimal clustering results.


Next Article

Similar Reads