Gower's Distance #34240
|
Is there implementation for Gower's distance yet? I was taking a look and saw AgglomerativeClustering. Which obviously doesn't look like it. Or is it? |
Replies: 4 comments
|
I do not think scikit-learn has a built-in Gower distance implementation at the moment. AgglomerativeClustering is a clustering algorithm, not a distance metric. It can use different metrics, and in some cases it can work with a precomputed distance matrix, but it does not compute Gower distance for you. So the usual workaround is:
For example, conceptually: D = gower_distance_matrix(X)
clustering = AgglomerativeClustering(
metric="precomputed",
linkage="average",
).fit(D)You would need to be careful with linkage choice. Some linkage methods have restrictions on which metrics/precomputed distances they support, while average/complete are usually the more natural choices for arbitrary dissimilarity matrices. So the short answer is: no, AgglomerativeClustering is not Gower distance. It can consume a Gower distance matrix if you compute one yourself, but scikit-learn does not appear to provide the Gower metric directly. |
|
Hi! Great question β Gower's distance is not natively implemented in scikit-learn as a built-in metric, but there are excellent ways to use it in your workflows. Current Status in scikit-learnscikit-learn's distance metrics (in What is Gower's Distance?Gower's distance (Gower, 1971) is specifically designed for mixed-type data (numerical + categorical + binary features). For each feature pair, it computes:
The final distance is a weighted average across all features. How to Use It with scikit-learnOption 1:
|
| Approach | Pros | Cons |
|---|---|---|
gower package + precomputed |
Fast, battle-tested, one-liner | Extra dependency |
| Manual implementation | No dependencies | Slower for large datasets |
sklearn-extra KMedoids |
Robust clustering | Requires scikit-learn-extra |
The gower package with metric='precomputed' is the standard approach used by the community. Hope this helps!
|
Hi! Great question β Gower's distance is not natively implemented in scikit-learn as a built-in metric, but there are excellent ways to use it in your workflows. Current Status in scikit-learnscikit-learn's distance metrics (in What is Gower's Distance?Gower's distance (Gower, 1971) is specifically designed for mixed-type data (numerical + categorical + binary features). For each feature pair, it computes:
The final distance is a weighted average across all features. How to Use It with scikit-learnOption 1:
|
| Approach | Pros | Cons |
|---|---|---|
gower package + precomputed |
Fast, battle-tested, one-liner | Extra dependency |
| Manual implementation | No dependencies | Slower for large datasets |
sklearn-extra KMedoids |
Robust clustering | Requires scikit-learn-extra |
The gower package with metric='precomputed' is the standard approach used by the community. Hope this helps!
|
scikit-learn does not currently provide Gower distance as a built-in metric, and AgglomerativeClustering is the clustering algorithm rather than the distance implementation. A common pattern is to compute the mixed-type distance matrix separately and pass it as precomputed: D = gower.gower_matrix(X) The linkage matters: Ward requires Euclidean features, so it cannot be used with a precomputed Gower matrix. Also validate how the chosen Gower implementation handles missing values and feature weights for your dataset. |
Hi! Great question β Gower's distance is not natively implemented in scikit-learn as a built-in metric, but there are excellent ways to use it in your workflows.
Current Status in scikit-learn
scikit-learn's distance metrics (in
sklearn.metrics.pairwise) and clustering algorithms likeAgglomerativeClusteringdo not include Gower's distance out of the box.AgglomerativeClusteringsupports Euclidean, Manhattan, cosine, and precomputed distance matrices β but not Gower directly.What is Gower's Distance?
Gower's distance (Gower, 1971) is specifically designed for mixed-type data (numerical + categorical + binary features). For each feature pair, it computes: