Difference between frompyfunc and vectorize in numpy

2024/11/19 13:43:45

What is the difference between vectorize and frompyfunc in numpy?

Both seem very similar. What is a typical use case for each of them?

Edit: As JoshAdel indicates, the class vectorize seems to be built upon frompyfunc. (see the source). It is still unclear to me whether frompyfunc may have any use case that is not covered by vectorize...

Answer

As JoshAdel points out, vectorize wraps frompyfunc. Vectorize adds extra features:

  • Copies the docstring from the original function
  • Allows you to exclude an argument from broadcasting rules.
  • Returns an array of the correct dtype instead of dtype=object

Edit: After some brief benchmarking, I find that vectorize is significantly slower (~50%) than frompyfunc for large arrays. If performance is critical in your application, benchmark your use-case first.

`

>>> a = 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 a 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)

`

https://en.xdnf.cn/q/26438.html

Related Q&A

Jupyter notebook command does not work on Mac

I installed jupyter using pip on my macbook air. Upon trying to execute the command jupyter notebook, I get an error jupyter: notebook is not a Jupyter commandI used the --h option to get a listing of …

Recursively compare two directories to ensure they have the same files and subdirectories

From what I observe filecmp.dircmp is recursive, but inadequate for my needs, at least in py2. I want to compare two directories and all their contained files. Does this exist, or do I need to build …

Specific reasons to favor pip vs. conda when installing Python packages

I use miniconda as my default python installation. What is the current (2019) wisdom regarding when to install something with conda vs. pip?My usual behavior is to install everything with pip, and onl…

Insert a link inside a Pandas table

Id like to insert a link (to a web page) inside a Pandas table, so when it is displayed in an IPython notebook, I could press the link. I tried the following: In [1]: import pandas as pdIn [2]: df = pd…

TypeError: string indices must be integers while parsing JSON using Python?

I am confuse now why I am not able to parse this JSON string. Similar code works fine on other JSON string but not on this one - I am trying to parse JSON String and extract script from the JSON.Below …

Python dynamic inheritance: How to choose base class upon instance creation?

IntroductionI have encountered an interesting case in my programming job that requires me to implement a mechanism of dynamic class inheritance in python. What I mean when using the term "dynamic …

Python: efficiently check if integer is within *many* ranges

I am working on a postage application which is required to check an integer postcode against a number of postcode ranges, and return a different code based on which range the postcode matches against.E…

How to pass on argparse argument to function as kwargs?

I have a class defined as followsclass M(object):def __init__(self, **kwargs):...do_somethingand I have the result of argparse.parse_args(), for example:> args = parse_args() > print args Namespa…

How do you check whether a python method is bound or not?

Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that its bound to?

Most Pythonic way to declare an abstract class property

Assume youre writing an abstract class and one or more of its non-abstract class methods require the concrete class to have a specific class attribute; e.g., if instances of each concrete class can be …