Clustergram
clustergram.
Clustergram class mimicking the interface of clustering class (e.g. KMeans).
Clustergram is a graph used to examine how cluster members are assigned to clusters as the number of clusters increases. This graph is useful in exploratory analysis for nonhierarchical clustering algorithms such as k-means and for hierarchical cluster algorithms when the number of observations is large enough to make dendrograms impractical.
Clustergram offers two backends for the computation - scikit-learn which uses CPU and RAPIDS.AI cuML, which uses GPU. Note that both are optional dependencies, but you will need at least one of them to generate clustergram.
scikit-learn
cuML
iterable of integer values to be tested as k.
Whether to use sklearn’s implementation of KMeans and PCA or cuML version. Sklearn does computation on CPU, cuML on GPU.
Clustering method. kmeans uses KMeans clustering, ‘gmm’ Gaussian Mixture Model. ‘gmm’ is currently supported only with ‘sklearn’ backend.
kmeans
Whether use PCA weighted mean of clusters or standard mean of clusters.
Additional arguments passed to the PCA object, e.g. svd_solver. Applies only if pca_weighted=True.
svd_solver
pca_weighted=True
Print progress and time of individual steps.
Additional arguments passed to the KMeans object, e.g. random_state.
random_state
References
The clustergram: A graph for visualizing hierarchical and nonhierarchical cluster analyses: https://journals.sagepub.com/doi/10.1177/1536867X0200200405
Tal Galili’s R implementation: https://www.r-statistics.com/2010/06/clustergram-visualization-and-diagnostics-for-cluster-analysis-r-code/
Examples
>>> c_gram = clustergram.Clustergram(range(1, 9)) >>> c_gram.fit(data) >>> c_gram.plot()
Specifying parameters:
>>> c_gram2 = clustergram.Clustergram( ... range(1, 9), backend="cuML", pca_weighted=False, random_state=0 ... ) >>> c_gram2.fit(cudf_data) >>> c_gram2.plot(figsize=(12, 12))
DataFrame with (weighted) means of clusters.
Methods
fit(data, **kwargs)
fit
Compute (weighted) means of clusters.
plot([ax, size, linewidth, cluster_style, …])
plot
Generate clustergram plot based on cluster centre mean values.
Input data to be clustered. It is expected that data are scaled. Can be numpy.array, pandas.DataFrame or their RAPIDS counterparts.
Additional arguments passed to the KMeans.fit(), e.g. sample_weight.
sample_weight
Fitted clustergram.
matplotlib axis on which to draw the plot
multiplier of the size of a cluster centre indication. Size is determined as 500 / count of observations in a cluster multiplied by size.
500 / count
size
multiplier of the linewidth of a branch. Line width is determined as 50 / count of observations in a branch multiplied by linewidth.
50 / count
Style options to be passed on to the cluster centre plot, such as color, linewidth, edgecolor or alpha.
color
linewidth
edgecolor
alpha
Style options to be passed on to branches, such as color, linewidth, edgecolor or alpha.
Size of the resulting matplotlib.figure.Figure. If the argument axes is given explicitly, figsize is ignored.
iterable of integer values to be plotted. In none, Clustergram.k_range will be used. Has to be a substet of Clustergram.k_range.
Clustergram.k_range