fuzzyclust.relational.RelationalFCM#
- class fuzzyclust.relational.RelationalFCM(n_clusters=3, metric='euclidean', m=2.0, tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererRelational fuzzy c-means (Hathaway, Davenport and Bezdek, 1989).
Fuzzy c-means needs feature vectors to compute prototypes and distances. When only the pairwise dissimilarities \(\mathbf{R}=[r_{jk}]\) of the \(N\) objects are available (a distance matrix, a dynamic-time-warping matrix, a dissimilarity between graphs or documents), the relational dual of FCM still minimizes the same functional. Its prototypes are weight vectors over the objects, \(\mathbf{v}_i\in\mathbb{R}^N\), and the squared distance of object \(k\) to prototype \(i\) is expressed through \(\mathbf{R}\) alone. If \(\mathbf{R}\) consists of squared Euclidean distances of some feature vectors, RFCM produces exactly the FCM partition of those vectors (the duality theorem of the paper); if not, the relational distances may become negative and the algorithm is undefined – then use
NERFCM.- Parameters:
- n_clusters
int, default=3 Number of clusters \(c\).
- metric
str, default=”euclidean” How
fit(X)obtains \(\mathbf{R}\)."precomputed":Xis the \(N\times N\) dissimilarity matrix (symmetric, zero diagonal, non-negative), whose entries play the role of squared distances \(r_{jk}=d^2(\mathbf{x}_j,\mathbf{x}_k)\). Any other value is a metric name ofscipy.spatial.distance.pdist(); the pairwise distances of the rows ofXare computed and squared ("euclidean"therefore reproduces FCM).- 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"draws a random partition \(\mathbf{U}\) (the paper starts from an arbitrary partition);"kmeans++"seeds \(c\) objects by \(D^2\)-weighted sampling on \(\mathbf{R}\) and derives \(\mathbf{U}\) from the dissimilarities to them; an array of shape(n_samples, n_clusters)is an initial partition, an integer array of lengthn_clustersgives initial objects.- n_init
int, default=1 Number of random restarts; the run with the lowest \(J\) is kept.
- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- relational_centers_
ndarrayofshape(n_clusters,n_samples) Relational prototypes \(\mathbf{v}_i=\mathbf{U}_i^m/\sum_k\mu_{ik}^m\) (rows sum to one): the weight of every training object in cluster \(i\).
- cluster_centers_
ndarrayofshape(n_clusters,n_features)or(n_clusters,n_samples) With a feature-based
metricthe dual centers in feature space, \(\sum_k v_{ik}\mathbf{x}_k\) (equal to the FCM centers for the Euclidean metric); withmetric="precomputed"the same asrelational_centers_, since no feature space exists.- dissimilarity_matrix_
ndarrayofshape(n_samples,n_samples) The matrix \(\mathbf{R}\) the algorithm ran on (squared distances when computed from features).
- membership_, labels_, distances_, objective_, objective_history_, n_iter_
See
BaseFuzzyClusterer;distances_is \(\sqrt{d^2_{ik}}\) of the relational distances.- beta_
float Always
0.0for RFCM (kept for a uniform interface withNERFCM).
- relational_centers_
Notes
RFCM minimizes the c-means functional \(J=\sum_i\sum_k\mu_{ik}^m d^2_{ik}\) by alternating
\[\mathbf{v}_i=\frac{(\mu_{i1}^m,\dots,\mu_{iN}^m)\T}{\sum_{k=1}^{N}\mu_{ik}^m},\qquad d^2_{ik}=(\mathbf{R}\mathbf{v}_i)_k-\tfrac12\,\mathbf{v}_i\T\mathbf{R}\mathbf{v}_i ,\]and the FCM membership update \(\mu_{ik}=1/\sum_j(d^2_{ik}/d^2_{jk})^{1/(m-1)}\). When \(r_{jk}=\norm{\mathbf{x}_j-\mathbf{x}_k}^2\), the identity \(\norm{\mathbf{x}_k-\sum_l v_{il}\mathbf{x}_l}^2=\sum_l v_{il}r_{kl} -\tfrac12\sum_{l,q}v_{il}v_{iq}r_{lq}\) (valid for any weights summing to one) makes \(d^2_{ik}\) the squared Euclidean distance to the FCM center, so RFCM and FCM generate identical iterates from identical starts. The identity holds for any object whose squared distances to the \(N\) training objects are known, which is how
predict_proba()assigns new objects: pass their(n_new, n_samples)dissimilarities (withmetric="precomputed") or their feature vectors.A matrix that is not Euclidean – squared city-block or Chebyshev distances, squared dynamic-time-warping distances, dissimilarities that violate the triangle inequality – can produce \(d^2_{ik}<0\); RFCM then raises an error and recommends
NERFCM. (Unsquared Euclidean distances, or any power \(\norm{\cdot}^{p}\) with \(p\le2\), still form a Euclidean matrix by Schoenberg’s theorem: RFCM runs on them, but it is then the dual of FCM on a different point configuration, not on \(\mathbf{X}\).) Memory and time are \(O(N^2)\).References
[1]R. J. Hathaway, J. W. Davenport, J. C. Bezdek, “Relational duals of the c-means clustering algorithms”, Pattern Recognition 22(2), 205–212, 1989.
[2]J. C. Bezdek, “Pattern Recognition with Fuzzy Objective Function Algorithms”, Plenum Press, 1981.
Examples
>>> import numpy as np >>> from fuzzyclust.cluster import FCM >>> from fuzzyclust.datasets import make_fuzzy_blobs >>> from fuzzyclust.relational import RelationalFCM, pairwise_dissimilarity >>> X, _ = make_fuzzy_blobs(random_state=0) >>> R = pairwise_dissimilarity(X, metric="sqeuclidean") >>> U0 = np.random.RandomState(0).rand(X.shape[0], 3) >>> rfcm = RelationalFCM(n_clusters=3, metric="precomputed", init=U0).fit(R) >>> fcm = FCM(n_clusters=3, init=U0).fit(X) >>> bool(np.allclose(rfcm.membership_, fcm.membership_, atol=1e-6)) True
- fit(X, y=None)[source]#
Cluster the objects.
- Parameters:
- Xarray_like
ofshape(n_samples,n_samples)or(n_samples,n_features) The dissimilarity matrix \(\mathbf{R}\) when
metric="precomputed", otherwise the feature vectors from which \(\mathbf{R}\) is computed withmetric.- y
Ignored
- Xarray_like
- Returns:
- self
object
- self
- predict_proba(X)[source]#
Memberships of (new) objects in the fitted clusters.
- Parameters:
- Xarray_like
ofshape(n_new,n_samples)or(n_new,n_features) With
metric="precomputed"the dissimilarities of the new objects to then_samplestraining objects (same kind of entries as the training matrix); otherwise feature vectors.
- Xarray_like
- Returns:
- U
ndarrayofshape(n_new,n_clusters)
- U
Examples using fuzzyclust.relational.RelationalFCM#
Relational fuzzy c-means and NERF for non-Euclidean data
Gower’s dissimilarity versus one-hot encoding for mixed data
Autocorrelation-based fuzzy clustering of time series