use of org.apache.ignite.ml.clustering.kmeans.KMeansTrainer in project ignite by apache.
the class KMeansClusterizationExportImportExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> KMeans clustering algorithm over cached dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
Path jsonMdlPath = null;
try {
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS);
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
KMeansTrainer trainer = new KMeansTrainer().withDistance(new WeightedMinkowskiDistance(2, new double[] { 5.9360, 2.7700, 4.2600, 1.3260 }));
// .withDistance(new MinkowskiDistance(2));
KMeansModel mdl = trainer.fit(ignite, dataCache, vectorizer);
System.out.println("\n>>> Exported KMeans model: " + mdl);
jsonMdlPath = Files.createTempFile(null, null);
mdl.toJSON(jsonMdlPath);
KMeansModel modelImportedFromJSON = KMeansModel.fromJSON(jsonMdlPath);
System.out.println("\n>>> Imported KMeans model: " + modelImportedFromJSON);
System.out.println("\n>>> KMeans clustering algorithm over cached dataset usage example completed.");
} finally {
if (dataCache != null)
dataCache.destroy();
if (jsonMdlPath != null)
Files.deleteIfExists(jsonMdlPath);
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.clustering.kmeans.KMeansTrainer in project ignite by apache.
the class LocalModelsTest method getClusterModel.
/**
*/
private KMeansModel getClusterModel() {
Map<Integer, double[]> data = new HashMap<>();
data.put(0, new double[] { 1.0, 1959, 325100 });
data.put(1, new double[] { 1.0, 1960, 373200 });
KMeansTrainer trainer = new KMeansTrainer().withAmountOfClusters(1);
return trainer.fit(new LocalDatasetBuilder<>(data, 2), new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
}
use of org.apache.ignite.ml.clustering.kmeans.KMeansTrainer in project ignite by apache.
the class KeepBinaryTest method test.
/**
* Startup Ignite, populate cache and train some model.
*/
@Test
public void test() {
IgniteCache<Integer, BinaryObject> dataCache = populateCache(ignite);
KMeansTrainer trainer = new KMeansTrainer();
CacheBasedDatasetBuilder<Integer, BinaryObject> datasetBuilder = new CacheBasedDatasetBuilder<>(ignite, dataCache).withKeepBinary(true);
KMeansModel mdl = trainer.fit(datasetBuilder, new BinaryObjectVectorizer<Integer>("feature1").labeled("label"));
Integer zeroCentre = mdl.predict(VectorUtils.num2Vec(0.0));
assertTrue(mdl.centers()[zeroCentre].get(0) == 0);
}
use of org.apache.ignite.ml.clustering.kmeans.KMeansTrainer in project ignite by apache.
the class CustomersClusterizationExample method main.
/**
* Runs example.
*/
public static void main(String[] args) throws IOException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
System.out.println(">>> Fill dataset cache.");
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.WHOLESALE_CUSTOMERS);
System.out.println(">>> Start training and scoring.");
for (int amountOfClusters = 1; amountOfClusters < 10; amountOfClusters++) {
KMeansTrainer trainer = new KMeansTrainer().withAmountOfClusters(amountOfClusters).withDistance(new EuclideanDistance()).withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0)).withMaxIterations(50);
// This vectorizer works with values in cache of Vector class.
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(// FIRST means "label are stored at first coordinate of vector"
Vectorizer.LabelCoordinate.FIRST);
// Splits dataset to train and test samples with 80/20 proportion.
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.8);
KMeansModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
double entropy = computeMeanEntropy(dataCache, split.getTestFilter(), vectorizer, mdl);
System.out.println(String.format(">> Clusters mean entropy [%d clusters]: %.2f", amountOfClusters, entropy));
}
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.clustering.kmeans.KMeansTrainer in project ignite by apache.
the class KMeansClusterizationExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> KMeans clustering algorithm over cached dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS);
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
KMeansTrainer trainer = new KMeansTrainer();
KMeansModel mdl = trainer.fit(ignite, dataCache, vectorizer);
System.out.println(">>> KMeans centroids");
Tracer.showAscii(mdl.centers()[0]);
Tracer.showAscii(mdl.centers()[1]);
System.out.println(">>>");
System.out.println(">>> --------------------------------------------");
System.out.println(">>> | Predicted cluster\t| Erased class label\t|");
System.out.println(">>> --------------------------------------------");
try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, Vector> observation : observations) {
Vector val = observation.getValue();
Vector inputs = val.copyOfRange(1, val.size());
double groundTruth = val.get(0);
double prediction = mdl.predict(inputs);
System.out.printf(">>> | %.4f\t\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
System.out.println(">>> ---------------------------------");
System.out.println(">>> KMeans clustering algorithm over cached dataset usage example completed.");
}
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
Aggregations