Search in sources :

Example 91 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class VectorUtils method of.

/**
 * Creates vector based on array of Doubles. If array contains null-elements then
 * method returns sparse local on head vector. In other case method returns
 * dense local on heap vector.
 *
 * @param values Values.
 */
public static Vector of(Double[] values) {
    A.notNull(values, "values");
    Vector answer;
    if (Arrays.stream(values).anyMatch(Objects::isNull))
        answer = new SparseVector(values.length);
    else
        answer = new DenseVector(values.length);
    for (int i = 0; i < values.length; i++) if (values[i] != null)
        answer.set(i, values[i]);
    return answer;
}
Also used : Objects(java.util.Objects) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) DelegatingNamedVector(org.apache.ignite.ml.math.primitives.vector.impl.DelegatingNamedVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 92 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class TracerTest method testWriteVectorToCSVFile.

/**
 */
@Test
public void testWriteVectorToCSVFile() throws IOException {
    DenseVector vector = new DenseVector(MathTestConstants.STORAGE_SIZE);
    for (int i = 0; i < vector.size(); i++) vector.set(i, Math.random());
    Path file = createTempFile("vector", ".csv");
    Tracer.saveAsCsv(vector, DEFAULT_FORMAT, file.toString());
    System.out.println("Vector exported: " + file.getFileName());
    List<String> strings = Files.readAllLines(file);
    Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2);
    String[] csvVals = reduce.orElse("").split(",");
    for (int i = 0; i < vector.size(); i++) {
        Double csvVal = Double.valueOf(csvVals[i]);
        assertEquals("Unexpected value.", csvVal, vector.get(i), DEFAULT_DELTA);
    }
    Files.deleteIfExists(file);
}
Also used : Path(java.nio.file.Path) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 93 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class TracerTest method makeRandomVector.

/**
 * @param size Vector size.
 */
private Vector makeRandomVector(int size) {
    DenseVector vec = new DenseVector(size);
    vec.assign((idx) -> Math.random());
    return vec;
}
Also used : DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 94 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class CosineSimilarityTest method cosineSimilarityDistance.

/**
 */
@Test
public void cosineSimilarityDistance() {
    double expRes = 0.9449111825230682d;
    DenseVector a = new DenseVector(new double[] { 1, 2, 3 });
    double[] b = { 1, 1, 4 };
    DistanceMeasure distanceMeasure = new CosineSimilarity();
    assertEquals(expRes, distanceMeasure.compute(a, b), PRECISION);
    assertEquals(expRes, distanceMeasure.compute(a, new DenseVector(b)), PRECISION);
}
Also used : DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 95 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class BlasTest method testAxpyArrayArray.

/**
 * Test 'axpy' operation for two array-based vectors.
 */
@Test
public void testAxpyArrayArray() {
    Vector y = new DenseVector(new double[] { 1.0, 2.0 });
    double a = 2.0;
    Vector x = new DenseVector(new double[] { 1.0, 2.0 });
    Vector exp = x.times(a).plus(y);
    Blas.axpy(a, x, y);
    Assert.assertEquals(y, exp);
}
Also used : SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Aggregations

DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)101 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)59 Test (org.junit.Test)59 Serializable (java.io.Serializable)16 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)14 HashMap (java.util.HashMap)13 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)13 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)10 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)10 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)9 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 HashSet (java.util.HashSet)7 TrainerTest (org.apache.ignite.ml.common.TrainerTest)7 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)5 LocalDatasetBuilder (org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder)5 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)5 IgniteDifferentiableVectorToDoubleFunction (org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction)5 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)5 OneHotEncoderPreprocessor (org.apache.ignite.ml.preprocessing.encoding.onehotencoder.OneHotEncoderPreprocessor)4 Random (java.util.Random)3