public class FloatKMeans extends Object implements SpatialClusterer<FloatCentroidsResult,float[]>
FloatNearestNeighbours
; for
example, approximate K-Means can be achieved using a
FloatNearestNeighboursKDTree
whilst exact K-Means can be achieved
using an FloatNearestNeighboursExact
. The specific choice of
nearest-neighbour object is controlled through the
NearestNeighboursFactory
provided to the KMeansConfiguration
used to construct instances of this class. The choice of
FloatNearestNeighbours
affects the speed of clustering; using
approximate nearest-neighbours algorithms for the K-Means can produces
comparable results to the exact KMeans algorithm in much shorter time.
The choice and configuration of FloatNearestNeighbours
can also
control the type of distance function being used in the clustering.
The algorithm is implemented as follows: Clustering is initiated using a
FloatKMeansInit
and is iterative. In each round, batches of
samples are assigned to centroids in parallel. The centroid assignment is
performed using the pre-configured FloatNearestNeighbours
instances
created from the KMeansConfiguration
. Once all samples are assigned
new centroids are calculated and the next round started. Data point pushing
is performed using the same techniques as center point assignment.
This implementation is able to deal with larger-than-memory datasets by
streaming the samples from disk using an appropriate DataSource
. The
only requirement is that there is enough memory to hold all the centroids
plus working memory for the batches of samples being assigned.
Modifier | Constructor and Description |
---|---|
protected |
FloatKMeans()
A completely default
FloatKMeans used primarily as a convenience function for reading. |
|
FloatKMeans(KMeansConfiguration<FloatNearestNeighbours,float[]> conf)
Construct the clusterer with the the given configuration.
|
Modifier and Type | Method and Description |
---|---|
FloatCentroidsResult |
cluster(DataSource<float[]> ds)
Perform clustering with data from a data source.
|
protected void |
cluster(DataSource<float[]> data,
org.openimaj.ml.clustering.kmeans.FloatKMeans.Result result)
Main clustering algorithm.
|
protected org.openimaj.ml.clustering.kmeans.FloatKMeans.Result |
cluster(DataSource<float[]> data,
int K)
Initiate clustering with the given data and number of clusters.
|
FloatCentroidsResult |
cluster(float[][] data)
Perform clustering on the given data.
|
static FloatKMeans |
createExact(int K)
Convenience method to quickly create an exact
FloatKMeans . |
static FloatKMeans |
createExact(int K,
int niters)
Convenience method to quickly create an exact
FloatKMeans . |
static FloatKMeans |
createKDTreeEnsemble(int K)
Convenience method to quickly create an approximate
FloatKMeans
using an ensemble of KD-Trees to perform nearest-neighbour lookup. |
KMeansConfiguration<FloatNearestNeighbours,float[]> |
getConfiguration()
Get the configuration
|
FloatKMeansInit |
getInit()
Get the current initialisation algorithm
|
int[][] |
performClustering(float[][] data) |
protected double |
roundDouble(double value) |
protected float |
roundFloat(double value) |
protected int |
roundInt(double value) |
protected long |
roundLong(double value) |
void |
seed(long seed)
Set the seed for the internal random number generator.
|
void |
setConfiguration(KMeansConfiguration<FloatNearestNeighbours,float[]> conf)
Set the configuration
|
void |
setInit(FloatKMeansInit init)
Set the current initialisation algorithm
|
String |
toString() |
public FloatKMeans(KMeansConfiguration<FloatNearestNeighbours,float[]> conf)
conf
- The configuration.protected FloatKMeans()
FloatKMeans
used primarily as a convenience function for reading.public FloatKMeansInit getInit()
public void setInit(FloatKMeansInit init)
init
- the init algorithm to be usedpublic void seed(long seed)
seed
- the random seed for init random sample selection, no seed if seed < -1public FloatCentroidsResult cluster(float[][] data)
SpatialClusterer
cluster
in interface SpatialClusterer<FloatCentroidsResult,float[]>
data
- the data.public int[][] performClustering(float[][] data)
performClustering
in interface Clusterer<float[][]>
protected org.openimaj.ml.clustering.kmeans.FloatKMeans.Result cluster(DataSource<float[]> data, int K) throws Exception
#cluster(DataSource, float [][])
.data
- data source to cluster withK
- number of clusters to findException
protected void cluster(DataSource<float[]> data, org.openimaj.ml.clustering.kmeans.FloatKMeans.Result result) throws Exception
data
- the data to be clusteredcentroids
- the centroids to be foundException
protected float roundFloat(double value)
protected double roundDouble(double value)
protected long roundLong(double value)
protected int roundInt(double value)
public FloatCentroidsResult cluster(DataSource<float[]> ds)
SpatialClusterer
DataSource
could potentially be backed by disk rather in memory.cluster
in interface SpatialClusterer<FloatCentroidsResult,float[]>
ds
- the data.public KMeansConfiguration<FloatNearestNeighbours,float[]> getConfiguration()
public void setConfiguration(KMeansConfiguration<FloatNearestNeighbours,float[]> conf)
conf
- the configuration to setpublic static FloatKMeans createExact(int K)
FloatKMeans
. All
parameters other than the number of clusters are set
at their defaults, but can be manipulated through the configuration
returned by getConfiguration()
.
Euclidean distance is used to measure the distance between points.
K
- the number of clustersFloatKMeans
instance configured for exact k-meanspublic static FloatKMeans createExact(int K, int niters)
FloatKMeans
. All
parameters other than the number of clusters and number
of iterations are set at their defaults, but can be manipulated through
the configuration returned by getConfiguration()
.
Euclidean distance is used to measure the distance between points.
K
- the number of clustersniters
- maximum number of iterationsFloatKMeans
instance configured for exact k-meanspublic static FloatKMeans createKDTreeEnsemble(int K)
FloatKMeans
using an ensemble of KD-Trees to perform nearest-neighbour lookup. All
parameters other than the number of clusters are set
at their defaults, but can be manipulated through the configuration
returned by getConfiguration()
.
Euclidean distance is used to measure the distance between points.
K
- the number of clustersFloatKMeans
instance configured for approximate k-means
using an ensemble of KD-Trees