alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (HistogramBinTest.java)

This example Java source code file (HistogramBinTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

before, bigdecimal, exception, histogrambin, histogrambintest, indarray, math, test

The HistogramBinTest.java Java example source code

package org.deeplearning4j.ui.weights;

import org.junit.Before;
import org.junit.Test;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;

import java.math.BigDecimal;

import static org.junit.Assert.*;

/**
 * @author raver119@gmail.com
 */
public class HistogramBinTest {

    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void testGetBins() throws Exception {
        INDArray array = Nd4j.create(new double[] {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.0});

        HistogramBin histogram = new HistogramBin.Builder(array)
                .setBinCount(10)
                .build();

        assertEquals(0.1, histogram.getMin(), 0.001);
        assertEquals(1.0, histogram.getMax(), 0.001);

        System.out.println("Result: " + histogram.getBins());

        assertEquals(2, histogram.getBins().getDouble(9), 0.001);
    }

    @Test
    public void testGetData1() throws Exception {
        INDArray array = Nd4j.create(new double[] {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.0});

        HistogramBin histogram = new HistogramBin.Builder(array)
                .setBinCount(10)
                .build();

        assertEquals(0.1, histogram.getMin(), 0.001);
        assertEquals(1.0, histogram.getMax(), 0.001);

        System.out.println("Result: " + histogram.getData());

        assertEquals(10, histogram.getData().size());
    }

    @Test
    public void testGetData2() throws Exception {
        INDArray array = Nd4j.create(new double[] {-1.0f, -0.50f, 0.0f, 0.50f, 1.0f, -1.0f, -0.50f, 0.0f, 0.50f, 1.0f});

        HistogramBin histogram = new HistogramBin.Builder(array)
                .setBinCount(10)
                .build();

        assertEquals(-1.0, histogram.getMin(), 0.001);
        assertEquals(1.0, histogram.getMax(), 0.001);

        System.out.println("Result: " + histogram.getData());

        assertEquals(10, histogram.getData().size());

        assertEquals(2, histogram.getData().get(new BigDecimal("1.00")).get());
    }

    @Test
    public void testGetData4() throws Exception {
        INDArray array = Nd4j.create(new double[] {-1.0f, -0.50f, 0.0f, 0.50f, 1.0f, -1.0f, -0.50f, 0.0f, 0.50f, 1.0f});

        HistogramBin histogram = new HistogramBin.Builder(array)
                .setBinCount(50)
                .build();

        assertEquals(-1.0, histogram.getMin(), 0.001);
        assertEquals(1.0, histogram.getMax(), 0.001);

        System.out.println("Result: " + histogram.getData());

        assertEquals(50, histogram.getData().size());

        assertEquals(2, histogram.getData().get(new BigDecimal("1.00")).get());
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java HistogramBinTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.