---
title: "ClusteringComponents"
language: "en"
type: "Symbol"
summary: "ClusteringComponents[array] gives an array in which each element at the lowest level of array is replaced by an integer index representing the cluster in which the element lies. ClusteringComponents[array, n] finds n clusters. ClusteringComponents[array, n, level] finds clusters at the specified level in array. ClusteringComponents[image] finds clusters of pixels with similar values in image. ClusteringComponents[image, n] finds n clusters in image. ClusteringComponents[video, ...] returns a time series of clustering result for all video frames."
keywords: 
- kmeans
- k-means
- PAM
- partitioning around medoids
- partitioning about medoids
- segmentation
- cluster analysis
canonical_url: "https://reference.wolfram.com/language/ref/ClusteringComponents.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Segmentation Analysis"
    link: "https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md"
  - 
    title: "Cluster Analysis"
    link: "https://reference.wolfram.com/language/guide/ClusterAnalysis.en.md"
  - 
    title: "Image Computation for Microscopy"
    link: "https://reference.wolfram.com/language/guide/ImageComputationForMicroscopy.en.md"
  - 
    title: "3D Images"
    link: "https://reference.wolfram.com/language/guide/3DImages.en.md"
  - 
    title: "Scientific Data Analysis"
    link: "https://reference.wolfram.com/language/guide/ScientificDataAnalysis.en.md"
  - 
    title: "Video Analysis"
    link: "https://reference.wolfram.com/language/guide/VideoAnalysis.en.md"
  - 
    title: "Computational Photography"
    link: "https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md"
  - 
    title: "Text Analysis"
    link: "https://reference.wolfram.com/language/guide/TextAnalysis.en.md"
  - 
    title: "Natural Language Processing"
    link: "https://reference.wolfram.com/language/guide/NaturalLanguageProcessing.en.md"
  - 
    title: "Unsupervised Machine Learning"
    link: "https://reference.wolfram.com/language/guide/UnsupervisedMachineLearning.en.md"
  - 
    title: "Tabular Modeling"
    link: "https://reference.wolfram.com/language/guide/TabularModeling.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "ArrayComponents"
    link: "https://reference.wolfram.com/language/ref/ArrayComponents.en.md"
  - 
    title: "FindClusters"
    link: "https://reference.wolfram.com/language/ref/FindClusters.en.md"
  - 
    title: "MorphologicalComponents"
    link: "https://reference.wolfram.com/language/ref/MorphologicalComponents.en.md"
  - 
    title: "WatershedComponents"
    link: "https://reference.wolfram.com/language/ref/WatershedComponents.en.md"
  - 
    title: "ClusteringTree"
    link: "https://reference.wolfram.com/language/ref/ClusteringTree.en.md"
  - 
    title: "ClusteringMeasurements"
    link: "https://reference.wolfram.com/language/ref/ClusteringMeasurements.en.md"
  - 
    title: "ComponentMeasurements"
    link: "https://reference.wolfram.com/language/ref/ComponentMeasurements.en.md"
---
# ClusteringComponents

ClusteringComponents[array] gives an array in which each element at the lowest level of array is replaced by an integer index representing the cluster in which the element lies.

ClusteringComponents[array, n] finds n clusters.

ClusteringComponents[array, n, level] finds clusters at the specified level in array.

ClusteringComponents[image] finds clusters of pixels with similar values in image.

ClusteringComponents[image, n] finds n clusters in image.

ClusteringComponents[video, …] returns a time series of clustering result for all video frames.

## Details and Options

* ``ClusteringComponents`` works for a variety of data types, including numerical, textual, image and video, as well as dates and times.

* The number of clusters can be specified in the following ways:

|           |                                           |
| --------- | ----------------------------------------- |
| Automatic | find the number of clusters automatically |
| n         | find exactly n clusters                   |
| UpTo[n]   | find at most n clusters                   |

* The following options can be given:

