You could try using ClusterR, especially the function MiniBatchKmeans
Here is an example of usage:
some data (smaller than yours - 300k rows and 30 columns)
z <- rbind(replicate(30, rnorm(1e5, 2)),
replicate(30, rnorm(1e5, -1)),
replicate(30, rnorm(1e5, 5)))
library(ClusterR)
km_model <- MiniBatchKmeans(z, clusters = 3, batch_size = 20, num_init = 5, max_iters = 100,
init_fraction = 0.2, initializer = 'kmeans++', early_stop_iter = 10,
verbose = F)
pred <- predict_MBatchKMeans(z, km_model$centroids)
object pred contains the associated clusters:
table(pred)
pred
1 2 3
100000 100000 100000
I'd say that was a perfect separation. Increasing the batch size and number of initiations is advisable if the function is fast for you.
Speed:
library(microbenchmark)
microbenchmark(km_model <- MiniBatchKmeans(z, clusters = 3, batch_size = 20, num_init = 5, max_iters = 100,
init_fraction = 0.2, initializer = 'kmeans++', early_stop_iter = 10,
verbose = F))
Unit: seconds
expr
km_model <- MiniBatchKmeans(z, clusters = 3, batch_size = 20, num_init = 5, max_iters = 100, init_fraction = 0.2, initializer = "kmeans++", early_stop_iter = 10, verbose = F)
min lq mean median uq max neval
3.338328 3.366573 3.473403 3.444095 3.518813 4.176116 100