If function with cellfun (i.e. vectorized code instead of ridiculously slow loop).
Replacing character withing an cell using cellfun
speeding up using cellfunction and arrayfun versus for-loop
Cellfun or For loop
Hi I'm currently doing a assignment and I'm just wonder which one would be quicker. Cellfun or for loop
Hi everyone,
Lately I am using functions like cellfun, arrayfun, etc. all the time to avoid writing loops. I was wondering if this is a good practise.
Is it better or simple loop which is much easier to write and read is a better approach?
In addition a for loop can run in parallel later.
The most readable/maintainable approach will probably be to use a list comprehension:
yy = [ np.exp(xxi) for xxi in xx ]
That relies on numpy.exp to implicitly convert each tuple into a numpy.ndarray, which in turn means that you'll get a list of numpy.ndarrays back rather than a list of tuples. That's probably OK for nearly all purposes, but if you absolutely have to have tuples that's also easy enough to arrange:
yy = [ tuple(np.exp(xxi)) for xxi in xx ]
For some purposes (e.g. to avoid memory bottlenecks) you may prefer to use a generator expression rather than a list comprehension (round brackets instead of square).
MATLAB cells were it's attempt to handle general lists like a real language. But being MATLAB they have to be 2d. But in general, in Python uses lists where MATLAB uses cells. numpy arrays with dtype=object behave similarly, adding multidimensions.
Taking the object array route, I can use frompyfunc to apply this function to elements of a list or array:
In [231]: np.frompyfunc(np.exp,1,1)([(4,2),(1,2,3)])
Out[231]:
array([array([ 54.59815003, 7.3890561 ]),
array([ 2.71828183, 7.3890561 , 20.08553692])], dtype=object)
In [232]: np.frompyfunc(np.exp,1,1)([(4,2),(1,2)])
Out[232]:
array([[54.598150033144236, 7.3890560989306504],
[2.7182818284590451, 7.3890560989306504]], dtype=object)
In the 2nd case the result is (2,2), in the first (2,) shape. That's because of how np.array([...]) handles those 2 inputs.
List comprehensions are just as fast, and probably give better control. Or at least can be more predictable.