Class KMeansTrainer
java.lang.Object
org.tribuo.clustering.kmeans.KMeansTrainer
- All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.config.Configurable
,com.oracle.labs.mlrg.olcut.provenance.Provenancable<TrainerProvenance>
,Trainer<ClusterID>
A K-Means trainer, which generates a K-means clustering of the supplied
data. The model finds the centres, and then predict needs to be
called to infer the centre assignments for the input data.
It's slightly contorted to fit the Tribuo Trainer and Model API, as the cluster assignments can only be retrieved from the model after training, and require re-evaluating each example.
The Trainer has a parameterised distance function, and a selectable number of threads used in the training step. The thread pool is local to an invocation of train, so there can be multiple concurrent trainings.
See:
J. Friedman, T. Hastie, & R. Tibshirani. "The Elements of Statistical Learning" Springer 2001. PDF
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from interface org.tribuo.Trainer
DEFAULT_SEED
-
Constructor Summary
ConstructorsConstructorDescriptionKMeansTrainer
(int centroids, int iterations, KMeansTrainer.Distance distanceType, int numThreads, long seed) Constructs a K-Means trainer using the supplied parameters. -
Method Summary
Modifier and TypeMethodDescriptionint
The number of times this trainer instance has had it's train method invoked.protected static DenseVector[]
initialiseCentroids
(int centroids, Dataset<ClusterID> examples, ImmutableFeatureMap featureMap, SplittableRandom rng) Initialisation method called at the start of each train call.protected void
mStep
(ForkJoinPool fjp, DenseVector[] centroidVectors, Map<Integer, List<Integer>> clusterAssignments, SparseVector[] data, double[] weights) void
toString()
Trains a predictive model using the examples in the given data set.train
(Dataset<ClusterID> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance) Trains a predictive model using the examples in the given data set.
-
Constructor Details
-
KMeansTrainer
public KMeansTrainer(int centroids, int iterations, KMeansTrainer.Distance distanceType, int numThreads, long seed) Constructs a K-Means trainer using the supplied parameters.- Parameters:
centroids
- The number of centroids to use.iterations
- The maximum number of iterations.distanceType
- The distance function.numThreads
- The number of threads.seed
- The random seed.
-
-
Method Details
-
postConfig
- Specified by:
postConfig
in interfacecom.oracle.labs.mlrg.olcut.config.Configurable
-
train
-
train
Description copied from interface:Trainer
Trains a predictive model using the examples in the given data set. -
getInvocationCount
Description copied from interface:Trainer
The number of times this trainer instance has had it's train method invoked.This is used to determine how many times the trainer's RNG has been accessed to ensure replicability in the random number stream.
- Specified by:
getInvocationCount
in interfaceTrainer<ClusterID>
- Returns:
- The number of train invocations.
-
initialiseCentroids
protected static DenseVector[] initialiseCentroids(int centroids, Dataset<ClusterID> examples, ImmutableFeatureMap featureMap, SplittableRandom rng) Initialisation method called at the start of each train call. Used to allow overriding for kmeans++, kmedoids etc.- Parameters:
centroids
- The number of centroids to create.examples
- The dataset to use.featureMap
- The feature map to use for centroid sampling.rng
- The RNG to use.- Returns:
- A
DenseVector
array of centroids.
-
mStep
protected void mStep(ForkJoinPool fjp, DenseVector[] centroidVectors, Map<Integer, List<Integer>> clusterAssignments, SparseVector[] data, double[] weights) -
toString
-
getProvenance
- Specified by:
getProvenance
in interfacecom.oracle.labs.mlrg.olcut.provenance.Provenancable<TrainerProvenance>
-