提交 bebe79eb authored 作者: Kumar Krishna Agrawal's avatar Kumar Krishna Agrawal

Updated documentation, added test

上级 4390245e
......@@ -3142,11 +3142,13 @@ def var(input, axis=None, keepdims=False, corrected=False):
corrected : bool
If this is set to True, the 'corrected_two_pass' algorithm is
used to compute the variance.
Refer : http://www.cs.yale.edu/publications/techreports/tr222.pdf
Notes
-----
It uses the two-pass algorithm for more stable results.
By default, uses the two-pass algorithm for more stable results.
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Two-pass_algorithm
Also supports 'corrected_two_pass' algorithm, as mentioned above.
There exist other implementations that are even more stable, but probably
slower.
......
......@@ -3609,16 +3609,16 @@ class T_Join_and_Split(unittest.TestCase):
f = function([a, b], s, mode=self.mode)
v = numpy.zeros((2, 3, 2))
v[:,:,0] = v1
v[:,:,1] = v2
out = f(v1, v2)
v[:,:,1] = v2
out = f(v1, v2)
self.assertTrue(v.shape == out.shape)
self.assertTrue(numpy.all(v == out))
s = stack([a, b], axis=-2)
f = function([a, b], s, mode=self.mode)
v = numpy.zeros((2, 2, 3))
v[:,0,:] = v1
v[:,1,:] = v2
out = f(v1, v2)
v[:,1,:] = v2
out = f(v1, v2)
self.assertTrue(v.shape == out.shape)
self.assertTrue(numpy.all(v == out))
# Testing out-of-bounds axis
......@@ -5437,7 +5437,7 @@ def test_tile():
# Test 1,2,3,4-dimensional cases.
# Test input x has the shape [2], [2, 4], [2, 4, 3], [2, 4, 3, 5].
test_shape = [2, 4, 3, 5]
k = 0
k = 0
for xtype in [vector(), matrix(), tensor3(), tensor4()]:
x = xtype
k = k+1
......@@ -5495,7 +5495,7 @@ def test_tile():
reps_ = r[:k-1]
f = function([x], tile(x, reps_, ndim_))
assert numpy.all( f(x_) == numpy.tile(x_, [1, 1] + reps_))
# error raising test: ndim not specified when reps is vector
reps = ivector()
numpy.testing.assert_raises(ValueError, tile, x, reps)
......@@ -5503,7 +5503,7 @@ def test_tile():
# error raising test: not a integer
for reps in [2.5, fscalar(), fvector()]:
numpy.testing.assert_raises(ValueError, tile, x, reps)
# error raising test: the dimension of reps exceeds 1
reps = imatrix()
numpy.testing.assert_raises(ValueError, tile, x, reps)
......@@ -5514,14 +5514,14 @@ def test_tile():
if k > 1:
ndim = k-1
numpy.testing.assert_raises(ValueError, tile, x, reps, ndim)
# error raising test: reps is list, len(reps) > ndim
r = [2, 3, 4, 5, 6]
reps = r[:k+1]
ndim = k
numpy.testing.assert_raises(ValueError, tile, x, reps, ndim)
# error raising test:
# error raising test:
# reps is tensor.vector and len(reps_value) > ndim,
# reps_value is the real value when excuting the function.
reps = ivector()
......@@ -6319,8 +6319,6 @@ def test_var():
f = function([a], var(a))
a_val = numpy.arange(60).reshape(3, 4, 5)
# print numpy.var(a_val)
# print f(a_val)
assert numpy.allclose(numpy.var(a_val), f(a_val))
f = function([a], var(a, axis=0))
......@@ -6333,7 +6331,13 @@ def test_var():
assert numpy.allclose(numpy.var(a_val, axis=2), f(a_val))
f = function([a], var(a, corrected=True))
assert numpy.allclose(numpy.var(a_val), f(a_val))
mean_a = numpy.mean(a_val)
centered_a = a_val - mean_a
v = numpy.mean(centered_a ** 2)
error = (numpy.mean(centered_a)) ** 2
v = v - error
assert numpy.allclose(v, f(a_val))
class T_sum(unittest.TestCase):
def test_sum_overflow(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论