use of org.apache.ignite.ml.clustering.KMeansDistributedClusterer in project ignite by apache.
the class KMeansDistributedClustererExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws InterruptedException {
// IMPL NOTE based on KMeansDistributedClustererTestSingleNode#testClusterizationOnDatasetWithObviousStructure
System.out.println(">>> K-means distributed clusterer example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
int ptsCnt = 10000;
SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
DatasetWithObviousStructure dataset = new DatasetWithObviousStructure(10000);
List<Vector> massCenters = dataset.generate(points);
EuclideanDistance dist = new EuclideanDistance();
KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
Vector[] resCenters = clusterer.cluster(points, 4).centers();
System.out.println("Mass centers:");
massCenters.forEach(Tracer::showAscii);
System.out.println("Cluster centers:");
Arrays.asList(resCenters).forEach(Tracer::showAscii);
points.destroy();
System.out.println("\n>>> K-means distributed clusterer example completed.");
});
igniteThread.start();
igniteThread.join();
}
}
use of org.apache.ignite.ml.clustering.KMeansDistributedClusterer in project ignite by apache.
the class IgniteKMeansDistributedClustererBenchmark method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
final DataChanger.Scale scale = new DataChanger.Scale();
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), this.getClass().getSimpleName(), new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// IMPL NOTE originally taken from KMeansDistributedClustererTest
KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(new EuclideanDistance(), 1, 1, 1L);
double[] v1 = scale.mutate(new double[] { 1959, 325100 });
double[] v2 = scale.mutate(new double[] { 1960, 373200 });
SparseDistributedMatrix points = new SparseDistributedMatrix(2, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
points.setRow(0, v1);
points.setRow(1, v2);
clusterer.cluster(points, 1);
points.destroy();
}
});
igniteThread.start();
igniteThread.join();
return true;
}
Aggregations