numpy 中 frompyfunc 和 vectorize 的区别
两者看起来非常相似。它们各自的典型用例是什么?
编辑:正如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 JoshAdel 指出的那样,
vectorize
包装了frompyfunc
。 Vectorize 添加了额外的功能:编辑: 经过一些简短的基准测试,我发现
vectorize
比frompyfunc 慢得多(~50%)
对于大型数组。如果性能对您的应用程序至关重要,请首先对您的用例进行基准测试。`
`
As JoshAdel points out,
vectorize
wrapsfrompyfunc
. Vectorize adds extra features:Edit: After some brief benchmarking, I find that
vectorize
is significantly slower (~50%) thanfrompyfunc
for large arrays. If performance is critical in your application, benchmark your use-case first.`
`
我不确定每个用例的不同用例是什么,但如果您查看源代码(/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
wrapsfrompyfunc
. My reading of the code is mostly thatvectorize
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 thatfrompyfunc
is just a lower level instance ofvectorize
.虽然这两种方法都为您提供了构建自己的 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