|                       |           |                                                                   |
| --------------------- | --------- | ----------------------------------------------------------------- |
| CriterionFunction     | Automatic | criterion for selecting a method                                  |
| DistanceFunction      | Automatic | the distance function to use                                      |
| FeatureExtractor      | Identity  | how to extract features from which to learn                       |
| FeatureNames          | Automatic | feature names to assign for input data                            |
| FeatureTypes          | Automatic | feature types to assume for input data                            |
| Method                | Automatic | what method to use                                                |
| MissingValueSynthesis | Automatic | how to synthesize missing values                                  |
| PerformanceGoal       | Automatic | aspect of performance to optimize                                 |
| RandomSeeding         | 1234      | what seeding of pseudorandom generators should be done internally |
| Weights               | Automatic | what weight to give to each example                               |

* By default, ``ClusteringComponents`` will preprocess the data automatically unless a ``DistanceFunction`` is specified.

* The setting for ``DistanceFunction`` can be any distance or dissimilarity function, or a function ``f`` defining a distance between two values.

* Possible settings for ``PerformanceGoal`` include:

|           |                                                      |
| --------- | ---------------------------------------------------- |
| Automatic | automatic tradeoff among speed, accuracy, and memory |
| "Quality" | maximize the accuracy of the classifier              |
| "Speed"   | maximize the speed of the classifier                 |

* Possible settings for ``Method`` include:

|                           |                                                             |
| ------------------------- | ----------------------------------------------------------- |
| Automatic                 | automatically select a method                               |
| "Agglomerate"             | single linkage clustering algorithm                         |
| "DBSCAN"                  | density-based spatial clustering of applications with noise |
| "GaussianMixture"         | variational Gaussian mixture algorithm                      |
| "JarvisPatrick"           | Jarvis–Patrick clustering algorithm                         |
| "KMeans"                  | *k*-means clustering algorithm                              |
| "KMedoids"                | partitioning around medoids                                 |
| "MeanShift"               | mean-shift clustering algorithm                             |
| "NeighborhoodContraction" | shift data points toward high-density regions               |
| "SpanningTree"            | minimum spanning tree-based clustering algorithm            |
| "Spectral"                | spectral clustering algorithm                               |

* The methods ``"KMeans"`` and ``"KMedoids"`` can only be used when the number of clusters is specified.

* The methods ``"DBSCAN"``, ``"GaussianMixture"``, ``"JarvisPatrick"``, ``"MeanShift"`` and ``"NeighborhoodContraction"`` can only be used when the number of clusters is ``Automatic``.

* The following plots show results of common methods on toy datasets:

```wl
[image]
```

* Possible settings for ``CriterionFunction`` include:

|                     |                                     |
| ------------------- | ----------------------------------- |
| "StandardDeviation" | root-mean-square standard deviation |
| "RSquared"          | R-squared                           |
| "Dunn"              | Dunn index                          |
| "CalinskiHarabasz"  | Calinski–Harabasz index             |
| "DaviesBouldin"     | Davies–Bouldin index                |
| Automatic           | internal index                      |

* Possible settings for ``RandomSeeding`` include:

|           |                                                        |
| --------- | ------------------------------------------------------ |
| Automatic | automatically reseed every time the function is called |
| Inherited | use externally seeded random numbers                   |
| seed      | use an explicit integer or string as a seed            |

---

## Examples (32)

### Basic Examples (3)

Label two clusters of values in a list:

```wl
In[1]:= ClusteringComponents[{1, 2, 3, 7, 8}, 2]

Out[1]= {1, 1, 1, 2, 2}
```

---

Label a vector of strings:

```wl
In[1]:= ClusteringComponents[{"a", "aa", "aab", "aabb", "abbc", "abcc", "abccd"}]

Out[1]= {1, 1, 2, 2, 2, 3, 4}
```

---

Cluster analysis of an MR image:

```wl
In[1]:= ClusteringComponents[[image], 7]//Colorize

Out[1]= [image]
```

### Scope (10)

Clusters of values in a matrix:

