Skip to content

Instantly share code, notes, and snippets.

@drorata
Created November 23, 2016 15:31
Show Gist options
  • Save drorata/dd9028c993b676328001c414ce822385 to your computer and use it in GitHub Desktop.
Save drorata/dd9028c993b676328001c414ce822385 to your computer and use it in GitHub Desktop.
In [1]: import numpy as np
In [2]: def myfunc(a,b):
...: if a > b:
...: return a+b
...: else:
...: return float(a) / float(b)
...:
In [3]: vfunc = np.vectorize(myfunc)
In [4]: vfunc([4,1,2,3],[1,2,3,4])
Out[4]: array([5, 0, 0, 0])
In [5]: vfunc([0,1,2,3],[1,2,3,4])
Out[5]: array([ 0. , 0.5 , 0.66666667, 0.75 ])
In [6]: vfunc2 = np.vectorize(myfunc, otypes=[np.float])
In [7]: vfunc2([4,1,2,3],[1,2,3,4])
Out[7]: array([ 5. , 0.5 , 0.66666667, 0.75 ])
In [8]: vfunc2([0,1,2,3],[1,2,3,4])
Out[8]: array([ 0. , 0.5 , 0.66666667, 0.75 ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment