[SciPy-Dev] Seeking help/ advice for applying functions

eat e.antero.tammi@gmail....
Fri Mar 5 17:56:37 CST 2010


"""
Hi,

I would like to ask some advice for a (pretty generic kind of) problem as:

I do have some data which I'll like to put trough several (thousands of)
functions.

I have cooked up a simple demonstration of the problem, and two plausible
solutions for it. However I'm not happy with either 'gproc1' or 'gproc2'.

So, does someone have a more suitable scipy/ numpy solution for this
kind of problem?


Regards,
eat

OK, here is the some code as demonstration:
"""

import numpy as np

data= np.random.random((1, 2))
fncs= np.asarray([np.sin, np.cos, np.tan, np.sinh, np.cosh, np.tanh])
cs= np.asarray(np.random.randint(0, 2, (5, len(fncs))), dtype= bool)

def mid(x): return np.round(x, 3)
def gcntrl(m, cs):
    for k in xrange(m):
        yield cs[k, :]


def gproc1(gcntrl, fncs, data):
    for c in gcntrl:
        yield np.asarray([mid(f(data)) for f in fncs[c]])

def rr(data):
    def f(g): return g(data)
    return f
def gproc2(gcntrl, fncs, data):
    for c in gcntrl:
        yield mid(np.asarray(map(rr(data), fncs[c])))


def a(g, n):
    def p(g, c, cs, f, d):
        for r in g(c(cs.shape[0], cs), f, d):
            print r.squeeze()
    print n+ ' approach:'; p(g, gcntrl, cs, fncs, data); print


if __name__ == '__main__':
    a(gproc1, 'firsth')
    a(gproc2, 'second')
    #??? a(gproc3, 'third')




More information about the SciPy-Dev mailing list