fuzzyclust.relational.FuzzyKPrototypes#

class fuzzyclust.relational.FuzzyKPrototypes(n_clusters=3, gamma=None, categorical_features=None, m=2.0, tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#

Bases: BaseFuzzyClusterer

Fuzzy k-prototypes clustering of mixed numeric and categorical data (Huang, 1998).

Records with numeric and categorical attributes cannot be clustered by fuzzy c-means without encoding the categories as numbers, which imposes an arbitrary geometry on them. The k-prototypes dissimilarity of Huang treats the two kinds of attributes in their own terms – the squared Euclidean distance for the numeric part and the number of mismatching categories for the categorical part – and balances them with a weight \(\gamma\); the prototype of a cluster is the fuzzy mean of the numeric attributes together with the mode (most frequent category, weighted by the memberships) of every categorical attribute. This is the fuzzy version of the algorithm: memberships follow the FCM formula with the exponent \(m\) (Huang and Ng, 1999), and the category of a mode is the one with the largest membership mass.

Parameters:
n_clustersint, default=3

Number of clusters \(c\).

gammafloat or None, default=None

Weight \(\gamma\) of the categorical mismatches against the squared numeric distances. None uses the average standard deviation of the (training) numeric columns, the guidance of Huang’s paper; larger values let the categorical attributes dominate the partition, 0 ignores them.

categorical_featuresNone, “all”, bool mask or list of columns, default=None

Which columns are categorical. None detects them from the dtypes of a pandas.DataFrame (object, string, category and boolean columns) and treats every column of a numeric array as numeric; column names (DataFrame) or integer positions are accepted.

mfloat, default=2.0

Fuzziness exponent \(m>1\).

tolfloat, default=1e-4
max_iterint, default=300
init{“random”, “kmeans++”} or ndarray, default=”random”

"random" picks \(c\) distinct records as initial prototypes (Huang’s start); "kmeans++" seeds them by \(D^2\)-weighted sampling with the k-prototypes dissimilarity; an integer array of length n_clusters gives the initial records, an array of shape (n_samples, n_clusters) an initial partition.

n_initint, default=1

Random restarts; the run with the lowest objective is kept.

random_stateint, RandomState instance or None, default=None
verboseint, default=0
Attributes:
cluster_centers_ndarray of shape (n_clusters, n_features)

Prototypes in the encoded representation: numeric columns hold the fuzzy means, categorical columns the integer code of the mode.

prototypes_pandas.DataFrame or ndarray of shape (n_clusters, n_features)

The prototypes with the original category labels (a DataFrame with the input’s column names when a DataFrame was fitted).

modes_pandas.DataFrame or ndarray of shape (n_clusters, n_categorical)

The categorical part of the prototypes with original labels.

numeric_centers_ndarray of shape (n_clusters, n_numeric)
categorical_features_ndarray of int

Positions of the categorical columns; numeric_features_ the rest.

gamma_float

The weight actually used.

categories_list of ndarray

Categories of every categorical column (from the encoder).

membership_, labels_, distances_, objective_, objective_history_, n_iter_

See BaseFuzzyClusterer; distances_ is the square root of the k-prototypes dissimilarity.

Notes

With the numeric attributes \(j\in\mathcal{N}\) and the categorical ones \(j\in\mathcal{C}\) the dissimilarity of record \(\mathbf{x}_k\) to prototype \(\mathbf{v}_i\) is

\[\begin{split}d(\mathbf{x}_k,\mathbf{v}_i)=\sum_{j\in\mathcal{N}}(x_{kj}-v_{ij})^2 +\gamma\sum_{j\in\mathcal{C}}\delta(x_{kj},v_{ij}),\qquad \delta(a,b)=\begin{cases}0&a=b\\1&a\neq b,\end{cases}\end{split}\]

and the objective \(J=\sum_i\sum_k\mu_{ik}^m\,d(\mathbf{x}_k,\mathbf{v}_i)\). For fixed prototypes the optimal memberships are \(\mu_{ik}=1/\sum_j\big(d(\mathbf{x}_k,\mathbf{v}_i)/d(\mathbf{x}_k,\mathbf{v}_j)\big)^{1/(m-1)}\) (with \(\mu_{ik}=1\) when \(d=0\)); for fixed memberships the numeric part of the prototype is the fuzzy mean \(v_{ij}=\sum_k\mu_{ik}^m x_{kj}/\sum_k\mu_{ik}^m\) and, by the theorem of Huang and Ng, the categorical part is the category with the largest membership mass,

\[v_{ij}=a^{(r)}_j\quad\text{with}\quad \sum_{k:\,x_{kj}=a^{(r)}_j}\mu_{ik}^m\;\ge\;\sum_{k:\,x_{kj}=a^{(t)}_j}\mu_{ik}^m \quad\forall t .\]

Unknown categories of new records (predict_proba) never match a mode and contribute one mismatch.

References

[1]

Z. Huang, “Extensions to the k-means algorithm for clustering large data sets with categorical values”, Data Mining and Knowledge Discovery 2(3), 283–304, 1998.

[2]

Z. Huang, M. K. Ng, “A fuzzy k-modes algorithm for clustering categorical data”, IEEE Transactions on Fuzzy Systems 7(4), 446–452, 1999.

Examples

>>> from fuzzyclust.relational import FuzzyKPrototypes, make_mixed_blobs
>>> data = make_mixed_blobs(random_state=0)
>>> kp = FuzzyKPrototypes(n_clusters=3, random_state=0).fit(data.data)
>>> kp.prototypes_.shape, len(kp.categorical_features_)
((3, 6), 3)
fit(X, y=None)[source]#

Cluster mixed-type records.

Parameters:
Xpandas.DataFrame or array_like of shape (n_samples, n_features)

Records; categorical columns may hold strings, categories, booleans or numbers (see categorical_features).

yIgnored
Returns:
selfobject
predict_proba(X)[source]#

Memberships of (new) records in the fitted clusters.

Returns:
Undarray of shape (n_samples, n_clusters)
score(X, y=None)[source]#

Negative objective on X (higher is better).

transform(X)[source]#

Square root of the k-prototypes dissimilarity of records to the prototypes.

Examples using fuzzyclust.relational.FuzzyKPrototypes#

Fuzzy k-prototypes on mixed numeric and categorical data

Fuzzy k-prototypes on mixed numeric and categorical data

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

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