Unnecessary as a decorator with arguments

I tried to draw (a consistent, not the most efficient way to do this, but my question is rather using a decorator) the following function

 @np.vectorize
 def diff_if_bigger(x, y):
     return y - x if y > x else 0

 x = np.array([5.6, 7.0])
 y = 8

 diff_if_bigger(x, y)
 # outputs array([2, 1]) which is not what I want

EDIT : after restarting IPython, the output was ok.

Can anyone explain why the result diff_if_biggergot tansformed into an array np.int, even if the first argument of x here is aray of np.float, as opposed to in doc ????

Now I want to force a float, so I did it

 @np.vectorize('np.float')
 def diff_if_bigger(x, y):
     return y - x if y > x else 0
 # Error !!
 # TypeError: Object is not callable.

 @np.vectorize(otypes='np.float')
 def diff_if_bigger(x, y):
     return y - x if y > x else 0
 # Again error !!
 # TypeError: __init__() takes at least 2 arguments (2 given)


 @np.vectorize(otypes=[np.float])
 def diff_if_bigger(x, y):
     return y - x if y > x else 0
 # Still an error !!
 # TypeError: __init__() takes at least 2 arguments (2 given)

By the way, even this

 vec_diff = np.vectorize(diff_if_bigger, otypes=[np.float])

does not work!!! So what is going on?

EDIT : Actually the latter worked after restarting IPython.

So, after my previous two changes, my question is now twofold:

1 np.vectorize ?

2 IPython?

+5
1

:

>>> import numpy as np
>>> @np.vectorize
... def diff_if_bigger(x, y):
...      return y - x if y > x else 0
...
>>> diff_if_bigger(np.array([5.6,7.0]), 8)
array([ 2.4,  1. ])

, np.vectorize , . otype, new_func = np.vectorize(old_func, otypes=...) functools.partial .

, np.vectorize :

vectorized .

, float float, , float dtype (, else 0.0 pass y = 8.0).

+7

All Articles