numexpr
numexpr copied to clipboard
Add minimum, maximum functions
From [email protected] on June 07, 2012 16:43:59
It would be useful to add np.mininum and np.maximum functions. 'maximum(x-a, 0)' would be faster and clearer than 'where(x-a>0,x-a,0)
Original issue: http://code.google.com/p/numexpr/issues/detail?id=86
This would be extremely useful, especially when searching a "nearest value", eg: abs(x-val)==min(abs(x-val)).
In the context of min and max I would also suggest to implement clip. While clip(a,b,c) is equivalent to min(max(a,b),c), the former is easier to read.
+1 for minimum and clip
But at least i am happy to read that i can use this where function.
Just found this thread still open. Any progress? Interested in a np.minimum() and np.maximum() style element wise min / max!
Min and max were implemented awhile ago. There is no room left for more opcodes, so clip cannot be implemented in NumExpr 2.7. I do believe numexpr3 could have clip implemented.
Really! That's super nice. How can we use it?
I tried all this sentences:
a = numpy.array([2,3,4])
b = numpy.array([3,2,4])
> numexpr.evaluate("min(a,2)")
ValueError: reduction axis is out of bounds
> numexpr.evaluate("min(a,b)")
TypeError: You can't use Python's standard boolean operators in NumExpr expressions.
You should use their bitwise counterparts instead: '&' instead of 'and', '|' instead of 'or',
and '~' instead of 'not'.
> numexpr.evaluate("minimum(a,2)")
TypeError: 'VariableNode' object is not callable
> numexpr.evaluate("a.min(b)")
AttributeError: 'VariableNode' object has no attribute 'min'
> numexpr.version.version
'2.7.1'
Oh, sorry, they are reductions.
Element-wise could be done for float32 and float64 but not for integers.
Hi,
Sorry if I misunderstood: does it mean that element-wise min and max should work with float32 and float64 arrays? I tried @vallsv expressions with a and b as float arrays, but got the same issue as for integer.
Are element-wise min and max implemented, and do you have an example?
Thanks
Element-wise are not implemented at this time. However, you can use the where function instead. It's just uglier.
Really! That's super nice. How can we use it?
I tried all this sentences:
a = numpy.array([2,3,4]) b = numpy.array([3,2,4]) > numexpr.evaluate("min(a,2)") ValueError: reduction axis is out of bounds > numexpr.evaluate("min(a,b)") TypeError: You can't use Python's standard boolean operators in NumExpr expressions. You should use their bitwise counterparts instead: '&' instead of 'and', '|' instead of 'or', and '~' instead of 'not'. > numexpr.evaluate("minimum(a,2)") TypeError: 'VariableNode' object is not callable > numexpr.evaluate("a.min(b)") AttributeError: 'VariableNode' object has no attribute 'min' > numexpr.version.version '2.7.1'
Hi, Is there a way to use all these in the current version of numexpr? I tried these and same error appears.
Such as this:
import numexpr as ne
import numpy as np
a = np.array([2,3,4])
b = np.array([3,2,4])
out1 = ne.evaluate('where(a < 2, a, 2)')
out2 = ne.evaluate('where(a < b, a, b)')
Thanks, @robbmcleod. I somehow missed that function earlier.
Is there a way to define your own operator in numexpr? For eg., if you want to do multiple operations on two parameters in a user-defined function. If there is a post somewhere or some documentation, can someone point to it? Any suggestions is welcome.
No, I'd suggest you look at numba for such a task.
I am not sure how to use numba with numexpr.evaluate and user-defined function.
Let me explain my issue with numexpr.evaluate in detail:
I have a string function in the form with data in variables A and B in data dictionary form:
def ufunc(A,B): ............ return var
The evaluation function goes like this:
numexpr.evaluate( 'A + B**2 + A*B + sin(A) + ufunc(A,B)' , data_dict)
The problem is I don't how to send the 'ufunc' information to 'numexpr.evaluate'. I am not sure how 'numba' will help with that.
I was evaluating this library and not having min or max operator is a big negative aspect... is there any plan to implement them? It's very strange not using min or max in the formula and have to use the where function.
Message to comment on stale issues. If none provided, will not mark issues stale