Skip to contents

Methods for e-means fits

Usage

# S3 method for class 'emeans'
predict(
  object,
  newdata,
  type = c("cluster", "distance"),
  device = object$recipe$device,
  ...
)

# S3 method for class 'emeans'
fitted(object, method = c("centers", "classes"), ...)

# S3 method for class 'emeans'
print(x, ...)

# S3 method for class 'emeans'
summary(object, ...)

# S3 method for class 'summary.emeans'
print(x, ...)

Arguments

object, x

An "emeans" object (x for print()).

newdata

Numeric matrix or data frame with the same columns as the training data. If both newdata and the centers have column names, the columns are matched by name. On a torch device it may also be a 2-D torch_tensor, which is cast to float32 and not checked for NA.

type

"cluster" for the nearest-center label, or "distance" for the score of that center: squared Euclidean distance, or inner-product similarity when the fit is spherical.

device

Device used to assign newdata; by default the fit's own (see "Devices" in emeans()).

...

Unused.

method

"centers" for each row's center, or "classes" for labels.

Value

predict() returns an integer vector of labels or a numeric vector of scores. fitted() returns a matrix or an integer vector. summary() returns an object of class "summary.emeans".

Examples

set.seed(1)
x <- rbind(matrix(rnorm(200, sd = 0.3), ncol = 2),
           matrix(rnorm(200, mean = 3, sd = 0.3), ncol = 2))
fit <- emeans(x, 2, threads = 1)
fit
#> e-means clustering: k = 2, 200 rows
#>   epochs 5, batch 2, alpha0 0.087
#>   tot.withinss 33.6568 (between_SS / total_SS = 96.4 %), n_splits 0
#>   device cpu
summary(fit)
#> Summary of e-means fit
#> Call: emeans(x = x, k = 2, threads = 1)
#> k = 2, epochs 5, batch 2, alpha0 0.087, device cpu
#> tot.withinss 33.6568 (between_SS / total_SS = 96.4 %), n_splits 0
#> Cluster size:
#> [1] 100 100
#> History (last rows):
#>  epoch rows      obj  time imbalance nsplit
#>      1  200 65.82473 0.011         1      0
#>      2  200 35.50273 0.021         1      0
#>      3  200 34.56189 0.032         1      0
#>      4  200 33.98544 0.043         1      0
#>      5  200 34.11096 0.057         1      0

# Label new points, or score them by their nearest center
newx <- rbind(c(0, 0), c(3, 3))
predict(fit, newx)
#> [1] 1 2
predict(fit, newx, type = "distance")
#> [1] 0.0011957318 0.0003188943

# Each training row's center, or its cluster
head(fitted(fit))
#>            [,1]        [,2]
#> [1,] 0.03266621 -0.01134242
#> [2,] 0.03266621 -0.01134242
#> [3,] 0.03266621 -0.01134242
#> [4,] 0.03266621 -0.01134242
#> [5,] 0.03266621 -0.01134242
#> [6,] 0.03266621 -0.01134242
head(fitted(fit, method = "classes"))
#> [1] 1 1 1 1 1 1