Python numpy.frompyfunc() Examples

The following are 30 code examples of numpy.frompyfunc(). 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 also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: misc.py    From xrayutilities with GNU General Public License v2.0 6 votes vote down vote up
def gcd(lst):
    """
    greatest common divisor function using library functions

    Parameters
    ----------
    lst:    array-like
        array of integer values for which the greatest common divisor should be
        determined

    Returns
    -------
    gcd:    int
    """
    if numpy.version.version >= '1.15.0':
        return numpy.gcd.reduce(lst)
    elif sys.version_info >= (3, 5):
        gcdfunc = numpy.frompyfunc(math.gcd, 2, 1)
    else:
        gcdfunc = numpy.frompyfunc(fractions.gcd, 2, 1)
    return numpy.ufunc.reduce(gcdfunc, lst) 
Example #2
Source File: elemwise.py    From attention-lvcsr with MIT License 6 votes vote down vote up
def set_ufunc(self, scalar_op):
        # This is probably a speed up of the implementation
        if isinstance(scalar_op, theano.scalar.basic.Add):
            self.ufunc = numpy.add
        elif isinstance(scalar_op, theano.scalar.basic.Mul):
            self.ufunc = numpy.multiply
        elif isinstance(scalar_op, theano.scalar.basic.Maximum):
            self.ufunc = numpy.maximum
        elif isinstance(scalar_op, theano.scalar.basic.Minimum):
            self.ufunc = numpy.minimum
        elif isinstance(scalar_op, theano.scalar.basic.AND):
            self.ufunc = numpy.bitwise_and
        elif isinstance(scalar_op, theano.scalar.basic.OR):
            self.ufunc = numpy.bitwise_or
        elif isinstance(scalar_op, theano.scalar.basic.XOR):
            self.ufunc = numpy.bitwise_xor
        else:
            self.ufunc = numpy.frompyfunc(scalar_op.impl, 2, 1) 
Example #3
Source File: elemwise.py    From D-VAE with MIT License 6 votes vote down vote up
def set_ufunc(self, scalar_op):
        # This is probably a speed up of the implementation
        if isinstance(scalar_op, theano.scalar.basic.Add):
            self.ufunc = numpy.add
        elif isinstance(scalar_op, theano.scalar.basic.Mul):
            self.ufunc = numpy.multiply
        elif isinstance(scalar_op, theano.scalar.basic.Maximum):
            self.ufunc = numpy.maximum
        elif isinstance(scalar_op, theano.scalar.basic.Minimum):
            self.ufunc = numpy.minimum
        elif isinstance(scalar_op, theano.scalar.basic.AND):
            self.ufunc = numpy.bitwise_and
        elif isinstance(scalar_op, theano.scalar.basic.OR):
            self.ufunc = numpy.bitwise_or
        elif isinstance(scalar_op, theano.scalar.basic.XOR):
            self.ufunc = numpy.bitwise_xor
        else:
            self.ufunc = numpy.frompyfunc(scalar_op.impl, 2, 1) 
Example #4
Source File: test_regression.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_frompyfunc_many_args(self):
        # gh-5672

        def passer(*args):
            pass

        assert_raises(ValueError, np.frompyfunc, passer, 32, 1) 
Example #5
Source File: test_regression.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_frompyfunc_endian(self, level=rlevel):
        # Ticket #503
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #6
Source File: test_regression.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def test_frompyfunc_many_args(self):
        # gh-5672

        def passer(*args):
            pass

        assert_raises(ValueError, np.frompyfunc, passer, 32, 1) 
Example #7
Source File: test_regression.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #8
Source File: test_regression.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_frompyfunc_many_args(self):
        # gh-5672

        def passer(*args):
            pass

        assert_raises(ValueError, np.frompyfunc, passer, 32, 1) 
Example #9
Source File: test_regression.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_frompyfunc_endian(self, level=rlevel):
        # Ticket #503
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #10
Source File: test_regression.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #11
Source File: test_regression.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_frompyfunc_endian(self, level=rlevel):
        """Ticket #503"""
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #12
Source File: test_regression.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_frompyfunc_endian(self):
        # Ticket #503
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #13
Source File: test_regression.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #14
Source File: pytables.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def _maybe_convert(values, val_kind, encoding):
    if _need_convert(val_kind):
        conv = _get_converter(val_kind, encoding)
        # conv = np.frompyfunc(conv, 1, 1)
        values = conv(values)
    return values 