```wl
In[1]:=
ClusteringComponents[(⁠|   |   |   |   |   |
| - | - | - | - | - |
| 1 | 2 | 2 | 2 | 1 |
| 1 | 1 | 3 | 5 | 5 |
| 1 | 1 | 5 | 5 | 5 |
| 1 | 1 | 2 | 5 | 6 |
| 1 | 1 | 4 | 5 | 6 |⁠)] // MatrixForm

Out[1]//MatrixForm=
(⁠|   |   |   |   |   |
| - | - | - | - | - |
| 1 | 2 | 2 | 2 | 1 |
| 1 | 1 | 2 | 3 | 3 |
| 1 | 1 | 3 | 3 | 3 |
| 1 | 1 | 2 | 3 | 3 |
| 1 | 1 | 3 | 3 | 3 |⁠)
```

---

Find color clusters in an image:

```wl
In[1]:= ClusteringComponents[[image], 5] // Colorize

Out[1]= [image]
```

---

Find clusters in a 3D image:

```wl
In[1]:= i = ExampleData[{"TestImage3D", "MRknee"}]

Out[1]= [image]

In[2]:=
seg = ClusteringComponents[i, 3];
Image3D[Colorize[seg], ClipPlanes -> {{-1, 1, 0, 12}}]

Out[2]= [image]
```

---

Clustering transform of nested lists:

```wl
In[1]:= ClusteringComponents[{{1, 2}, 3, {10, 11}, {12, {13}}, 14}, 2]

Out[1]= {{1, 1}, 1, {2, 2}, {2, {2}}, 2}
```

---

Find clusters at list level 2:

```wl
In[1]:= ClusteringComponents[{{1, 2}, {3, 4}, {10, 11}, {12, 13}}, 2, 2]

Out[1]= {{1, 1}, {1, 1}, {2, 2}, {2, 2}}
```

---

Find clusters at list level 1:

```wl
In[1]:= ClusteringComponents[{{1, 2}, {3, 4}, {10, 11}, {12, 13}}, 2, 1]

Out[1]= {1, 1, 2, 2}
```

---

Find duplicates by specifying a large number of potential clusters:

```wl
In[1]:= ClusteringComponents[{1, 2, 2, 7, 7, 8}, UpTo[100]]

Out[1]= {4, 3, 3, 2, 2, 1}
```

---

Labeling clusters in a matrix:

```wl
In[1]:=
ClusteringComponents[(⁠|   |   |   |   |
| - | - | - | - |
| 1 | 1 | 7 | 7 |
| 1 | 2 | 6 | 6 |
| 1 | 6 | 5 | 5 |
| 0 | 7 | 5 | 5 |⁠), 2]//Colorize

Out[1]= [image]
```

---

Clustering lists of Booleans:

```wl
In[1]:= ClusteringComponents[{{True, False, True}, {False, False, True}, {False, True, False}}, 2, 1]

Out[1]= {1, 1, 2}
```

---

Clustering a list of Boolean vectors:

```wl
In[1]:=
SeedRandom[1234];
tfdata = RandomChoice[{True, False}, {30, 3}]

Out[1]= {{False, False, False}, {True, True, True}, {True, True, True}, {False, False, True}, {False, True, True}, {False, True, False}, {False, True, False}, {True, False, True}, {True, True, True}, {True, True, False}, {False, False, True}, {True, True,  ...  True}, {False, True, False}, {False, True, True}, {False, False, False}, {False, False, True}, {False, False, False}, {True, True, False}, {True, False, False}, {False, False, False}, {True, True, True}, {True, False, True}, {True, False, False}}

In[2]:= c = ClusteringComponents[tfdata, Automatic, 1]

Out[2]= {1, 2, 2, 3, 4, 5, 5, 6, 2, 7, 3, 2, 2, 6, 2, 4, 6, 6, 2, 5, 4, 1, 3, 1, 7, 8, 1, 2, 6, 8}
```

### Options (13)

#### CriterionFunction (1)

Generate some separated data and visualize it:

```wl
In[1]:=
circle[r_, theta_] := {r Sin[theta], r Cos[theta]};
points  = RandomVariate[MixtureDistribution[{1, 1}, {UniformDistribution[{{3 / 2, 2}, {0,   2 Pi}}], UniformDistribution[{{0, 1 / 2}, {0,   2 Pi}}]}],   1000];
data = circle@@@points;
ListPlot[data, PlotRange -> All]

Out[1]= [image]
```

