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.331442 0.334705 0.333854
0.335799 0.330725 0.333477
0.33404 0.331592 0.334367
0.333958 0.331913 0.334129
0.33173 0.336183 0.332087
0.335707 0.330325 0.333967
0.330475 0.336607 0.332917
0.334023 0.332983 0.332994
0.331624 0.335362 0.333014
0.331865 0.334229 0.333906
⋮
0.334405 0.331094 0.334501
0.33571 0.331116 0.333174
0.33175 0.334173 0.334078
0.332709 0.333976 0.333315
0.330452 0.335102 0.334446
0.334575 0.330656 0.334769
0.334914 0.331425 0.333661
0.335804 0.329848 0.334348
0.332221 0.334744 0.333036