fuzzyclust.graph.MSTClustering#
- class fuzzyclust.graph.MSTClustering(n_clusters=None, threshold=None, inconsistency=2.5, inconsistency_std=None, n_hops=2, criterion='hypervolume', min_gain=0.15, min_cluster_size=5, max_clusters=20, refine='gg', m=2.0, gamma=0.0, tol=0.0001, max_iter=100, backend='scipy', random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererHybrid clustering with a minimal spanning tree and Gath–Geva models.
A divisive graph-theoretic clusterer that combines the minimal spanning tree (MST) of the data with a fuzzy hypervolume splitting criterion and a final Gath–Geva (GG) refinement, following Vathy-Fogarassy, Feil and Abonyi (2005). The procedure has two phases (plus the refinement):
Classical MST cutting (Step 0 of the paper). Edges are declared inconsistent and removed when they are longer than
thresholdtimes the mean edge length of the tree (Zahn’s global criterion, Criterion 1) or longer thaninconsistencytimes the mean length – or more thaninconsistency_stdstandard deviations above it – of the edges withinn_hopstree steps of their end points (Zahn’s local criterion, Criterion 2). The connected components of the remaining forest are the coarse clusters: well separated groups and groups of clearly different density are found here, but a chain of points between two groups keeps them together, and a curved group stays one cluster.Volume-based binary splitting (Steps 1–3). The sub-cluster with the largest fuzzy hypervolume \(V_i=\sqrt{\det\mathbf{F}_i}\) is split by cutting every edge of its sub-tree in turn; each cut yields a candidate partition whose total hypervolume \(FHV=\sum_j V_j\) is evaluated, and the cut with the smallest \(FHV\) is kept if it lowers the volume of the split cluster by at least the relative gain
min_gain(Criterion 3). This repeats depth-first until no cluster can be improved (or untiln_clusters/max_clustersis reached). Elongated Gaussian groups are not split (halving them increases the total volume), whereas chains and curved groups are, so that every final cluster is well approximated by one ellipsoid.Refinement (Step 4). The crisp tree partition initializes a fuzzy clusterer (
refine="gg"by default) with as many clusters as there are tree clusters. GG is very sensitive to its start and fails from an FCM start on such data; the tree partition is an excellent start and GG only polishes it, delivering memberships, covariance matrices and a parametric mixture-of-Gaussians description.
- Parameters:
- n_clusters
intorNone, default=None Number of clusters.
Nonelets the volume criterion decide. If given, the coarse phase may be undone edge by edge (shortest cut first) when it produces too many clusters, and the splitting phase continues (best available cut) until the requested number is reached.- threshold
floatorNone, default=None Criterion 1: edges longer than
thresholdtimes the mean MST edge length are cut.Nonedisables the criterion.- inconsistency
floatorNone, default=2.5 Criterion 2, ratio form: an edge is cut when it is at least
inconsistencytimes longer than the mean of the other edges withinn_hopssteps of its end points (the factor \(f\) of Zahn and of the paper).Nonedisables this form.- inconsistency_std
floatorNone, default=None Criterion 2, deviation form (Zahn): an edge is cut when it exceeds the mean of the nearby edges by more than
inconsistency_stdstandard deviations of their lengths. Either form flags an edge;Nonedisables this form.- n_hops
int, default=2 Neighborhood depth (in tree steps) of Criterion 2. A deeper neighborhood averages over more edges and tolerates sparse spots inside a cluster; a shallow one reacts to every local gap.
- criterion{“hypervolume”, “fukuyama_sugeno”}, default=”hypervolume”
Validity measure of the splitting phase: the total fuzzy hypervolume of Gath and Geva, or the (crisp) Fukuyama–Sugeno measure used by Varma and Simon, both evaluated on the crisp tree partition.
- min_gain
float, default=0.15 Minimum relative decrease of the volume criterion, \((V_i - V_a - V_b)/V_i\), for a split of cluster \(i\) into \(a\) and \(b\) to be accepted when
n_clusters=None. Cutting a few tail points off a compact cluster always lowers the volume a little; the threshold separates such cosmetic cuts from real structure (chains, bends, merged groups).- min_cluster_size
int, default=5 Cuts that would create a component with fewer samples are not applied (in either phase). Keeps single outliers from becoming clusters.
- max_clusters
int, default=20 Upper bound on the number of clusters when
n_clusters=None: coarse cuts beyond it are undone (shortest first) and the splitting phase stops at it.- refine{“gg”, “fcm”}
orNone, default=”gg” Fuzzy clusterer initialized with the tree partition.
Nonekeeps the crisp partition; memberships are then FCM-type memberships to the cluster means.- m
float, default=2.0 Fuzziness exponent of the refinement.
- gamma
float, default=0.0 Covariance regularization of the GG refinement, see
GG.- tol
float, default=1e-4 - max_iter
int, default=100 Termination of the refinement.
- backend{“scipy”, “networkx”}, default=”scipy”
MST backend, see
minimum_spanning_tree().- random_state
int,RandomStateinstanceorNone, default=None Only used to break ties in the refinement (the tree is deterministic).
- verbose
int, default=0
- n_clusters
- Attributes:
- n_clusters_
int Number of clusters found.
- labels_
ndarrayofshape(n_samples,) Final labels (
argmaxofmembership_).- tree_labels_
ndarrayofshape(n_samples,) Crisp labels of the tree partition before refinement.
- membership_
ndarrayofshape(n_samples,n_clusters_) - cluster_centers_
ndarrayofshape(n_clusters_,n_features) - covariances_
ndarrayofshape(n_clusters_,n_features,n_features) Fuzzy covariance matrices of the refined clusters (GG) or of the crisp tree clusters.
- mst_edges_
ndarrayofshape(n_samples- 1, 2) Edges of the minimal spanning tree (sorted by length).
- mst_weights_
ndarrayofshape(n_samples- 1,) - cut_edges_
ndarrayofshape(n_cuts, 2) End points of the removed edges.
- cut_phases_
ndarrayofshape(n_cuts,) 0for cuts of the threshold/inconsistency phase,1for volume-based splits.- cut_quality_
ndarrayofshape(n_cuts,) Adequacy \(CP(e_{ab})=\sum_{k\in\{a,b\}}|\mu_{ik}-\mu_{jk}|\) of every cut in the final partition, where \(i,j\) are the clusters of the end points \(a,b\) (eq. 9 of the paper): values near \(2\) mean that the refinement confirmed the separation, values near \(0\) that the end points were re-united.
- hypervolumes_
ndarrayofshape(n_clusters_,) Fuzzy hypervolume of every tree cluster.
- cluster_similarity_
ndarrayofshape(n_clusters_,n_clusters_) Fuzzy similarity \(S_{ij}\) of the final clusters (
fuzzy_cluster_similarity()), the basis of the paper’s dendrogram of clusters.- refiner_
fittedGGorFCM,orNone - distances_, objective_, objective_history_, n_iter_
Of the refinement (
None/zeros whenrefine=None).
- n_clusters_
Notes
The fuzzy hypervolume of cluster \(j\) is
\[V_j = \sqrt{\det\mathbf{F}_j},\qquad \mathbf{F}_j = \frac{\sum_k\mu_{jk}^m(\mathbf{x}_k-\mathbf{v}_j) (\mathbf{x}_k-\mathbf{v}_j)^T}{\sum_k\mu_{jk}^m}, \qquad FHV=\sum_{j=1}^{c}V_j ,\]evaluated here with the crisp memberships of the tree partition; the volume of the subtree on either side of every edge is obtained from subtree sums of \(\mathbf{x}_k\) and \(\mathbf{x}_k\mathbf{x}_k^T\), so that the statistics of all \(N_i-1\) candidate cuts of a sub-tree cost \(O(N_i n^2)\), plus two \(n\times n\) determinants (one for each side, \(O(n^3)\) each) per cut. The MST itself is built from the dense distance matrix, \(O(N^2)\) memory, which limits the method to a few thousand samples.
References
[1]Á. Vathy-Fogarassy, B. Feil, J. Abonyi, “Minimal spanning tree based fuzzy clustering”, International Journal of Computer and Information Engineering 1(8), 2505–2510, 2007 (re-issue of the 2005 conference paper).
[2]C. T. Zahn, “Graph-theoretical methods for detecting and describing gestalt clusters”, IEEE Trans. Computers C-20(1), 68–86, 1971.
[3]I. Gath, A. B. Geva, “Unsupervised optimal fuzzy clustering”, IEEE Trans. Pattern Anal. Mach. Intell. 11(7), 773–781, 1989.
[4]S. Varma, R. Simon, “Iterative class discovery and feature selection using minimal spanning trees”, BMC Bioinformatics 5, 126, 2004.
Examples
>>> from fuzzyclust.graph import MSTClustering >>> from fuzzyclust.datasets import make_fuzzy_blobs >>> X, y = make_fuzzy_blobs(n_clusters=3, random_state=0) >>> mst = MSTClustering(random_state=0).fit(X) >>> mst.n_clusters_, mst.mst_edges_.shape (3, (299, 2))
- fit(X, y=None)[source]#
Build the tree, cut it, split by volume and refine with GG.
- Parameters:
- Xarray_like
ofshape(n_samples,n_features) - y
Ignored
- Xarray_like
- Returns:
- self
object
- self
Examples using fuzzyclust.graph.MSTClustering#
Minimal spanning tree clustering: cutting, splitting, and Gath–Geva refinement
Inconsistent edges: Zahn’s criteria and the neighborhood depth
Number of clusters: volume-based validity and fuzzy cluster similarity