fuzzyclust.relational.gower_distance_matrix#

fuzzyclust.relational.gower_distance_matrix(X, Y=None, categorical_features=None, ranges=None, feature_weights=None)[source]#

Gower’s general coefficient of dissimilarity for mixed data (Gower, 1971).

Every attribute contributes a score in \([0,1]\): for a numeric attribute the absolute difference scaled by the attribute’s range \(R_j\), for a categorical attribute \(0\) when the two records agree and \(1\) otherwise. The dissimilarity is the weighted mean of the scores over the attributes for which both values are present,

\[\begin{split}d_G(\mathbf{x}_k,\mathbf{x}_l)= \frac{\sum_j w_j\,\delta_{klj}\,s_{klj}}{\sum_j w_j\,\delta_{klj}},\qquad s_{klj}=\begin{cases}|x_{kj}-x_{lj}|/R_j & \text{numeric}\\ [x_{kj}\neq x_{lj}] & \text{categorical,}\end{cases}\end{split}\]

with \(\delta_{klj}=0\) when either value is missing (NaN or None). Gower showed that \(\sqrt{d_G}\) is Euclidean-embeddable when there are no missing values, so \(\mathbf{R}=[d_G]\) (the squared embedded distance) is a valid input of relational c-means.

Parameters:
Xarray_like or pandas.DataFrame of shape (n_samples, n_features)
Yarray_like of shape (n_other, n_features), optional
categorical_featuresNone, “all”, bool mask or list of columns, default=None

Which columns are categorical. None detects them from the dtypes of a DataFrame (non-numeric and boolean columns); for a numeric array all columns are numeric, for an object array all are categorical.

rangesarray_like of shape (n_numeric,), optional

Ranges \(R_j\) of the numeric columns (default: observed range of X, which must be reused for new data to keep the scale).

feature_weightsarray_like of shape (n_features,), optional

Attribute weights \(w_j\) (uniform by default).

Returns:
Dndarray of shape (n_samples, n_samples) or (n_samples, n_other)

Gower dissimilarities in \([0, 1]\).

References

[1]

J. C. Gower, “A general coefficient of similarity and some of its properties”, Biometrics 27(4), 857–871, 1971.

Examples

>>> import pandas as pd
>>> from fuzzyclust.relational import gower_distance_matrix
>>> df = pd.DataFrame({"age": [20.0, 40.0, 60.0], "job": ["a", "a", "b"]})
>>> gower_distance_matrix(df).round(3)
array([[0.  , 0.25, 1.  ],
       [0.25, 0.  , 0.75],
       [1.  , 0.75, 0.  ]])

Examples using fuzzyclust.relational.gower_distance_matrix#

Gower’s dissimilarity versus one-hot encoding for mixed data

Gower's dissimilarity versus one-hot encoding for mixed data