Exemplo n.º 1
0
    def test_count_reduce_items(self):
        from _numpypy import count_reduce_items, arange

        a = arange(24).reshape(2, 3, 4)
        assert count_reduce_items(a) == 24
        assert count_reduce_items(a, 1) == 3
        assert count_reduce_items(a, (1, 2)) == 3 * 4
Exemplo n.º 2
0
    def test_count_reduce_items(self):
        from _numpypy import count_reduce_items, arange

        a = arange(24).reshape(2, 3, 4)
        assert count_reduce_items(a) == 24
        assert count_reduce_items(a, 1) == 3
        assert count_reduce_items(a, (1, 2)) == 3 * 4
        raises(ValueError, count_reduce_items, a, -4)
        raises(ValueError, count_reduce_items, a, (0, 2, -4))
Exemplo n.º 3
0
def _mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False):
    arr = asanyarray(a)

    # Upgrade bool, unsigned int, and int to float64
    if dtype is None and arr.dtype.kind in ['b','u','i']:
        ret = um.add.reduce(arr, axis=axis, dtype='f8',
                            out=out, skipna=skipna, keepdims=keepdims)
    else:
        ret = um.add.reduce(arr, axis=axis, dtype=dtype,
                            out=out, skipna=skipna, keepdims=keepdims)
    rcount = mu.count_reduce_items(arr, axis=axis,
                            skipna=skipna, keepdims=keepdims)
    if isinstance(ret, mu.ndarray):
        ret = um.true_divide(ret, rcount,
                        casting='unsafe', subok=False)
    else:
        ret = ret / float(rcount)
    return ret
Exemplo n.º 4
0
def _var(a, axis=None, dtype=None, out=None, ddof=0,
                            skipna=False, keepdims=False):
    arr = asanyarray(a)

    # First compute the mean, saving 'rcount' for reuse later
    if dtype is None and arr.dtype.kind in ['b','u','i']:
        arrmean = um.add.reduce(arr, axis=axis, dtype='f8',
                            skipna=skipna, keepdims=True)
    else:
        arrmean = um.add.reduce(arr, axis=axis, dtype=dtype,
                            skipna=skipna, keepdims=True)
    rcount = mu.count_reduce_items(arr, axis=axis,
                            skipna=skipna, keepdims=True)
    if isinstance(arrmean, mu.ndarray):
        arrmean = um.true_divide(arrmean, rcount,
                                  casting='unsafe', subok=False)
    else:
        arrmean = arrmean / float(rcount)

    # arr - arrmean
    x = arr - arrmean

    # (arr - arrmean) ** 2
    if arr.dtype.kind == 'c':
        x = um.multiply(x, um.conjugate(x)).real
    else:
        x = um.multiply(x, x)

    # add.reduce((arr - arrmean) ** 2, axis)
    ret = um.add.reduce(x, axis=axis, dtype=dtype, out=out,
                        skipna=skipna, keepdims=keepdims)

    # add.reduce((arr - arrmean) ** 2, axis) / (n - ddof)
    if not keepdims and isinstance(rcount, mu.ndarray):
        rcount = rcount.squeeze(axis=axis)
    rcount -= ddof
    if isinstance(ret, mu.ndarray):
        ret = um.true_divide(ret, rcount,
                        casting='unsafe', subok=False)
    else:
        ret = ret / float(rcount)

    return ret
Exemplo n.º 5
0
 def test_count_reduce_items(self):
     from _numpypy import count_reduce_items, arange
     a = arange(24).reshape(2, 3, 4)
     assert count_reduce_items(a) == 24
     assert count_reduce_items(a, 1) == 3
     assert count_reduce_items(a, (1, 2)) == 3 * 4