Find a cluster assignment with exactly two clusters using different settings for ``CriterionFunction`` :

```wl
In[2]:= assignment1 = ClusteringComponents[data, 2, 1];

In[3]:= assignment2 = ClusteringComponents[data, 2, 1, CriterionFunction -> "CalinskiHarabasz"];
```

Compare the two clusterings of the data:

```wl
In[4]:=
cCluster1 = Pick[data, assignment1, 1];
cCluster2 = Pick[data, assignment1, 2];
ListPlot[{cCluster1, cCluster2}]

Out[4]= [image]

In[5]:=
dCluster1 = Pick[data, assignment2, 1];
dCluster2 = Pick[data, assignment2, 2];
ListPlot[{dCluster1, dCluster2}]

Out[5]= [image]
```

#### DistanceFunction (1)

By default, ``EditDistance`` is used to cluster a list of strings:

```wl
In[1]:= sdata = {"GCTAT", "TAGGA", "GAATT", "CATCT", "TCAGG", "GGGGA", "TTACG", "GTCAG", "TGGAG", "GAAAA", "ATAGG", "TCCGA", "TAACT", "GTGAT", "AAGAA", "CCGTA", "GCTAA", "GCTGG", "GAGGG", "CTCAT"};

In[2]:= ClusteringComponents[sdata, 2]

Out[2]= {1, 2, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1}
```

Use ``HammingDistance`` to cluster based on the number of characters that disagree:

```wl
In[3]:= ClusteringComponents[sdata, 2, DistanceFunction -> HammingDistance]

Out[3]= {1, 2, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1}
```

#### FeatureExtractor (1)

Find clustering components for a list of images:

```wl
In[1]:= flowers = {[image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image]};

In[2]:= ClusteringComponents[flowers, 3, 1]

Out[2]= {1, 2, 1, 1, 3, 3, 1, 3, 1, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2}
```

Create a custom ``FeatureExtractor`` to extract features:

```wl
In[3]:= fe = FeatureExtraction[flowers, (DominantColors[#, 2]&)]

Out[3]=
FeatureExtractorFunction[Association["ExampleNumber" -> 19, 
  "Preprocessor" -> MachineLearning`MLProcessor["ToMLDataset", 
    Association["Input" -> Association["f1" -> Association["Type" -> "Image"]], 
     "Output" -> Association["f1" -> Assoc ...     "Date" -> DateObject[{2022, 6, 14, 8, 53, 27.447026`8.19107027550846}, "Instant", "Gregorian", 
      2.], "ProcessorCount" -> 4, "ProcessorType" -> "x86-64", "OperatingSystem" -> "MacOSX", 
    "SystemWordLength" -> 64, "Evaluations" -> {}]]]
```

Look at the resulting features:

```wl
In[4]:= extractedfeatures = fe[flowers]

Out[4]= {{RGBColor[0.7514938880709875, 0.018700447181179487, 0.11595800339283588], RGBColor[0.29169042723361244, 0.37625623698670124, 0.3753636261757429]}, {RGBColor[0.5199027873459928, 0.08132817565833245, 0.07884452028185182], RGBColor[0.7171828173384399 ... 50914522469, 0.28309941596928934], RGBColor[0.19964915539330125, 0.2978026189009533, 0.11083189902103519]}, {RGBColor[0.620642773281255, 0.673520386604496, 0.826554458253343], RGBColor[0.4600641015346651, 0.33194257644353387, 0.0819176493210926]}}
```

Use the ``FeatureExtractor`` to find new clustering components:

```wl
In[5]:= cnew = ClusteringComponents[flowers, 3, 1, FeatureExtractor -> fe]

Out[5]= {1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 3, 2, 3, 3, 2, 2, 3, 3}
```

Look at the new clustering:

```wl
In[6]:= GroupBy[Transpose[{extractedfeatures, flowers, cnew}], Last -> Most]

Out[6]= <|1 -> {{{RGBColor[0.7514938880709875, 0.018700447181179487, 0.11595800339283588], RGBColor[0.29169042723361244, 0.37625623698670124, 0.3753636261757429]}, [image]}, {{RGBColor[0.5199027873459928, 0.08132817565833245, 0.07884452028185182], RGBColor ... 596928934], RGBColor[0.19964915539330125, 0.2978026189009533, 0.11083189902103519]}, [image]}, {{RGBColor[0.620642773281255, 0.673520386604496, 0.826554458253343], RGBColor[0.4600641015346651, 0.33194257644353387, 0.0819176493210926]}, [image]}}|>
```

#### FeatureNames (1)

Use ``FeatureNames`` to name features, and refer to their names in further specifications:

```wl
In[1]:= c = ClusteringComponents[{{2.3, "male"}, {4.8, "male"}, {9, "female"}, {5.2, "female"}, {1, "male"}, {10, "male"}, {5, "female"}}, Automatic, 1, FeatureNames -> {"age", "gender"}, FeatureTypes -> <|"age" -> "Numerical", "gender" -> "Nominal"|>]

Out[1]= {1, 1, 2, 2, 1, 3, 2}
```

#### FeatureTypes (1)

Use ``FeatureTypes`` to enforce the interpretation of the features:

```wl
In[1]:= ClusteringComponents[{{2.3, "male"}, {4.8, "male"}, {9, "female"}, {5.2, "female"}, {1, "male"}, {10, "male"}, {5, "female"}}, Automatic, 1, FeatureTypes -> {"Numerical", "Nominal"}]

Out[1]= {1, 1, 2, 2, 1, 3, 2}
```

Compare it to the result obtained by assuming nominal features:

```wl
In[2]:= ClusteringComponents[{{2.3, "male"}, {4.8, "male"}, {9, "female"}, {5.2, "female"}, {1, "male"}, {10, "male"}, {5, "female"}}, Automatic, 1, FeatureTypes -> {"Nominal", "Nominal"}]

Out[2]= {1, 1, 2, 3, 1, 4, 2}
```

#### Method (5)

Generate normally distributed data and visualize its histogram:

```wl
In[1]:=
Dis = MixtureDistribution[{1, 1, 1, 1}, {
	NormalDistribution[1, 1], 
	NormalDistribution[-5, 1], 
	NormalDistribution[10, 1], NormalDistribution[20, 1]}];
data = RandomVariate[Dis, 500];
Histogram[data, {-7, 25, 0.3}]

Out[1]= [image]
```

Find cluster assignments for this data using the ``"GaussianMixture"`` method:

```wl
In[2]:= assignment = ClusteringComponents[data, Method -> "GaussianMixture"];
```

Visualize the corresponding clustering:

```wl
In[3]:=
clustering = Pick[data, assignment, #]& /@ DeleteDuplicates[assignment];
Histogram[clustering,   {-7, 25, 0.3}]

Out[3]= [image]
```

---

Find cluster assignments for a list of string using the k-medoids method:

```wl
In[1]:= strings = DictionaryLookup["gi" ~~ __];

In[2]:= assignment = ClusteringComponents[strings, 4, Method -> "KMedoids"];
```

Look at the resulting clustering:

```wl
In[3]:= GatherBy[Transpose[{strings, assignment}], Last][[All, All, 1]]

Out[3]= {{"giant", "giantess", "giantesses", "giants", "giblets", "gift", "gifted", "gifting", "gifts", "gigabytes", "gigantic", "gigantically", "gigavolt", "giggles", "giggliest", "gigolos", "gigs", "gillies", "gilt", "gilts", "gimbals", "gimlet", "gimlet ... , "gigabyte", "ginned", "ginning"}, {"giddier", "giddiest", "giddily", "giddiness", "giddy", "giggled", "giggly", "gild", "gilded", "gilders", "gilding", "gilds", "gill", "gills", "girded", "girdled", "girdles", "girds", "girlhoods", "girlishly"}}
```

---

Find color clusters in an image using different methods:

```wl
In[1]:= animals = [image];

In[2]:= Table[Colorize[ClusteringComponents[animals,   Method -> method]], {method, {"GaussianMixture", "NeighborhoodContraction", "MeanShift", "Spectral", "JarvisPatrick"}}]

Out[2]= {[image], [image], [image], [image], [image]}
```

---

Find color clusters in an image using the ``"NeighborhoodContraction"`` method and its suboption:

```wl
In[1]:= house  = [image];

In[2]:= Table[Colorize[ClusteringComponents[house,   Method -> {"NeighborhoodContraction", "NeighborhoodRadius" -> t}]], {t, {0.3, 0.1, 0.05}}]

Out[2]= {[image], [image], [image]}
```

---

Find color clusters in an image using the ``"Spectral"`` method and its suboption:

```wl
In[1]:= flower = [image];

In[2]:= ClusteringComponents[flower,   Method -> "Spectral"]//Colorize

Out[2]= [image]

In[3]:= Multicolumn[Table[Colorize[ClusteringComponents[flower,   Method -> {"Spectral", "NeighborhoodRadius" -> t}]], {t, {0.3, 0.2, 0.1, 0.08}}], 2, Frame -> All]

Out[3]=
|         |         |
| :------ | :------ |
| [image] | [image] |
| [image] | [image] |
```

#### PerformanceGoal (1)

Generate 500 random numerical vectors of length 1000:

```wl
In[1]:= vectors = RandomReal[{10, 11}, {5000, 5}];
```

Compute their clustering and benchmark the operation:

```wl
In[2]:= AbsoluteTiming[ ClusteringComponents[vectors, Automatic, 1];]

Out[2]= {3.82865, Null}
```

Perform the same operation with ``PerformanceGoal`` set to ``"Quality"`` :

```wl
In[3]:= AbsoluteTiming[ClusteringComponents[vectors, Automatic, 1, PerformanceGoal -> "Quality"];]

Out[3]= {11.9943, Null}
```

#### RandomSeeding (1)

Generate 500 random numerical vectors in 2 dimensions:

```wl
In[1]:= vectors = RandomReal[{10, 11}, {500, 2}];
```

Compute their clustering several times and compare the results:

```wl
In[2]:= clusterings = Table[ ClusteringComponents[vectors, Automatic, 1], 5];

In[3]:= SameQ@@clusterings

Out[3]= True
```

Compute their clustering several times by changing the ``RandomSeeding`` option and compare the results:

```wl
In[4]:= randomclusterings = Table[ ClusteringComponents[vectors, Automatic, 1, RandomSeeding -> RandomInteger[10]], 5];

In[5]:= SameQ@@randomclusterings

Out[5]= False
```

#### Weights (1)

Obtain cluster assignment for some numerical data:

```wl
In[1]:= data = {1, 1.1, 1.3, 5.6, 5.7, 5.65, 6.5};

In[2]:= ClusteringComponents[data]

Out[2]= {1, 1, 1, 2, 2, 2, 3}
```

Look at the cluster assignment when changing the weight given to each number:

```wl
In[3]:= ClusteringComponents[data, Weights -> {1, 1, 1, 1, 1, 1, 0.2}]

Out[3]= {1, 1, 1, 2, 2, 2, 2}
```

### Applications (2)

Color segmentation of a microscopic image, after smoothing with a Perona–Malik filter:

```wl
In[1]:= ClusteringComponents[PeronaMalikFilter[[image], 5, 0.05], 6] // Colorize

Out[1]= [image]
```

---

Binary segmentation of an image:

```wl
In[1]:= (segs = ClusteringComponents[[image], 2])//Colorize

Out[1]= [image]

In[2]:= Image[segs - 1, "Bit"]

Out[2]= [image]
```

### Properties & Relations (3)

``ClusteringComponents`` gives an array of cluster indices while ``FindClusters`` returns the list of clusters:

```wl
In[1]:= ClusteringComponents[{10, 4, 5, 6, 11}, 2]

Out[1]= {1, 2, 2, 2, 1}

In[2]:= FindClusters[{10, 4, 5, 6, 11}, 2]

Out[2]= {{10, 11}, {4, 5, 6}}
```

---

Convert the result of ``ClusteringComponents`` to partitions of similar elements:

```wl
In[1]:=
list = {10, 4, 5, 6, 11};
components = ClusteringComponents[list]

Out[1]= {1, 2, 2, 2, 1}

In[2]:= Map[First, GatherBy[Transpose[{list, components}], Last], {2}]

Out[2]= {{10, 11}, {4, 5, 6}}
```

``FindClusters`` yields the same result:

```wl
In[3]:= FindClusters[list]

Out[3]= {{10, 11}, {4, 5, 6}}
```

---

Convert the result of ``FindClusters`` to a list of cluster indices:

```wl
In[1]:=
list = {10, 4, 5, 6, 11};
f = FindClusters[list, 2]

Out[1]= {{10, 11}, {4, 5, 6}}

In[2]:= list /. Flatten[MapIndexed[(#1 -> #2[[1]])&, f, {2}]]

Out[2]= {1, 2, 2, 2, 1}
```

``ClusteringComponents`` yields the same result:

```wl
In[3]:= ClusteringComponents[list]

Out[3]= {1, 2, 2, 2, 1}
```

### Possible Issues (1)

The ``"KMeans"`` method cannot be used when the mean of a subset of the input does not belong to the input space:

```wl
In[1]:= ClusteringComponents[{{False, False, False}, {True, True, False}, {False, False, False}, {True, True, False}, {True, False, False}, {True, False, False}, {True, False, False}}, 2, 1, DistanceFunction -> (Abs[Length[#1] - Length[#2]]&), Method -> "KMeans"]
```

ClusteringComponents::wrgmth: The clustering methods selected cannot be used. Use any among {KMedoids,Agglomerate}.

```wl
Out[1]= ClusteringComponents[{{False, False, False}, {True, True, False}, {False, False, False}, {True, True, False}, {True, False, False}, {True, False, False}, {True, False, False}}, 2, 1, DistanceFunction -> (Abs[Length[#1] - Length[#2]]&), Method -> "KMeans"]
```

## See Also

* [`ArrayComponents`](https://reference.wolfram.com/language/ref/ArrayComponents.en.md)
* [`FindClusters`](https://reference.wolfram.com/language/ref/FindClusters.en.md)
* [`MorphologicalComponents`](https://reference.wolfram.com/language/ref/MorphologicalComponents.en.md)
* [`WatershedComponents`](https://reference.wolfram.com/language/ref/WatershedComponents.en.md)
* [`ClusteringTree`](https://reference.wolfram.com/language/ref/ClusteringTree.en.md)
* [`ClusteringMeasurements`](https://reference.wolfram.com/language/ref/ClusteringMeasurements.en.md)
* [`ComponentMeasurements`](https://reference.wolfram.com/language/ref/ComponentMeasurements.en.md)

## Related Guides

* [Segmentation Analysis](https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md)
* [Cluster Analysis](https://reference.wolfram.com/language/guide/ClusterAnalysis.en.md)
* [Image Computation for Microscopy](https://reference.wolfram.com/language/guide/ImageComputationForMicroscopy.en.md)
* [3D Images](https://reference.wolfram.com/language/guide/3DImages.en.md)
* [Scientific Data Analysis](https://reference.wolfram.com/language/guide/ScientificDataAnalysis.en.md)
* [Video Analysis](https://reference.wolfram.com/language/guide/VideoAnalysis.en.md)
* [Computational Photography](https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md)
* [Text Analysis](https://reference.wolfram.com/language/guide/TextAnalysis.en.md)
* [Natural Language Processing](https://reference.wolfram.com/language/guide/NaturalLanguageProcessing.en.md)
* [Unsupervised Machine Learning](https://reference.wolfram.com/language/guide/UnsupervisedMachineLearning.en.md)
* [Tabular Modeling](https://reference.wolfram.com/language/guide/TabularModeling.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2016 (11.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn110.en.md) ▪ [2017 (11.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn111.en.md) ▪ [2017 (11.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn112.en.md) ▪ [2018 (11.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn113.en.md) ▪ [2020 (12.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn121.en.md) ▪ [2022 (13.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn131.en.md) ▪ [2025 (14.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn143.en.md)