Search in sources :

Example 21 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance 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;
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) DataChanger(org.apache.ignite.yardstick.ml.DataChanger) KMeansDistributedClusterer(org.apache.ignite.ml.clustering.KMeansDistributedClusterer) IgniteThread(org.apache.ignite.thread.IgniteThread)

Example 22 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance in project ignite by apache.

the class IgniteKMeansLocalClustererBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    final DataChanger.Scale scale = new DataChanger.Scale();
    // IMPL NOTE originally taken from KMeansLocalClustererTest
    KMeansLocalClusterer clusterer = new KMeansLocalClusterer(new EuclideanDistance(), 1, 1L);
    double[] v1 = scale.mutate(new double[] { 1959, 325100 });
    double[] v2 = scale.mutate(new double[] { 1960, 373200 });
    DenseLocalOnHeapMatrix points = new DenseLocalOnHeapMatrix(new double[][] { v1, v2 });
    clusterer.cluster(points, 1);
    return true;
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DataChanger(org.apache.ignite.yardstick.ml.DataChanger) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) KMeansLocalClusterer(org.apache.ignite.ml.clustering.KMeansLocalClusterer)

Example 23 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance in project ignite by apache.

the class IgniteKNNClassificationBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    // 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 KNNClassificationExample.
            // Obtain shuffled dataset.
            LabeledDataset dataset = new Datasets().shuffleIris((int) (DataChanger.next()));
            // Random splitting of iris data as 70% train and 30% test datasets.
            LabeledDatasetTestTrainPair split = new LabeledDatasetTestTrainPair(dataset, 0.3);
            LabeledDataset test = split.test();
            LabeledDataset train = split.train();
            KNNModel knnMdl = new KNNModel(5, new EuclideanDistance(), KNNStrategy.SIMPLE, train);
            // Calculate predicted classes.
            for (int i = 0; i < test.rowSize() - 1; i++) knnMdl.apply(test.getRow(i).features());
        }
    });
    igniteThread.start();
    igniteThread.join();
    return true;
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) LabeledDatasetTestTrainPair(org.apache.ignite.ml.structures.LabeledDatasetTestTrainPair) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) IgniteThread(org.apache.ignite.thread.IgniteThread) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset)

Example 24 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance in project ignite by apache.

the class LocalModelsTest method getClusterModel.

/**
 */
private KMeansModel getClusterModel() {
    KMeansLocalClusterer clusterer = new KMeansLocalClusterer(new EuclideanDistance(), 1, 1L);
    double[] v1 = new double[] { 1959, 325100 };
    double[] v2 = new double[] { 1960, 373200 };
    DenseLocalOnHeapMatrix points = new DenseLocalOnHeapMatrix(new double[][] { v1, v2 });
    return clusterer.cluster(points, 1);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) KMeansLocalClusterer(org.apache.ignite.ml.clustering.KMeansLocalClusterer)

Example 25 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance in project ignite by apache.

the class KMeansDistributedClustererTestSingleNode method testPerformClusterAnalysisDegenerate.

/**
 */
public void testPerformClusterAnalysisDegenerate() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(new EuclideanDistance(), 1, 1, 1L);
    double[] v1 = new double[] { 1959, 325100 };
    double[] v2 = 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);
    KMeansModel mdl = clusterer.cluster(points, 1);
    Assert.assertEquals(1, mdl.centers().length);
    Assert.assertEquals(2, mdl.centers()[0].size());
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)

Aggregations

EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)37 Vector (org.apache.ignite.ml.math.Vector)22 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)18 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)14 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)13 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)10 Test (org.junit.Test)10 KNNModel (org.apache.ignite.ml.knn.models.KNNModel)9 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)9 ArrayList (java.util.ArrayList)6 IgniteThread (org.apache.ignite.thread.IgniteThread)6 Arrays (java.util.Arrays)5 Comparator (java.util.Comparator)5 Collections (java.util.Collections)4 Ignite (org.apache.ignite.Ignite)4 FuzzyCMeansModel (org.apache.ignite.ml.clustering.FuzzyCMeansModel)4 KNNMultipleLinearRegression (org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression)4 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)4 SparseBlockDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)4 Assert.assertEquals (org.junit.Assert.assertEquals)4