Featured post
python - Numpy vectorize, using lists as arguments -
the numpy vectorize
function useful, doesn't behave when function arguments lists rather scalars. example:
import numpy np def f(x, a): print "type(a)=%s, a=%s"%(type(a),a) return sum(a)/x x = np.linspace(1,2,10) p = [1,2,3] f2 = np.vectorize(f) f(x,p) f2(x,p)
gives:
type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'numpy.int64'>, a=1 traceback (most recent call last): file "vectorize.py", line 14, in <module> f2(x,p) file "/usr/local/lib/python2.6/dist-packages/numpy/lib/function_base.py", line 1824, in __call__ theout = self.thefunc(*newargs) file "vectorize.py", line 5, in f return sum(a)/x typeerror: 'numpy.int64' object not iterable
i understand function f works just fine without vectorize
ing it, i'd know how (in general) vectorize function arguments take in lists rather scalar.
your question doesn't make clear precisely output see vectorized function, i'm going assume same list (a) applied argument every invocation of f() (ie once each element in x array)
the vectorized version of function ensures arguments arrays, , applies numpy's broadcasting rules determine how these arguments should combined.
as np.array's wrapping of np.ndarray, coercion of arguments arrays tries helpful, automatically converting list array containing same elements, rather making array dtype=object contains list sole element. of time want, in case, "smart" behaviour coming bite you.
while there may way instruct numpy treat inputs vectors, there 2 straightforward ways behaviour you're after:
- manually create array dtype=object work within broadcasting rules
- curry value prior vectorizing function
1. dtype=object
numpy arrays derive efficiency storing 1 type of item, can still contain arbitrary python objects specifying stored data type python objects:
list_obj_array = np.ndarray((1,), dtype=object) list_obj_array[0] = [1,2,3] f2(x,list_obj_array) # using definition above
prints:
type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3]
and returns:
array([ 6. , 5.4 , 4.90909091, 4.5 , 4.15384615, 3.85714286, 3.6 , 3.375 , 3.17647059, 3. ])
2. currying
since passing same list function call each item in array, can store list directly function currying before applying vectorization:
def curry_f(a): def f_curried(x): return f(x, a) # using definition above return f_curried f2 = np.vectorize(curry_f(p)) f2(x)
prints:
type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3]
and returns:
array([ 6. , 5.4 , 4.90909091, 4.5 , 4.15384615, 3.85714286, 3.6 , 3.375 , 3.17647059, 3. ])
p.s. may wish have @ np.frompyfunc -- similar vectorize(), works @ lower level.
- Get link
- X
- Other Apps
Comments
Post a Comment