def test_density(self): # Check that the integral of the density equals 1. n = 100 v = dpnp.random.rand(n) a, b = dpnp.histogram(v, density=True) area = dpnp.sum(a * dpnp.diff(b)[0])[0] numpy.testing.assert_almost_equal(area, 1) # Check with non-constant bin widths v = dpnp.arange(10) bins = [0, 1, 3, 6, 10] a, b = dpnp.histogram(v, bins, density=True) numpy.testing.assert_array_equal(a, .1) numpy.testing.assert_equal(dpnp.sum(a * dpnp.diff(b))[0], 1) # Test that passing False works too a, b = dpnp.histogram(v, bins, density=False) numpy.testing.assert_array_equal(a, [1, 2, 3, 4]) # Variable bin widths are especially useful to deal with # infinities. v = dpnp.arange(10) bins = [0, 1, 3, 6, numpy.inf] a, b = dpnp.histogram(v, bins, density=True) numpy.testing.assert_array_equal(a, [.1, .1, .1, 0.]) # Taken from a bug report from N. Becker on the numpy-discussion # mailing list Aug. 6, 2010. counts, dmy = dpnp.histogram([1, 2, 3, 4], [0.5, 1.5, numpy.inf], density=True) numpy.testing.assert_equal(counts, [.25, 0])
def test_sum_axis(): a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]]) ia = dpnp.array(a) result = dpnp.sum(ia, axis=1) expected = numpy.sum(a, axis=1) numpy.testing.assert_array_equal(expected, result)
def test_sum_int(): a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9]) ia = dpnp.array(a) result = dpnp.sum(ia) expected = numpy.sum(a) numpy.testing.assert_array_equal(expected, result)
def test_sum_float64(): a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]]) ia = dpnp.array(a) for axis in range(len(a)): result = dpnp.sum(ia, axis=axis) expected = numpy.sum(a, axis=axis) numpy.testing.assert_array_equal(expected, result)
def test_simple(self): n = 100 v = dpnp.random.rand(n) (a, b) = dpnp.histogram(v) # check if the sum of the bins equals the number of samples numpy.testing.assert_equal(dpnp.sum(a, axis=0), n) # check that the bin counts are evenly spaced when the data is from # a linear function (a, b) = dpnp.histogram(numpy.linspace(0, 10, 100)) numpy.testing.assert_array_equal(a, 10)
def sum(self, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True): """ Returns the sum along a given axis. .. seealso:: :obj:`dpnp.sum` for full documentation, :meth:`dpnp.dparray.sum` """ return dpnp.sum(self, axis, dtype, out, keepdims, initial, where)