Java Code Examples for org.nd4j.linalg.factory.Nd4j#sort()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#sort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridExecutionerTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReverseFlow3() throws Exception {
    INDArray toSort = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray ascending = Nd4j.sort(toSort.dup(), 1, true);
    assertEquals(toSort, ascending);

    INDArray columnSorted = Nd4j.create(new float[]{2, 1, 4, 3}, new int[]{2, 2});

    // in this particular code point, toSort.dup() isn't executed, but queued
    INDArray dupd = toSort.dup();

    // if we execute muli - dup() op will be flushed as metaOp
    //dupd.muli(1.0);

    INDArray sorted = Nd4j.sort(dupd, 1, false);
    assertEquals(columnSorted, sorted);
}
 
Example 2
Source File: RandomTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Uses a test of Gaussianity for testing the values out of GaussianDistribution
 * See https://en.wikipedia.org/wiki/Anderson%E2%80%93Darling_test
 *
 * @throws Exception
 */
@Test
public void testAndersonDarling() throws Exception {

    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    INDArray z1 = Nd4j.create(1000);

    GaussianDistribution op1 = new GaussianDistribution(z1, 0.0, 1.0);
    Nd4j.getExecutioner().exec(op1, random1);

    val n = z1.length();
    //using this just for the cdf
    Distribution nd = new NormalDistribution(random1, 0.0, 1.0);
    Nd4j.sort(z1, true);

    System.out.println("Data for Anderson-Darling: " + z1);

    for (int i = 0; i < n; i++) {

        Double res = nd.cumulativeProbability(z1.getDouble(i));
        assertTrue (res >= 0.0);
        assertTrue (res <= 1.0);
        // avoid overflow when taking log later.
        if (res == 0) res = 0.0000001;
        if (res == 1) res = 0.9999999;
        z1.putScalar(i, res);
    }

    double A = 0.0;
    for (int i = 0; i < n; i++) {

        A -= (2*i+1) * (Math.log(z1.getDouble(i)) + Math.log(1-z1.getDouble(n - i - 1)));
    }

    A = A / n - n;
    A *= (1 + 4.0/n - 25.0/(n*n));

    assertTrue("Critical (max) value for 1000 points and confidence α = 0.0001 is 1.8692, received: "+ A, A < 1.8692);
}
 
Example 3
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortWithIndicesDescending() {
    INDArray toSort = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    //indices,data
    INDArray[] sorted = Nd4j.sortWithIndices(toSort.dup(), 1, false);
    INDArray sorted2 = Nd4j.sort(toSort.dup(), 1, false);
    assertEquals(sorted[1], sorted2);
    INDArray shouldIndex = Nd4j.create(new float[] {1, 1, 0, 0}, new long[] {2, 2});
    assertEquals(getFailureMessage(), shouldIndex, sorted[0]);


}
 
Example 4
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortWithIndices() {
    INDArray toSort = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    //indices,data
    INDArray[] sorted = Nd4j.sortWithIndices(toSort.dup(), 1, true);
    INDArray sorted2 = Nd4j.sort(toSort.dup(), 1, true);
    assertEquals(sorted[1], sorted2);
    INDArray shouldIndex = Nd4j.create(new float[] {0, 0, 1, 1}, new long[] {2, 2});
    assertEquals(getFailureMessage(), shouldIndex, sorted[0]);


}
 
Example 5
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public Number percentileNumber(Number quantile) {
    if (quantile.intValue() < 0 || quantile.intValue() > 100)
        throw new ND4JIllegalStateException("Percentile value should be in 0...100 range");

    if (isScalar())
        return this.getDouble(0);

    INDArray sorted = Nd4j.sort(this.dup(this.ordering()), true);

    return getPercentile(quantile, sorted);
}
 
Example 6
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public Number percentileNumber(Number quantile) {
    if (quantile.intValue() < 0 || quantile.intValue() > 100)
        throw new ND4JIllegalStateException("Percentile value should be in 0...100 range");

    if (isScalar())
        return this.getDouble(0);

    INDArray sorted = Nd4j.sort(this.dup(this.ordering()), true);

    return getPercentile(quantile, sorted);
}
 
