clustering
affinity propagation
machine learning
algorithm initialization
data science

Affinity Propagation preferences initialization

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Affinity Propagation, the preference value controls how willing each data point is to become an exemplar, which in turn strongly affects the number of clusters produced. Higher preferences usually lead to more exemplars and more clusters, while lower preferences tend to reduce them. Initialization therefore is not a cosmetic detail. It is one of the main tuning levers in the algorithm.

What the Preference Represents

Affinity Propagation clusters data by exchanging responsibility and availability messages between samples. The diagonal of the similarity matrix holds the preference values. Conceptually, that diagonal says how suitable each sample is to serve as a cluster center.

If every sample gets the same preference, you are telling the algorithm to start from a uniform prior belief about exemplar candidacy.

Common Default: Median Similarity

A widely used default is the median of the pairwise similarities. In scikit-learn, when preference=None, the estimator uses the median of the input similarities.

That default is popular because it often produces a moderate number of clusters without requiring much tuning.

python
1import numpy as np
2from sklearn.cluster import AffinityPropagation
3from sklearn.datasets import make_blobs
4
5X, _ = make_blobs(n_samples=100, centers=3, random_state=42)
6
7model = AffinityPropagation(random_state=42)
8model.fit(X)
9
10print(model.cluster_centers_indices_)
11print(model.labels_[:10])

This is a reasonable first pass when you do not yet know what cluster count range makes sense.

Raise or Lower the Preference Deliberately

If the default produces too many or too few clusters, move the preference value up or down and re-evaluate.

python
1import numpy as np
2from sklearn.cluster import AffinityPropagation
3from sklearn.datasets import make_blobs
4from sklearn.metrics import pairwise_distances
5
6X, _ = make_blobs(n_samples=100, centers=3, random_state=42)
7
8similarities = -pairwise_distances(X, metric="sqeuclidean")
9median_pref = np.median(similarities)
10
11for scale in [0.5, 1.0, 1.5]:
12    pref = median_pref * scale
13    model = AffinityPropagation(preference=pref, random_state=42)
14    model.fit(X)
15    print(scale, len(model.cluster_centers_indices_))

Because similarities are often negative distances, the numerical direction can feel counterintuitive. The practical rule is simpler: experiment around the default and watch how the number of exemplars changes.

Uniform vs Sample-Specific Preferences

Many examples use one scalar preference for all samples. That is the standard starting point.

You can also provide different preference values per sample if domain knowledge suggests some points are more plausible exemplars than others. That is more advanced and only worth doing when you have a strong reason to bias the clustering.

For most applications, uniform initialization is easier to reason about and easier to reproduce.

Damping and Convergence Still Matter

Preference is not the only sensitive parameter. damping affects numerical stability during message passing. If the algorithm oscillates or produces unstable results, changing preference alone may not solve the issue.

That means tuning usually happens in this order:

  • choose a sensible similarity measure
  • start with median preference
  • adjust preference to influence cluster count
  • tune damping if convergence is unstable

Common Pitfalls

  • Treating preference as a minor setting when it directly affects the number of clusters.
  • Assuming there is one universally correct preference value for every dataset.
  • Changing preference without checking whether the similarity measure itself makes sense.
  • Forgetting that negative-distance similarities can make preference values look unintuitive.
  • Overfitting the cluster count to a desired number without checking cluster quality.

Summary

  • Preference controls how likely samples are to become exemplars in Affinity Propagation.
  • The median similarity is a common and effective default initialization.
  • Higher preferences usually produce more clusters; lower preferences usually produce fewer.
  • Uniform preference is the normal starting point, while sample-specific preferences are an advanced option.
  • Tune preference together with similarity choice and damping, not in isolation.

Course illustration
Course illustration

All Rights Reserved.