View source: R/BalancedKmeansClustering.R
| BalancedKmeansClustering | R Documentation |
Performs balanced k-means clustering using an independent base-R implementation of Balanced k-Means Revisited (BKM+) [de Maeyer et al., 2023].
The method combines squared-Euclidean k-means optimization with an adaptive
cluster-size penalty. It searches for a partition whose difference between the
largest and smallest cluster sizes does not exceed MaxDiff, while
retaining a low total within-cluster sum of squares.
BalancedKmeansClustering(
Data,
ClusterNo = 2,
MaxDiff = 1,
IterMax = 1000,
Switch = 10,
Nstart = 1,
Centers = NULL,
Seed = NULL,
PartlyRemainingFraction = 0.15,
IncreasingPenaltyFactor = 1.01,
UseFunctionIter = TRUE,
StopWhenBalanced = FALSE,
PlotIt = FALSE,
Verbose = FALSE,
KeepHistory = TRUE
)
Data |
[1:n,1:d] matrix containing the dataset to be clustered. It consists of n cases of d-dimensional data points. Every case has d attributes, variables or features. |
ClusterNo |
Positive integer number of clusters. It must lie between one and the number of observations. |
MaxDiff |
Non-negative integer giving the largest permitted difference between the
largest and smallest cluster sizes. Default is
|
IterMax |
Positive integer maximum number of adaptive-penalty iterations per start.
Default is |
Switch |
Non-negative integer maximum number of pairwise switch-refinement passes.
|
Nstart |
Positive integer number of initializations. Default is |
Centers |
Optional initial centroid matrix with |
Seed |
An explicit seed is local to the function call: the previous global
random-number-generator state is restored on exit, including after errors.
With |
PartlyRemainingFraction |
Finite numeric scalar strictly between zero and one. It supplies the
fractional term used in the adaptive cluster-size penalty after an
observation is temporarily removed from its current cluster. Default is
|
IncreasingPenaltyFactor |
Finite numeric scalar at least one. Constant multiplier used to increase
the balancing penalty when |
UseFunctionIter |
Logical scalar. If |
StopWhenBalanced |
Logical scalar. If |
PlotIt |
Logical scalar. If |
Verbose |
Logical scalar. If |
KeepHistory |
Logical scalar. If |
Feature data only
BKM+ updates arithmetic centroids in the original feature space. In addition to objects of class
"dist", a finite numeric square matrix
is rejected as a distance matrix when it is approximately symmetric, has an
approximately zero diagonal, and has no materially negative entries. A square
feature matrix that also has all of these properties is therefore ambiguous
and is rejected deliberately.
Initialization
If Centers = NULL, ClusterNo observations are sampled without
replacement as initial centers. Initial assignment uses squared Euclidean
distance and chooses the first cluster in exact ties.
Duplicate observations or supplied centers may create empty initial clusters. Each empty cluster is initialized with a high-error observation taken from a non-singleton donor cluster. This repair is confined to initialization.
Adaptive-penalty balancing
The algorithm performs sequential observation reassignments. It never removes an observation from a singleton cluster. Candidate target clusters are evaluated using squared centroid distance and an adaptive cluster-size penalty.
A partition is considered balanced when
\max_k n_k - \min_k n_k \leq \mathrm{MaxDiff}.
The best feasible assignment is retained according to total within-cluster SSE. A stored feasible assignment is not discarded if the iteration limit is subsequently reached.
When UseFunctionIter = TRUE, the next finite penalty threshold is
multiplied by
1.1009 - 0.0009 s
for iteration step s \leq 100, and by 1.01 thereafter. When
UseFunctionIter = FALSE, IncreasingPenaltyFactor is used.
Switch refinement
After the balancing phase, the algorithm considers pairwise exchanges of observations between clusters. Exchanges are accepted when they reduce total within-cluster SSE. Because observations are exchanged in pairs, cluster sizes remain unchanged.
Multiple starts
Feasible starts always outrank infeasible starts. Among feasible starts, the smallest total within-cluster SSE is selected. Among infeasible starts, the smallest cluster-size difference is selected first, with SSE used as a tie-breaker.
Classification and output order
The classification is represented consistently by Cls. It is a
numerical vector with labels 1:ClusterNo. No post-fitting sorting or
renumbering by centroid coordinates is applied. Thus Cls[i] always
refers to Data[i, ], and all cluster-indexed outputs use the same native
cluster numbering in contrast to the referenced algorithm in C.
Detailed object fields
The returned Object includes:
ClsSame numerical cluster vector as the main output.
centersFinal centroid matrix.
sizeCluster sizes.
withinssWithin-cluster SSE by cluster.
tot.withinssTotal within-cluster SSE.
MSEtot.withinss / n.
iterBalancing iterations executed.
best.iterIteration associated with the retained assignment.
penaltyPenalty associated with the retained assignment.
last.penaltyFinal penalty reached by the balancing loop.
maxdiffFinal maximum cluster-size difference.
balancedWhether maxdiff <= MaxDiff.
hard.balancedWhether maxdiff <= 1.
convergedBalancing convergence indicator.
terminationCharacter termination reason.
initial.centersInitial centers of the selected start.
initial.empty.clustersNumber of repaired empty initial clusters.
sse.before.switchSSE before switch refinement.
switch.iterNumber of switch passes.
switchesTotal number of exchanged observation pairs.
switch.convergedSwitch-refinement convergence indicator.
historyBalancing history when requested.
switch.historySwitch-pass history when requested.
nstartNumber of requested starts.
startsSummary data frame for all starts.
best.startIndex of the selected start.
maxdiff.requestedRequested maximum size difference.
parametersPrincipal input parameters.
callMatched call.
A list with components:
Cls |
Numerical vector of length |
Object |
Detailed BKM+ result. Important fields include |
Centroids |
Final centroid matrix, identical to |
Michael Thrun
[de Maeyer et al., 2023] de Maeyer, R., Sieranoja, S., and Franti, P.:Balanced k-means revisited, Applied Computing and Intelligence, 3(2), 145-179. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3934/aci.2023008")}, 2023
kmeansClustering, kmeans
data('Hepta')
out=BalancedKmeansClustering(Hepta$Data,ClusterNo = 7,PlotIt=FALSE)
## Not run:
set.seed(1)
Data <- rbind(
matrix(rnorm(60, mean = 0), ncol = 2),
matrix(rnorm(60, mean = 5), ncol = 2)
)
result <- BalancedKmeansClustering(
Data = Data,
ClusterNo = 2,
MaxDiff = 1,
Nstart = 10,
Seed = 123
)
table(result$Cls)
result$Centroids
result$Object$tot.withinss
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.