Example #15
Source File: test_regression.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_frompyfunc_endian(self):
        # Ticket #503
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #16
Source File: test_regression.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_frompyfunc_many_args(self):
        # gh-5672

        def passer(*args):
            pass

        assert_raises(ValueError, np.frompyfunc, passer, 32, 1) 
Example #17
Source File: test_regression.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #18
Source File: pytables.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _maybe_convert(values, val_kind, encoding):
    if _need_convert(val_kind):
        conv = _get_converter(val_kind, encoding)
        # conv = np.frompyfunc(conv, 1, 1)
        values = conv(values)
    return values 
Example #19
Source File: test_regression.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #20
Source File: prim_array_reduce.py    From myia with MIT License 5 votes vote down vote up
def pyimpl_array_reduce(fn, array, shp):
    """Implement `array_reduce`."""
    idtype = array.dtype
    ufn = np.frompyfunc(fn, 2, 1)
    delta = len(array.shape) - len(shp)
    if delta < 0:
        raise ValueError("Shape to reduce to cannot be larger than original")

    def is_reduction(ishp, tshp):
        if tshp == 1 and ishp > 1:
            return True
        elif tshp != ishp:
            raise ValueError("Dimension mismatch for reduce")
        else:
            return False

    reduction = [
        (delta + idx if is_reduction(ishp, tshp) else None, True)
        for idx, (ishp, tshp) in enumerate(zip(array.shape[delta:], shp))
    ]

    reduction = [(i, False) for i in range(delta)] + reduction

    for idx, keep in reversed(reduction):
        if idx is not None:
            array = ufn.reduce(array, axis=idx, keepdims=keep)

    if not isinstance(array, np.ndarray):
        # Force result to be ndarray, even if it's 0d
        array = np.array(array)

    array = array.astype(idtype)

    return array 
Example #21
Source File: test_regression.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #22
Source File: test_regression.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def test_frompyfunc_many_args(self):
        # gh-5672

        def passer(*args):
            pass

        assert_raises(ValueError, np.frompyfunc, passer, 32, 1) 
Example #23
Source File: test_regression.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def test_frompyfunc_endian(self, level=rlevel):
        # Ticket #503
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #24
Source File: iterative_imputer.py    From ME-Net with MIT License 5 votes vote down vote up
def _object_dtype_isnan(X):
        return np.frompyfunc(lambda x: x != x, 1, 1)(X).astype(bool)



# TODO: once sklearn is updated to 0.20, we can take this out 
Example #25
Source File: fixes.py    From pyglmnet with MIT License 5 votes vote down vote up
def _object_dtype_isnan(X):
        return np.frompyfunc(lambda x: x != x, 1, 1)(X).astype(bool) 
Example #26
Source File: utilities.py    From CatKit with GNU General Public License v3.0 5 votes vote down vote up
def list_gcd(values):
    """Return the greatest common divisor of a list of values."""
    if isinstance(values[0], float):
        values = np.array(values, dtype=int)

    gcd_func = np.frompyfunc(gcd, 2, 1)
    _gcd = np.ufunc.reduce(gcd_func, values)

    return _gcd 
Example #27
Source File: test_regression.py    From pySINDy with MIT License 5 votes vote down vote up
def test_frompyfunc_nout_0(self):
        # gh-2014

        def f(x):
            x[0], x[-1] = x[-1], x[0]

        uf = np.frompyfunc(f, 1, 0)
        a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        assert_equal(uf(a), ())
        assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) 
Example #28
Source File: test_regression.py    From pySINDy with MIT License 5 votes vote down vote up
def test_frompyfunc_many_args(self):
        # gh-5672

        def passer(*args):
            pass

        assert_raises(ValueError, np.frompyfunc, passer, 32, 1) 
Example #29
Source File: test_regression.py    From pySINDy with MIT License 5 votes vote down vote up
def test_frompyfunc_endian(self):
        # Ticket #503
        from math import radians
        uradians = np.frompyfunc(radians, 1, 1)
        big_endian = np.array([83.4, 83.5], dtype='>f8')
        little_endian = np.array([83.4, 83.5], dtype='<f8')
        assert_almost_equal(uradians(big_endian).astype(float),
                            uradians(little_endian).astype(float)) 
Example #30
Source File: pytables.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def _maybe_convert(values, val_kind, encoding, errors):
    if _need_convert(val_kind):
        conv = _get_converter(val_kind, encoding, errors)
        # conv = np.frompyfunc(conv, 1, 1)
        values = conv(values)
    return values