Custom *metric* parameter for matrix in unsupervised clustering

Viewed 36

Multiple unsupervised algorithms in scikit-learn have two parameters: affinity and metric, which can take a precomputed matrix for affinity and distance matrix respectively.

AffinityPropagation, AgglomerativeClustering, FeatureAgglomeration, and SpectralClustering take affinity for precomputed matrix where as DBSCAN and OPTICS take metric for precomputed matrix.

I have a two part question:

  1. Is it possible to write a custom function that can be used for all of above mentioned six methods?
  2. If not, then I want to have a custom function that will be 'callable' for metric parameter in DBSCAN and OPTICS (both methods use distance matrix).

From the documentation, I understand that

it (metric) must be one of the options allowed by sklearn.metrics.pairwise_distances for its metric parameter.

So is it possible to define a custom function (in DBSCAN and OPTICS) and overwrite one of the preexisting sklearn.metrics.pairwise_distances function or I have to use precomputed in parameters?

1 Answers

Your first question:
You should not use the same function for both affinity and metric, since they mean two different things: the larger the affinity, the nearer the two points, while the larger the metric, the farther away the two points.

Your second question:
The parameter metric of both DBSCAN and OPTICS can be a custom function, taking two vectors and returning their difference. From the documentation of sklearn.metrics.pairwise_distances

Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded.

So, yes, you can provide a custom function for the parameter metric of DBSCAN and OPTICS.

Related