What is the difference between vectorize and frompyfunc in numpy?
numpy中vectorize和frompyfunc有什么区别?
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
...
编辑:正如JoshAdel所指出的,类vectorize似乎是建立在frompyfunc之上的。 (见来源)。我仍然不清楚frompyfunc是否有任何vectorize未涵盖的用例......
3 个解决方案
#1
13
As JoshAdel points out, vectorize
wraps frompyfunc
. Vectorize adds extra features:
正如JoshAdel指出的那样,vectorize包含了frompyfunc。 Vectorize增加了额外的功能:
- Copies the docstring from the original function
- 从原始函数复制docstring
- Allows you to exclude an argument from broadcasting rules.
- 允许您从广播规则中排除参数。
- Returns an array of the correct dtype instead of dtype=object
- 返回正确dtype的数组,而不是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.
编辑:经过一些简短的基准测试后,我发现vectorize比大数组的frompyfunc慢得多(~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)
`
`
#2
7
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
.
我不确定每个用例的不同用例是什么,但是如果你看一下源代码(/numpy/lib/function_base.py),你会看到vectorize包含frompyfunc。我对代码的阅读主要是vectorize正在正确处理输入参数。可能有一些特殊情况,你更喜欢一个与另一个,但似乎frompyfunc只是一个较低级别的vectorize实例。
#3
1
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
虽然这两种方法都为您提供了构建自己的ufunc的方法,但是numpy.frompyfunc方法总是返回一个python对象,而在使用numpy.vectorize方法时可以指定返回类型
#1
13
As JoshAdel points out, vectorize
wraps frompyfunc
. Vectorize adds extra features:
正如JoshAdel指出的那样,vectorize包含了frompyfunc。 Vectorize增加了额外的功能:
- Copies the docstring from the original function
- 从原始函数复制docstring
- Allows you to exclude an argument from broadcasting rules.
- 允许您从广播规则中排除参数。
- Returns an array of the correct dtype instead of dtype=object
- 返回正确dtype的数组,而不是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.
编辑:经过一些简短的基准测试后,我发现vectorize比大数组的frompyfunc慢得多(~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)
`
`
#2
7
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
.
我不确定每个用例的不同用例是什么,但是如果你看一下源代码(/numpy/lib/function_base.py),你会看到vectorize包含frompyfunc。我对代码的阅读主要是vectorize正在正确处理输入参数。可能有一些特殊情况,你更喜欢一个与另一个,但似乎frompyfunc只是一个较低级别的vectorize实例。
#3
1
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
虽然这两种方法都为您提供了构建自己的ufunc的方法,但是numpy.frompyfunc方法总是返回一个python对象,而在使用numpy.vectorize方法时可以指定返回类型