python - Difference between frompyfunc and vectorize in numpy -


what difference between vectorize , frompyfunc in numpy?

both seem similar. typical use case each of them?

edit: joshadel indicates, class vectorize seems built upon frompyfunc. (see the source). still unclear me whether frompyfunc may have use case not covered vectorize...

as joshadel points out, vectorize wraps frompyfunc. vectorize adds features:

  • copies docstring original function
  • allows exclude argument broadcasting rules.
  • returns array of correct dtype instead of dtype=object

edit: after brief benchmarking, find vectorize slower (~50%) frompyfunc large arrays. if performance critical in application, benchmark use-case first.

`

>>> = numpy.indices((3,3)).sum(0)  >>> print a, a.dtype [[0 1 2]  [1 2 3]  [2 3 4]] int32  >>> def f(x,y):     """returns 2 times x plus y"""     return 2*x+y  >>> f_vectorize = numpy.vectorize(f)  >>> f_frompyfunc = numpy.frompyfunc(f, 2, 1) >>> f_vectorize.__doc__ 'returns 2 times x plus y'  >>> f_frompyfunc.__doc__ 'f (vectorized)(x1, x2[, out])\n\ndynamic ufunc based on python function'  >>> f_vectorize(a,2) array([[ 2,  4,  6],        [ 4,  6,  8],        [ 6,  8, 10]])  >>> f_frompyfunc(a,2) array([[2, 4, 6],        [4, 6, 8],        [6, 8, 10]], dtype=object) 

`


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -