# fcm

Fuzzy c-means clustering

## Syntax

• ``````[centers,U] = fcm(data,Nc)``````
example
• ``````[centers,U] = fcm(data,Nc,options)``````
example
• ``````[centers,U,objFunc] = fcm(___)``````
example

## Description

example

``````[centers,U] = fcm(data,Nc)``` performs fuzzy c-means clustering on the given data and returns `Nc` cluster centers.```

example

``````[centers,U] = fcm(data,Nc,options)``` specifies additional clustering options.```

example

``````[centers,U,objFunc] = fcm(___)``` also returns the objective function values at each optimization iteration for all of the previous syntaxes.```

## Examples

collapse all

### Cluster Data Using Fuzzy C-Means Clustering

```load fcmdata.dat ```

Find `2` clusters using fuzzy c-means clustering.

```[centers,U] = fcm(fcmdata,2); ```
```Iteration count = 1, obj. fcn = 8.970479 Iteration count = 2, obj. fcn = 7.197402 Iteration count = 3, obj. fcn = 6.325579 Iteration count = 4, obj. fcn = 4.586142 Iteration count = 5, obj. fcn = 3.893114 Iteration count = 6, obj. fcn = 3.810804 Iteration count = 7, obj. fcn = 3.799801 Iteration count = 8, obj. fcn = 3.797862 Iteration count = 9, obj. fcn = 3.797508 Iteration count = 10, obj. fcn = 3.797444 Iteration count = 11, obj. fcn = 3.797432 Iteration count = 12, obj. fcn = 3.797430 ```

Classify each data point into the cluster with the largest membership value.

```maxU = max(U); index1 = find(U(1,:) == maxU); index2 = find(U(2,:) == maxU); ```

Plot the clustered data and cluster centers.

```plot(fcmdata(index1,1),fcmdata(index1,2),'ob') hold on plot(fcmdata(index2,1),fcmdata(index2,2),'or') plot(centers(1,1),centers(1,2),'xb','MarkerSize',15,'LineWidth',3) plot(centers(2,1),centers(2,2),'xr','MarkerSize',15,'LineWidth',3) hold off ```

### Specify Fuzzy Overlap Between Clusters

Create a random data set.

```data = rand(100,2); ```

Specify a large fuzzy partition matrix exponent to increase the amount of fuzzy ovrelap between the clusters.

```options = [3.0 NaN NaN 0]; ```

Cluster the data.

```[centers,U] = fcm(data,2,options); ```

### Configure Clustering Termination Conditions

```load clusterdemo.dat ```

Set the clustering termination conditions such that the optimization stops when either of the following occurs:

• The number of iterations reaches a maximum of `25`.

• The objective function improves by less than `0.001` between two consecutive iterations.

```options = [NaN 25 0.001 0]; ```

The first option is `NaN`, which sets the fuzzy partition matrix exponent to its default value of `2`. Setting the fourth option to `0` suppresses the objective function display.

Cluster the data.

```[centers,U,objFun] = fcm(clusterdemo,3,options); ```

View the objective function vector to determine which termination condition stopped the clustering.

```objFun ```
```objFun = 54.7257 42.9867 42.8554 42.1857 39.0857 31.6814 28.5736 27.1806 20.7359 15.7147 15.4353 15.4306 15.4305 ```

The optimization stopped because the objective function improved by less than `0.001` between the final two iterations.

## Input Arguments

collapse all

### `data` — Data set to be clusteredmatrix

Data set to be clustered, specified as a matrix with Nd rows, where Nd is the number of data points. The number of columns in `data` is equal to the data dimensionality.

### `Nc` — Number of clustersinteger

Number of clusters, specified as an integer greater than `1`.

### `options` — Clustering optionsvector

Clustering options, specified as a vector with the following elements:

OptionDescriptionDefault
`options(1)`

Exponent for the fuzzy partition matrix `U`, specified as a scalar greater than `1.0`. This option controls the amount of fuzzy overlap between clusters, with larger values indicating a greater degree of overlap.

If your data set is wide with a lot of overlap between potential clusters, then the calculated cluster centers might be very close to each other. In this case, each data point has approximately the same degree of membership in all clusters. To improve your clustering results, decrease this value, which limits the amount of fuzzy overlap during clustering.

For an example of fuzzy overlap adjustment, see Adjust Fuzzy Overlap in Fuzzy C-Means Clustering.

`2.0`
`options(2)`

Maximum number of iterations, specified as a positive integer.

`100`
`options(3)`

Minimum improvement in objective function between two consecutive iterations, specified as a positive scalar.

`1e-5`
`options(4)`

Information display toggle indicating whether to display the objective function value after each iteration, specified as one of the following:

• `0` — Do not display objective function.

• `1` — Display objective function.

`1`

If any element of `options` is `NaN`, the default value for that option is used.

The clustering process stops when the maximum number of iterations is reached or when the objective function improvement between two consecutive iterations is less than the specified minimum.

## Output Arguments

collapse all

### `centers` — Cluster centersmatrix

Final cluster centers, returned as a matrix with `Nc` rows containing the coordinates of each cluster center. The number of columns in `centers` is equal to the dimensionality of the data being clustered.

### `U` — Fuzzy partition matrixmatrix

Fuzzy partition matrix, returned as a matrix with `Nc` rows and Nd columns. Element `U(i,j)` indicates the degree of membership of the jth data point in the ith cluster. For a given data point, the sum of the membership values for all clusters is one.

### `objFunc` — Objective function valuesvector

Objective function values for each iteration, returned as a vector.

expand all

### Algorithms

Fuzzy c-means (FCM) is a clustering method that allows each data point to belong to multiple clusters with varying degrees of membership.

FCM is based on the minimization of the following objective function

${J}_{m}=\sum _{i=1}^{D}\sum _{j=1}^{N}{\mu }_{ij}^{m}{‖{x}_{i}-{c}_{j}‖}^{2},$

where

• D is the number of data points.

• N is the number of clusters.

• m is fuzzy partition matrix exponent for controlling the degree of fuzzy overlap, with m > 1. Fuzzy overlap refers to how fuzzy the boundaries between clusters are, that is the number of data points that have significant membership in more than one cluster.

• xi is the ith data point.

• cj is the center of the jth cluster.

• μij is the degree of membership of xi in the jth cluster. For a given data point, xi, the sum of the membership values for all clusters is one.

`fcm` performs the following steps during clustering:

1. Randomly initialize the cluster membership values, μij.

2. Calculate the cluster centers:

${c}_{j}=\frac{\sum _{i=1}^{D}{\mu }_{ij}^{m}{x}_{i}}{\sum _{i=1}^{D}{\mu }_{ij}^{m}}.$

3. Update μij according to the following:

${\mu }_{ij}=\frac{1}{\sum _{k=1}^{N}{\left(\frac{‖{x}_{i}-{c}_{j}‖}{‖{x}_{i}-{c}_{k}‖}\right)}^{\frac{2}{m-1}}}.$

4. Calculate the objective function, Jm.

5. Repeat steps 2–4 until Jm improves by less than a specified minimum threshold or until after a specified maximum number of iterations.

## References

[1] Bezdec, J.C., Pattern Recognition with Fuzzy Objective Function Algorithms, Plenum Press, New York, 1981.