fuzzyclust.extensions.KernelFCM#
- class fuzzyclust.extensions.KernelFCM(n_clusters=3, m=2.0, kernel='rbf', gamma=None, degree=3, coef0=1.0, prototypes='feature', tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererKernel fuzzy c-means: FCM in the feature space of a Mercer kernel.
FCM measures distances with the Euclidean norm of the input space and therefore finds clusters that are convex and roughly spherical. Kernel methods map the data with a (usually unknown) nonlinear map \(\Phi\) into a high-dimensional feature space \(\mathcal{H}\) where inner products are given by a kernel, \(K(\mathbf{x},\mathbf{y})=\langle\Phi(\mathbf{x}),\Phi(\mathbf{y})\rangle\), and run a linear algorithm there. Kernel FCM runs FCM in \(\mathcal{H}\): the objective and the membership formula are those of FCM, only the distance is the feature-space distance \(\|\Phi(\mathbf{x}_k)-\mathbf{w}_i\|^2\), which is evaluated with the kernel trick without ever computing \(\Phi\). Spheres in \(\mathcal{H}\) are curved regions in the input space, so rings, moons and other nonconvex groups become separable. Two variants are implemented, following the comparative study of Graves and Pedrycz [2] (who call them KFCM-K and KFCM-F, with “feature space” meaning the input space in their terminology):
prototypes="feature"(default) – the prototypes \(\mathbf{w}_i\) live in \(\mathcal{H}\) as weighted means of the mapped samples, \(\mathbf{w}_i=\sum_k w_{ik}\Phi(\mathbf{x}_k)\) with \(w_{ik}=\mu_{ik}^m/\sum_l\mu_{il}^m\), and all distances are kernel evaluations. Any Mercer kernel can be used; the clusters can be nonconvex in the input space, at the price of \(O(N^2)\) memory for the kernel matrix and of prototypes that have no exact pre-image.prototypes="input"– the KFCM of Zhang and Chen [1]: the prototypes \(\mathbf{v}_i\) stay in the input space and the distance is \(\|\Phi(\mathbf{x}_k)-\Phi(\mathbf{v}_i)\|^2 =2(1-K(\mathbf{x}_k,\mathbf{v}_i))\) for the Gaussian kernel. The distance saturates for far-away samples, so outliers lose their leverage on the prototypes: this variant is a robust FCM, but, because the distance is a monotone function of \(\|\mathbf{x}_k-\mathbf{v}_i\|\), its hard partition boundaries are the same bisecting hyperplanes as those of FCM.
- Parameters:
- n_clusters
int, default=3 Number of clusters \(c\).
- m
float, default=2.0 Fuzziness exponent \(m>1\).
- kernel{“rbf”, “poly”, “linear”, “sigmoid”, “cosine”, “laplacian”}, default=”rbf”
Kernel function, evaluated with
sklearn.metrics.pairwise.pairwise_kernels(). Withprototypes="input"only"rbf"is allowed, because the prototype update of Zhang and Chen is specific to it.- gamma
floatorNone, default=None Kernel coefficient of the
"rbf"(\(\exp(-\gamma\|\mathbf{x}-\mathbf{y}\|^2)\)),"laplacian","poly"and"sigmoid"kernels.Noneuses \(1/(n\,\mathrm{Var}(\mathbf{X}))\) (the"scale"rule of scikit-learn’s SVMs). Large \(\gamma\) gives very local kernels.- degree
int, default=3 Degree of the
"poly"kernel \((\gamma\,\mathbf{x}^T\mathbf{y}+c_0)^d\).- coef0
float, default=1.0 Constant \(c_0\) of the
"poly"and"sigmoid"kernels.- prototypes{“feature”, “input”}, default=”feature”
Where the prototypes live, see above.
- tol
float, default=1e-4 - max_iter
int, default=300 - init{“random”, “kmeans++”}
orndarray, default=”random” Initialization in the input space (as in the base class); the initial partition is derived from Euclidean distances.
- n_init
int, default=1 Number of random restarts; the run with the lowest objective is kept. Kernel FCM with a local kernel has many local optima, so restarts matter more than for FCM.
- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_features) Prototypes in the input space. For
prototypes="feature"these are the membership-weighted means \(\sum_k w_{ik}\mathbf{x}_k\) of the samples, i.e. only an approximate pre-image of the feature-space prototypes, provided for plotting.- membership_
ndarrayofshape(n_samples,n_clusters) - labels_
ndarrayofshape(n_samples,) - gamma_
float Kernel coefficient used.
- distances_, objective_, objective_history_, n_iter_
See
BaseFuzzyClusterer;distances_are feature-space distances.
- cluster_centers_
Notes
Both variants minimize \(J=\sum_i\sum_k\mu_{ik}^m D^2_{ik}\) with the FCM membership formula. For the feature-space prototypes, expanding \(\|\Phi(\mathbf{x}_k)-\mathbf{w}_i\|^2\) gives
\[D^2_{ik} = K(\mathbf{x}_k,\mathbf{x}_k) - 2\sum_l w_{il}K(\mathbf{x}_k,\mathbf{x}_l) + \sum_{l,l'} w_{il}w_{il'}K(\mathbf{x}_l,\mathbf{x}_{l'}),\qquad w_{il} = \frac{\mu_{il}^m}{\sum_{l'}\mu_{il'}^m},\]the fuzzy counterpart of kernel k-means [3]; the same formula with \(K(\mathbf{x},\mathbf{x}_l)\) for a new sample \(\mathbf{x}\) gives its memberships in
predict_proba(). For the input-space prototypes and the Gaussian kernel\[D^2_{ik} = 2\left(1-K(\mathbf{x}_k,\mathbf{v}_i)\right),\qquad \mathbf{v}_i = \frac{\sum_k\mu_{ik}^m K(\mathbf{x}_k,\mathbf{v}_i)\,\mathbf{x}_k} {\sum_k\mu_{ik}^m K(\mathbf{x}_k,\mathbf{v}_i)},\]a fixed-point update in which every sample is additionally weighted by its kernel similarity to the current prototype (far samples get weight \(\approx 0\)). The kernel width \(\gamma\) is the decisive parameter: \(\gamma\to0\) recovers FCM (the RBF kernel becomes linear in the squared distance), a very large \(\gamma\) makes all samples mutually orthogonal in \(\mathcal{H}\) and the memberships uninformative.
References
[1]D.-Q. Zhang, S.-C. Chen, “Clustering incomplete data using kernel-based fuzzy c-means algorithm”, Neural Processing Letters 18, 155–162, 2003.
[2]D. Graves, W. Pedrycz, “Kernel-based fuzzy clustering and fuzzy clustering: a comparative experimental study”, Fuzzy Sets and Systems 161(4), 522–543, 2010.
[3]M. Girolami, “Mercer kernel-based clustering in feature space”, IEEE Trans. Neural Networks 13(3), 780–784, 2002.
Examples
>>> from fuzzyclust.extensions import KernelFCM >>> from fuzzyclust.datasets import make_rings >>> X, y = make_rings(random_state=0) >>> kfcm = KernelFCM(n_clusters=2, gamma=2.0, n_init=3, random_state=0).fit(X) >>> kfcm.membership_.shape (300, 2)
Examples using fuzzyclust.extensions.KernelFCM#
Kernel fuzzy c-means: rings and moons through the kernel trick