kmeans_cluster: kmeans_cluster

Description Usage Arguments Value Examples

View source: R/kmeans_cluster.R

Description

Perform k-means clustering on a data matrix.

Usage

1
kmeans_cluster(X, center, max.iter = 2500, tol = 1e-08)

Arguments

X

numeric matrix of data, or an object that can be coerced to such a matrix (such as a numeric vector or a data frame with all numeric columns).

center

either the number of clusters, say \(k\), or a set of initial (distinct) cluster centres. If a number, a random center will be assigned.

max.iter

the maximum number of iterations allowed.

tol

Parameters for determining convergence.

Value

a list contains centres of each cluster and the labels for each observation

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# One Dimension Example
x1 <-  runif(10000,0,1)
x2 <-  runif(10000,2,3)
x3 <-  runif(10000,4,5)
train_data <-  c(x1,x2,x3)
train_label <-  c(rep(0,10000),rep(1,10000),rep(2,10000))
training_result <-  kmeans_cluster(train_data,3,2500,1e-8)

# Multi Dimension Example
X1 <-  runif(10000,0,1)
X2 <-  runif(10000,0,1)
X3 <-  runif(10000,3,5)
X4 <-  runif(10000,3,5)
train_data <-  cbind(c(X1,X3),c(X2,X4))
train_label <-  c(rep(0,10000),rep(1,10000))
training_result<-  kmeans_cluster(train_data,2,2500,1e-8)

zhaodyleo/Kmeans documentation built on Dec. 23, 2021, 9:18 p.m.