numpy 中 frompyfunc 和 vectorize 的区别

发布于 2024-11-25 11:00:08 字数 648 浏览 3 评论 0原文

向量化frompyfunc

两者看起来非常相似。它们各自的典型用例是什么?

编辑:正如 JoshAdel 所指出的,vectorize 类似乎是基于 frompyfunc 构建的。 (请参阅来源)。我仍然不清楚 frompyfunc 是否可能有任何 vectorize 未涵盖的用例......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2024-12-02 11:00:08

正如 JoshAdel 指出的那样,vectorize 包装了frompyfunc。 Vectorize 添加了额外的功能:

  • 从原始函数复制文档字符串
  • 允许您从广播规则中排除参数。
  • 返回正确 dtype 的数组,而不是 dtype=object

编辑: 经过一些简短的基准测试,我发现 vectorizefrompyfunc 慢得多(~50%) 对于大型数组。如果性能对您的应用程序至关重要,请首先对您的用例进行基准测试。

`

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

`

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)

`

弥繁 2024-12-02 11:00:08

我不确定每个用例的不同用例是什么,但如果您查看源代码(/numpy/lib/function_base.py),您会发现 vectorize 包装 来自pyfunc。我对代码的阅读主要是向量化正在对输入参数进行正确的处理。在某些特定情况下,您可能会更喜欢其中一种,但似乎 frompyfunc 只是 vectorize 的较低级别实例。

I'm not sure what the different use cases for each is, but if you look at the source code (/numpy/lib/function_base.py), you'll see that vectorize wraps frompyfunc. My reading of the code is mostly that vectorize is doing proper handling of the input arguments. There might be particular instances where you would prefer one vs the other, but it would seem that frompyfunc is just a lower level instance of vectorize.

朦胧时间 2024-12-02 11:00:08

虽然这两种方法都为您提供了构建自己的 ufunc 的方法,但 numpy.frompyfunc 方法始终返回一个 python 对象,而您可以在使用 numpy 时指定返回类型.vectorize 方法

Although both methods provide you a way to build your own ufunc, numpy.frompyfunc method always returns a python object, while you could specify a return type when using numpy.vectorize method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文