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.33434 0.331047 0.334612
0.334198 0.333619 0.332183
0.333612 0.33434 0.332048
0.334455 0.331583 0.333961
0.33395 0.334411 0.331639
0.333246 0.334795 0.331959
0.33284 0.333236 0.333924
0.331636 0.33434 0.334024
0.333507 0.332945 0.333548
0.332767 0.335326 0.331907
⋮
0.331098 0.334792 0.33411
0.331947 0.337083 0.33097
0.333607 0.331842 0.334551
0.331902 0.333937 0.334162
0.334562 0.332209 0.333229
0.335227 0.332442 0.332332
0.332186 0.331953 0.33586
0.331836 0.334882 0.333282
0.333181 0.33488 0.331939