Fuzzy C-means
Fuzzy C-means is a clustering method that provides cluster membership weights instead of "hard" classification (e.g. K-means).
From a mathematical standpoint, fuzzy C-means solves the following optimization problem:
\[\arg\min_\mathcal{C} \ \sum_{i=1}^n \sum_{j=1}^C w_{ij}^\mu \| \mathbf{x}_i - \mathbf{c}_j \|^2, \ \text{where}\ w_{ij} = \left(\sum_{k=1}^{C} \left(\frac{\left\|\mathbf{x}_i - \mathbf{c}_j \right\|}{\left\|\mathbf{x}_i - \mathbf{c}_k \right\|}\right)^{\frac{2}{\mu-1}}\right)^{-1}\]
Here, $\mathbf{c}_j$ is the center of the $j$-th cluster, $w_{ij}$ is the membership weight of the $i$-th point in the $j$-th cluster, and $\mu > 1$ is a user-defined fuzziness parameter.
Clustering.fuzzy_cmeans
— Functionfuzzy_cmeans(data::AbstractMatrix, C::Integer, fuzziness::Real;
[dist_metric::SemiMetric], [...]) -> FuzzyCMeansResult
Perform Fuzzy C-means clustering over the given data
.
Arguments
data::AbstractMatrix
: $d×n$ data matrix. Each column represents one $d$-dimensional data point.C::Integer
: the number of fuzzy clusters, $2 ≤ C < n$.fuzziness::Real
: clusters fuzziness ($μ$ in the mathematical formulation), $μ > 1$.
Optional keyword arguments:
dist_metric::SemiMetric
(defaults toEuclidean
): theSemiMetric
object that defines the distance between the data pointsmaxiter
,tol
,display
,rng
: see common options
Clustering.FuzzyCMeansResult
— TypeFuzzyCMeansResult{T<:AbstractFloat}
The output of fuzzy_cmeans
function.
Fields
centers::Matrix{T}
: the $d×C$ matrix with columns being the centers of resulting fuzzy clustersweights::Matrix{Float64}
: the $n×C$ matrix of assignment weights ($\mathrm{weights}_{ij}$ is the weight (probability) of assigning $i$-th point to the $j$-th cluster)iterations::Int
: the number of executed algorithm iterationsconverged::Bool
: whether the procedure converged
Clustering.wcounts
— Functionwcounts(R::ClusteringResult) -> Vector{Float64}
wcounts(R::FuzzyCMeansResult) -> Vector{Float64}
Get the weighted cluster sizes as the sum of weights of points assigned to each cluster.
For non-weighted clusterings assumes the weight of every data point is 1.0, so the result is equivalent to convert(Vector{Float64}, counts(R))
.
Examples
using Clustering
# make a random dataset with 1000 points
# each point is a 5-dimensional vector
X = rand(5, 1000)
# performs Fuzzy C-means over X, trying to group them into 3 clusters
# with a fuzziness factor of 2. Set maximum number of iterations to 200
# set display to :iter, so it shows progressive info at each iteration
R = fuzzy_cmeans(X, 3, 2, maxiter=200, display=:iter)
# get the centers (i.e. weighted mean vectors)
# M is a 5x3 matrix
# M[:, k] is the center of the k-th cluster
M = R.centers
# get the point memberships over all the clusters
# memberships is a 20x3 matrix
memberships = R.weights
1000×3 Matrix{Float64}:
0.335172 0.332774 0.332054
0.334844 0.332036 0.333119
0.33287 0.333427 0.333703
0.333038 0.333624 0.333338
0.333143 0.336599 0.330258
0.334728 0.335842 0.32943
0.334694 0.336375 0.328931
0.33332 0.334219 0.332461
0.334234 0.331995 0.333771
0.332663 0.332805 0.334533
⋮
0.332834 0.332918 0.334248
0.334971 0.332476 0.332553
0.332947 0.333854 0.333199
0.332761 0.334764 0.332475
0.332491 0.32828 0.339228
0.33194 0.33634 0.33172
0.333879 0.329714 0.336407
0.3328 0.330262 0.336938
0.333461 0.335005 0.331534