FirstHistogramGap(freq_threshold=0, max_fraction=None, n_bins_start=5, affinity='euclidean', memory=None, linkage='single')¶
Agglomerative clustering with stopping rule given by a histogram-based version of the first gap method, introduced in 1.
Given a frequency threshold f and an initial integer k: 1) create a histogram of k equally spaced bins of the number of merges in the dendrogram, as a function of the linkage parameter; 2) the value of linkage at which the tree is to be cut is the first one after which a bin of height no greater than f (i.e. a “gap”) is observed; 3) if no gap is observed, increase k and repeat 1) and 2) until termination. The algorithm can be partially overridden to ensure that the final number of clusters does not exceed a certain threshold, by passing a parameter max_fraction.
freq_threshold (int, optional, default:
0) – The frequency threshold for declaring that a gap in the histogram of merges is present.
max_fraction (float or None, optional, default:
None) – When not
None, the algorithm is constrained to produce no more than
max_fraction * (n_samples - 1)clusters, even if a candidate gap is observed in the iterative process which would produce a greater number of clusters.
n_bins_start (int, optional, default:
5) – The initial number of bins in the iterative process for finding a gap in the histogram of merges.
affinity (str, optional, default:
'euclidean') – Metric used to compute the linkage. Can be
'precomputed'. If linkage is
'euclidean'is accepted. If
'precomputed', a distance matrix (instead of a similarity matrix) is needed as input for
memory (None, str or object with the joblib.Memory interface, optional, default:
None) – Used to cache the output of the computation of the tree. By default, no caching is done. If a string is given, it is the path to the caching directory.
'single', optional, default:
Which linkage criterion to use. The linkage criterion determines which distance to use between sets of observation. The algorithm will merge the pairs of cluster that minimize this criterion.
'ward'minimizes the variance of the clusters being merged.
'average'uses the average of the distances of each observation of the two sets.
'complete'linkage uses the maximum distances between all observations of the two sets.
'single'uses the minimum of the distances between all observations of the two sets.
The number of clusters found by the algorithm.
Cluster labels for each sample.
ndarray of shape (n_samples,)
The children of each non-leaf node. Values less than
n_samplescorrespond to leaves of the tree which are the original samples. A node
igreater than or equal to
n_samplesis a non-leaf node and has children
children_[i - n_samples]. Alternatively at the
children[i]are merged to form node
n_samples + i.
ndarray of shape (n_nodes - 1, 2)
Number of leaves in the hierarchical tree.
Distances between nodes in the corresponding place in
ndarray of shape (n_nodes - 1,)
G. Singh, F. Mémoli, and G. Carlsson, “Topological methods for the analysis of high dimensional data sets and 3D object recognition”; in SPBG, pp. 91–100, 2007.
__init__(freq_threshold=0, max_fraction=None, n_bins_start=5, affinity='euclidean', memory=None, linkage='single')¶
Initialize self. See help(type(self)) for accurate signature.
Fit the agglomerative clustering from features or distance matrix.
The stopping rule is used to determine
n_clusters_, and the full dendrogram is cut there to compute
X (ndarray of shape (n_samples, n_features) or (n_samples, n_samples)) – Training instances to cluster, or distances between instances if
y (ignored) – Not used, present here for API consistency by convention.
- Return type
Perform clustering on X and returns cluster labels.
X (ndarray, shape (n_samples, n_features)) – Input data.
y (Ignored) – Not used, present for API consistency by convention.
labels – Cluster labels.
- Return type
ndarray, shape (n_samples,)
Get parameters for this estimator.
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
params – Parameter names mapped to their values.
- Return type
mapping of string to any
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>so that it’s possible to update each component of a nested object.
**params (dict) – Estimator parameters.
self – Estimator instance.
- Return type