2

I have an array of numbers, and a list of functions.

I want to run every function on every number to get back a matrix.

Is there a way to do it without slow python for looping / mapping?

import numpy
    arr = numpy.array([1,2,3,4,5])
    fns = [numpy.sin, numpy.cos, numpy.exp]
    results = numpy.zeros(shape=( len(fns), len(arr) ))
    for i, fn in enumerate(fns):
        for j, val in enumerate(arr):
            results[i][j] = fn(val)
    print ('results', results)

I can get rid of one loop with function broadcasting:

results2 = numpy.zeros(shape=( len(fns), len(arr) ))
    for i, fn in enumerate(fns):
        results2[i] = fn(arr)
    print ('results2', results2)

Is there some clever pythonic numpy-ish way to get rid of my second loop?

Perhaps some built in outer-product-ish interaction which is difficult to google?

results3 = numpy.function_outer( fns, arr)
2
  • Numpy does not know how to vectorize fns. You could create a lambda function and use numpy's frompyfunc or apply_along_axis, but this will just marginally slow things down. Commented May 4, 2021 at 9:21
  • I found this link stackoverflow.com/questions/44614254/… that is related to your question. Commented May 4, 2021 at 9:23

1 Answer 1

1

You can use a list comprehension and transform it back to a numpy array like this:

results3 = numpy.array([ fn(arr) for fn in fns])
Sign up to request clarification or add additional context in comments.

6 Comments

This is cleaner to write, but not faster as it's still a for loop.
I cannot agree, since this is a pythonic way, as it was requested.
"Is there a way to do it without slow python for looping / mapping?"
@RandomGuy It depends on what you mean by slow. By my measurement the solution int the answer takes 5.84 µs ± 257 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each). It is a less than half the time compared to the solution with classical loop.
The author expected a numpy function like apply_along_axis to do so, but after some researchs it seems it is simply not possible. Moreover I tried your solution but I did not measure any significant difference between the two execution times, could you please provide a piece of your code so I can compare?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.