Search in sources :

Example 56 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class VectorCustomStorageExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Vector custom storage API usage example started.");
    System.out.println("\n>>> Creating perpendicular vectors.");
    double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
    double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
    Vector v1 = new VectorCustomStorage(data1);
    Vector v2 = new VectorCustomStorage(data2);
    System.out.println(">>> First vector: " + Arrays.toString(data1));
    System.out.println(">>> Second vector: " + Arrays.toString(data2));
    double dotProduct = v1.dot(v2);
    boolean dotProductIsAsExp = dotProduct == 0;
    System.out.println("\n>>> Dot product of vectors: [" + dotProduct + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
    Vector hypotenuse = v1.plus(v2);
    System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
    double lenSquared1 = v1.getLengthSquared();
    double lenSquared2 = v2.getLengthSquared();
    double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
    boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
    System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
    System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
    System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse + "], equals sum of squared lengths of two original vectors as expected: [" + lenSquaredHypotenuseIsAsExp + "].");
    System.out.println("\n>>> Vector custom storage API usage example completed.");
}
Also used : AbstractVector(org.apache.ignite.ml.math.impls.vector.AbstractVector) Vector(org.apache.ignite.ml.math.Vector)

Example 57 with Vector

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

Example 58 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class SparseVectorExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Sparse vector API usage example started.");
    System.out.println("\n>>> Creating perpendicular sparse vectors.");
    double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
    double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
    Vector v1 = new SparseLocalVector(data1.length, RANDOM_ACCESS_MODE);
    Vector v2 = new SparseLocalVector(data2.length, RANDOM_ACCESS_MODE);
    v1.assign(data1);
    v2.assign(data2);
    System.out.println(">>> First vector: " + Arrays.toString(data1));
    System.out.println(">>> Second vector: " + Arrays.toString(data2));
    double dotProduct = v1.dot(v2);
    boolean dotProductIsAsExp = dotProduct == 0;
    System.out.println("\n>>> Dot product of vectors: [" + dotProduct + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
    Vector hypotenuse = v1.plus(v2);
    System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
    double lenSquared1 = v1.getLengthSquared();
    double lenSquared2 = v2.getLengthSquared();
    double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
    boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
    System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
    System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
    System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse + "], equals sum of squared lengths of two original vectors as expected: [" + lenSquaredHypotenuseIsAsExp + "].");
    System.out.println("\n>>> Sparse vector API usage example completed.");
}
Also used : Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector)

Example 59 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class DatasetWithObviousStructure method generate.

/**
 */
List<Vector> generate(Matrix points) {
    int ptsCnt = points.rowSize();
    // Mass centers of dataset.
    List<Vector> massCenters = new ArrayList<>();
    int centersCnt = centers.size();
    List<Integer> permutation = IntStream.range(0, ptsCnt).boxed().collect(Collectors.toList());
    Collections.shuffle(permutation, rnd);
    Vector[] mc = new Vector[centersCnt];
    Arrays.fill(mc, VectorUtils.zeroes(2));
    int totalCnt = 0;
    int centIdx = 0;
    massCenters.clear();
    for (Integer count : centers.keySet()) {
        for (int i = 0; i < count; i++) {
            Vector pnt = getPoint(count);
            mc[centIdx] = mc[centIdx].plus(pnt);
            points.assignRow(permutation.get(totalCnt), pnt);
            totalCnt++;
        }
        massCenters.add(mc[centIdx].times(1 / (double) count));
        centIdx++;
    }
    return massCenters;
}
Also used : ArrayList(java.util.ArrayList) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 60 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class DatasetWithObviousStructure method getPoint.

/**
 */
private Vector getPoint(Integer cnt) {
    Vector pnt = new DenseLocalOnHeapVector(2).assign(centers.get(cnt));
    // Perturbate point on random value.
    pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
    return pnt;
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)116 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)62 Test (org.junit.Test)29 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)20 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)20 Matrix (org.apache.ignite.ml.math.Matrix)19 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)17 Random (java.util.Random)12 ArrayList (java.util.ArrayList)11 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)10 Arrays (java.util.Arrays)9 Ignite (org.apache.ignite.Ignite)9 SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)9 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)9 UUID (java.util.UUID)8 Collections (java.util.Collections)7 List (java.util.List)7 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)7 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7 HashMap (java.util.HashMap)6