<jupyter_start><jupyter_text>Clustering methods```{note}You can try this notebook in you browser: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/martinfleis/clustergram/HEAD?filepath=doc%2Fnotebooks%2Fmethods.ipynb)``` Scikit-learn, SciPy and RAPIDS cuML backendsClustergram offers three backends for the computation - `scikit-learn` and `scipy` which use CPU and RAPIDS.AI `cuML`, which uses GPU. Note that all are optional dependencies but you will need at least one of them to generate clustergram.Let's load the data on Palmer penguins dataset. See the [Introduction](introduction) for its overview.<jupyter_code>import seaborn
from sklearn.preprocessing import scale
from clustergram import Clustergram

df = seaborn.load_dataset('penguins')
data = scale(df.drop(columns=['species', 'island', 'sex']).dropna())

seaborn.set(style='whitegrid')<jupyter_output><empty_output><jupyter_text>Using `scikit-learn` (default):<jupyter_code>cgram = Clustergram(range(1, 8), backend="sklearn")
cgram.fit(data)<jupyter_output>K=1 fitted in 0.03873419761657715 seconds.
K=2 fitted in 0.019382953643798828 seconds.
K=3 fitted in 0.029963016510009766 seconds.
K=4 fitted in 0.03318214416503906 seconds.
K=5 fitted in 0.028235912322998047 seconds.
K=6 fitted in 0.03303194046020508 seconds.
K=7 fitted in 0.03239035606384277 seconds.<jupyter_text>Using `cuML`:<jupyter_code>cgram = Clustergram(range(1, 8), backend='cuML')
cgram.fit(data)<jupyter_output><empty_output><jupyter_text>`data` can be all data types supported by the selected backend (including `cudf.DataFrame` or `cupy.ndarray` with `cuML` backend).  Supported methodsClustergram currently supports K-Means, Mini Batch K-Means, Gaussian Mixture Model and SciPy's hierarchical clustering methods. Note tha GMM and Mini Batch K-Means are supported only for `scikit-learn` backend and hierarchical methods are supported only for `scipy` backend.Using K-Means (default):<jupyter_code>cgram = Clustergram(range(1, 8), method='kmeans')
cgram.fit(data)
cgram.plot()<jupyter_output>K=1 fitted in 0.037338972091674805 seconds.
K=2 fitted in 0.01883101463317871 seconds.
K=3 fitted in 0.03318500518798828 seconds.
K=4 fitted in 0.038063764572143555 seconds.
K=5 fitted in 0.031418800354003906 seconds.
K=6 fitted in 0.03259706497192383 seconds.
K=7 fitted in 0.03434491157531738 seconds.<jupyter_text>Using Mini Batch K-Means, which can provide significant speedup over K-Means:<jupyter_code>cgram = Clustergram(range(1, 8), method='minibatchkmeans', batch_size=100)
cgram.fit(data)
cgram.plot()<jupyter_output>K=1 fitted in 0.013917922973632812 seconds.
K=2 fitted in 0.011487007141113281 seconds.
K=3 fitted in 0.01315617561340332 seconds.
K=4 fitted in 0.013792037963867188 seconds.
K=5 fitted in 0.01885986328125 seconds.
K=6 fitted in 0.019958019256591797 seconds.
K=7 fitted in 0.01650094985961914 seconds.<jupyter_text>Using Gaussian Mixture Model:<jupyter_code>cgram = Clustergram(range(1, 8), method='gmm')
cgram.fit(data)
cgram.plot()<jupyter_output>K=1 fitted in 0.009224891662597656 seconds.
K=2 fitted in 0.00626683235168457 seconds.
K=3 fitted in 0.012464761734008789 seconds.
K=4 fitted in 0.008269071578979492 seconds.
K=5 fitted in 0.009671926498413086 seconds.
K=6 fitted in 0.015596151351928711 seconds.
K=7 fitted in 0.02029728889465332 seconds.<jupyter_text>Using Ward's hierarchical clustering:<jupyter_code>cgram = Clustergram(range(1, 8), method='hierarchical', linkage='ward')
cgram.fit(data)
cgram.plot()<jupyter_output><empty_output><jupyter_text>Manual inputAlternatively, you can create clustergram using `from_data` or  `from_centers` methods based on alternative clustering algorithms.Using `Clustergram.from_data` which creates cluster centers as mean or median values:<jupyter_code>import numpy
import pandas

data = numpy.array([[-1, -1, 0, 10], [1, 1, 10, 2], [0, 0, 20, 4]])
labels = pandas.DataFrame({1: [0, 0, 0], 2: [0, 0, 1], 3: [0, 2, 1]})

cgram = Clustergram.from_data(data, labels)
cgram.plot()<jupyter_output><empty_output><jupyter_text>Using `Clustergram.from_centers` based on explicit cluster centers.:<jupyter_code>labels = pandas.DataFrame({1: [0, 0, 0], 2: [0, 0, 1], 3: [0, 2, 1]})
centers = {
            1: numpy.array([[0, 0, 0]]),
            2: numpy.array([[-1, -1, -1], [1, 1, 1]]),
            3: numpy.array([[-1, -1, -1], [1, 1, 1], [0, 0, 0]]),
        }
cgram = Clustergram.from_centers(centers, labels)
cgram.plot(pca_weighted=False)<jupyter_output><empty_output><jupyter_text>To support PCA weighted plots with clustergram created from centers you also need to pass data:<jupyter_code>labels = pandas.DataFrame({1: [0, 0, 0], 2: [0, 0, 1], 3: [0, 2, 1]})
centers = {
    1: numpy.array([[0, 0]]),
    2: numpy.array([[-1, -1], [1, 1]]),
    3: numpy.array([[-1, -1], [1, 1], [0, 0]]),
}
data = numpy.array([[-1, -1], [1, 1], [0, 0]])
cgram = Clustergram.from_centers(centers, labels, data=data)
cgram.plot(pca_weighted=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13123622520536968, 'fraction_numerical': 0.13123622520536968, 'mean_word_length': 6.070821529745042, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '598444', 'license': 'MIT', 'n_tokens_mistral': 2253, 'n_tokens_neox': 1835, 'n_words': 480, 'path': 'doc/notebooks/methods.ipynb', 'repo_name': 'martinfleis/clustergram', 'size': '4991'}