Example 7
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
     * Uses a test of Gaussianity for testing the values out of GaussianDistribution
     * See https://en.wikipedia.org/wiki/Anderson%E2%80%93Darling_test
     *
     * @throws Exception
     */
    @Test
    public void testAndersonDarling() {

        Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
        INDArray z1 = Nd4j.create(1000);

        GaussianDistribution op1 = new GaussianDistribution(z1, 0.0, 1.0);
        Nd4j.getExecutioner().exec(op1, random1);

        val n = z1.length();
        //using this just for the cdf
        Distribution nd = new NormalDistribution(random1, 0.0, 1.0);
        Nd4j.sort(z1, true);

//        System.out.println("Data for Anderson-Darling: " + z1);

        for (int i = 0; i < n; i++) {

            Double res = nd.cumulativeProbability(z1.getDouble(i));
            assertTrue (res >= 0.0);
            assertTrue (res <= 1.0);
            // avoid overflow when taking log later.
            if (res == 0) res = 0.0000001;
            if (res == 1) res = 0.9999999;
            z1.putScalar(i, res);
        }

        double A = 0.0;
        for (int i = 0; i < n; i++) {

            A -= (2*i+1) * (Math.log(z1.getDouble(i)) + Math.log(1-z1.getDouble(n - i - 1)));
        }

        A = A / n - n;
        A *= (1 + 4.0/n - 25.0/(n*n));

        assertTrue("Critical (max) value for 1000 points and confidence α = 0.0001 is 1.8692, received: "+ A, A < 1.8692);
    }
 
Example 8
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortWithIndicesDescending() {
    INDArray toSort = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    //indices,data
    INDArray[] sorted = Nd4j.sortWithIndices(toSort.dup(), 1, false);
    INDArray sorted2 = Nd4j.sort(toSort.dup(), 1, false);
    assertEquals(sorted[1], sorted2);
    INDArray shouldIndex = Nd4j.create(new double[] {1, 1, 0, 0}, new long[] {2, 2});
    assertEquals(getFailureMessage(), shouldIndex, sorted[0]);
}
 
Example 9
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortWithIndices() {
    INDArray toSort = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    //indices,data
    INDArray[] sorted = Nd4j.sortWithIndices(toSort.dup(), 1, true);
    INDArray sorted2 = Nd4j.sort(toSort.dup(), 1, true);
    assertEquals(sorted[1], sorted2);
    INDArray shouldIndex = Nd4j.create(new double[] {0, 0, 1, 1}, new long[] {2, 2});
    assertEquals(getFailureMessage(), shouldIndex, sorted[0]);
}
 
Example 10
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Number percentileNumber(Number quantile) {
    validateNumericalArray("percentileNumber", false);
    if (quantile.intValue() < 0 || quantile.intValue() > 100)
        throw new ND4JIllegalStateException("Percentile value should be in 0...100 range");

    if (isScalar())
        return this.getDouble(0);

    INDArray sorted = Nd4j.sort(this.dup(this.ordering()), true);

    return getPercentile(quantile, sorted);
}
 
Example 11
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testNd4jSortScalar() {
    INDArray linspace = Nd4j.linspace(1, 8, 8);
    INDArray sorted = Nd4j.sort(linspace, 1, false);
    System.out.println(sorted);
}
 
Example 12
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSortDeadlock() {
    val toSort = Nd4j.linspace(DataType.DOUBLE, 1, 32*768, 1).reshape(32, 768);

    val sorted = Nd4j.sort(toSort.dup(), 1, false);
}
 
Example 13
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testNd4jSortScalar() {
        INDArray linspace = Nd4j.linspace(1, 8, 8, DataType.DOUBLE).reshape(1, -1);
        INDArray sorted = Nd4j.sort(linspace, 1, false);
//        System.out.println(sorted);
    }