[英]Can Python perform vectorized operations?
I want to implement the following Matlab code in Python:我想在 Python 中实现以下 Matlab 代码:
x=1:100;
y=20*log10(x);
I tried using Numpy to do this:我尝试使用 Numpy 来做到这一点:
y = numpy.zeros(x.shape)
for i in range(len(x)):
y[i] = 20*math.log10(x[i])
But this uses a for loop;但这使用了一个 for 循环; is there anyway to do a vectorized operation like in Matlab?有没有像在 Matlab 中那样做矢量化操作? I know for some simple math such as division and multiplication, it's possible.我知道对于一些简单的数学运算,例如除法和乘法,这是可能的。 But what about other more sophisticated operations like logarithm here?但是这里的对数等其他更复杂的操作呢?
y = numpy.log10(numpy.arange(1, 101)) * 20
In [30]: numpy.arange(1, 10)
Out[30]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [31]: numpy.log10(numpy.arange(1, 10))
Out[31]:
array([ 0. , 0.30103 , 0.47712125, 0.60205999, 0.69897 ,
0.77815125, 0.84509804, 0.90308999, 0.95424251])
In [32]: numpy.log10(numpy.arange(1, 10)) * 20
Out[32]:
array([ 0. , 6.02059991, 9.54242509, 12.04119983,
13.97940009, 15.56302501, 16.9019608 , 18.06179974, 19.08485019])
Yep, there certainly is.是的,当然有。
x = numpy.arange(1, 100)
y = 20 * numpy.log10(x)
Numpy has a lot of built-in array operators like log10 . Numpy 有很多内置的数组运算符,例如log10 。 If it's not listed in numpy's documentation and you can't generate it from combining built-in methods, then there's no easy way to do it efficiently.如果它没有在 numpy 的文档中列出,并且您无法通过组合内置方法生成它,那么就没有简单的方法可以有效地做到这一点。 You can implement a C-level function to work on numpy arrays and compile that, but this is a lot more work than one or two lines of Python code.您可以实现一个 C 级函数来处理 numpy 数组并对其进行编译,但这比一两行 Python 代码要多得多。
For your case you almost have the right output already:对于您的情况,您几乎已经有了正确的输出:
y = 20*numpy.log10(x)
You may want to take a look at the Numpy documentation.您可能想查看 Numpy 文档。 This is a good place to start:这是一个很好的起点:
http://docs.scipy.org/doc/numpy/reference/routines.html http://docs.scipy.org/doc/numpy/reference/routines.html
And specifically related to your question:特别与您的问题有关:
http://docs.scipy.org/doc/numpy/reference/routines.math.htmlhttp://docs.scipy.org/doc/numpy/reference/routines.math.html
If you're not trying to do anything complicated, the original code could be implemented this way as well, without requiring the use of numpy, if I'm not mistaken.如果你不想做任何复杂的事情,如果我没记错的话,原始代码也可以这样实现,而不需要使用 numpy。
>>> import math
>>> x = range(1, 101)
>>> y = [ 20 * math.log10(z) for z in x ]
Apart from performing vectorized operation using numpy standard vectorized functions, you can also make your custom vectorized function using numpy.vectorize .除了使用 numpy 标准向量化函数执行向量化操作外,您还可以使用numpy.vectorize制作自定义向量化函数。 Here is one example:这是一个例子:
>>> def myfunc(a, b):
... "Return a-b if a>b, otherwise return a+b"
... if a > b:
... return a - b
... else:
... return a + b
>>>
>>> vfunc = np.vectorize(myfunc)
>>> vfunc([1, 2, 3, 4], 2)
array([3, 4, 1, 2])
As mentioned in documentation, unlike numpy's standard vectorized functions, this won't improve the performance如文档中所述,与 numpy 的标准矢量化函数不同,这不会提高性能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.