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:
BaseFuzzyClustererFuzzy 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_clusters
int, default=3 Number of clusters \(c\).
- gamma
floatorNone, default=None Weight \(\gamma\) of the categorical mismatches against the squared numeric distances.
Noneuses the average standard deviation of the (training) numeric columns, the guidance of Huang’s paper; larger values let the categorical attributes dominate the partition,0ignores them.- categorical_features
None, “all”, boolmaskorlistofcolumns, default=None Which columns are categorical.
Nonedetects them from the dtypes of apandas.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.- m
float, default=2.0 Fuzziness exponent \(m>1\).
- tol
float, default=1e-4 - max_iter
int, default=300 - init{“random”, “kmeans++”}
orndarray, 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 lengthn_clustersgives the initial records, an array of shape(n_samples, n_clusters)an initial partition.- n_init
int, default=1 Random restarts; the run with the lowest objective is kept.
- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(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.DataFrameorndarrayofshape(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.DataFrameorndarrayofshape(n_clusters,n_categorical) The categorical part of the prototypes with original labels.
- numeric_centers_
ndarrayofshape(n_clusters,n_numeric) - categorical_features_
ndarrayofint Positions of the categorical columns;
numeric_features_the rest.- gamma_
float The weight actually used.
- categories_
listofndarray 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.
- cluster_centers_
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:
- X
pandas.DataFrameor array_likeofshape(n_samples,n_features) Records; categorical columns may hold strings, categories, booleans or numbers (see
categorical_features).- y
Ignored
- X
- Returns:
- self
object
- self
Examples using fuzzyclust.relational.FuzzyKPrototypes#
Fuzzy k-prototypes on mixed numeric and categorical data
Gower’s dissimilarity versus one-hot encoding for mixed data