Search in sources :

Example 1 with WeightedMinkowskiDistance

use of org.apache.ignite.ml.math.distances.WeightedMinkowskiDistance 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();
    }
}
Also used : Path(java.nio.file.Path) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) KMeansModel(org.apache.ignite.ml.clustering.kmeans.KMeansModel) KMeansTrainer(org.apache.ignite.ml.clustering.kmeans.KMeansTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) WeightedMinkowskiDistance(org.apache.ignite.ml.math.distances.WeightedMinkowskiDistance)

Aggregations

Path (java.nio.file.Path)1 Ignite (org.apache.ignite.Ignite)1 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)1 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)1 KMeansTrainer (org.apache.ignite.ml.clustering.kmeans.KMeansTrainer)1 WeightedMinkowskiDistance (org.apache.ignite.ml.math.distances.WeightedMinkowskiDistance)1 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)1