在numpy数组上映射函数的最有效方法是什么?我目前正在做:

import numpy as np 

x = np.array([1, 2, 3, 4, 5])

# Obtain array of square of each element in x
squarer = lambda t: t ** 2
squares = np.array([squarer(xi) for xi in x])

然而,这可能非常低效,因为我在将新数组转换回numpy数组之前,使用列表推导式将其构造为Python列表。我们能做得更好吗?


当前回答

编辑:原来的答案是误导性的,np。SQRT直接应用于数组,开销很小。

在多维情况下,您希望应用一个内建函数,操作1d数组numpy。Apply_along_axis是一个不错的选择,对于numpy和scipy中更复杂的函数组合也是如此。

先前的误导性陈述:

添加方法:

def along_axis(x):
    return np.apply_along_axis(f, 0, x)

perfplot代码给出接近np.sqrt的性能结果。

其他回答

博士TL;

正如@user2357112所指出的,应用函数的“直接”方法总是在Numpy数组上映射函数的最快和最简单的方法:

import numpy as np
x = np.array([1, 2, 3, 4, 5])
f = lambda x: x ** 2
squares = f(x)

一般避免np。向量化,因为它性能不佳,并且已经(或曾经)有许多问题。如果您正在处理其他数据类型,您可能需要研究下面所示的其他方法。

方法比较

下面是一些简单的测试,比较三种映射函数的方法,本例使用Python 3.6和NumPy 1.15.4。首先,测试的设置函数:

import timeit
import numpy as np

f = lambda x: x ** 2
vf = np.vectorize(f)

def test_array(x, n):
    t = timeit.timeit(
        'np.array([f(xi) for xi in x])',
        'from __main__ import np, x, f', number=n)
    print('array: {0:.3f}'.format(t))

def test_fromiter(x, n):
    t = timeit.timeit(
        'np.fromiter((f(xi) for xi in x), x.dtype, count=len(x))',
        'from __main__ import np, x, f', number=n)
    print('fromiter: {0:.3f}'.format(t))

def test_direct(x, n):
    t = timeit.timeit(
        'f(x)',
        'from __main__ import x, f', number=n)
    print('direct: {0:.3f}'.format(t))

def test_vectorized(x, n):
    t = timeit.timeit(
        'vf(x)',
        'from __main__ import x, vf', number=n)
    print('vectorized: {0:.3f}'.format(t))

测试五个元素(从最快到最慢排序):

x = np.array([1, 2, 3, 4, 5])
n = 100000
test_direct(x, n)      # 0.265
test_fromiter(x, n)    # 0.479
test_array(x, n)       # 0.865
test_vectorized(x, n)  # 2.906

包含100个元素:

x = np.arange(100)
n = 10000
test_direct(x, n)      # 0.030
test_array(x, n)       # 0.501
test_vectorized(x, n)  # 0.670
test_fromiter(x, n)    # 0.883

并且使用1000个或更多的数组元素:

x = np.arange(1000)
n = 1000
test_direct(x, n)      # 0.007
test_fromiter(x, n)    # 0.479
test_array(x, n)       # 0.516
test_vectorized(x, n)  # 0.945

不同版本的Python/NumPy和编译器优化会有不同的结果,所以对您的环境进行类似的测试。

就像在这篇文章中提到的,像这样使用生成器表达式:

numpy.fromiter((<some_func>(x) for x in <something>),<dtype>,<size of something>)
squares = squarer(x)

数组上的算术运算以元素方式自动应用,高效的c级循环避免了适用于python级循环或理解的所有解释器开销。

您希望应用到NumPy数组elementwise的大多数函数都可以正常工作,尽管有些函数可能需要更改。例如,if不能在元素方面工作。你需要将它们转换为使用numpy.where这样的结构:

def using_if(x):
    if x < 5:
        return x
    else:
        return x**2

就变成了

def using_where(x):
    return numpy.where(x < 5, x, x**2)

使用numpy.fromfunction(function, shape, **kwargs)

看到“https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfunction.html”

我相信在numpy的新版本(我使用1.13)中,您可以简单地通过将numpy数组传递给您为标量类型编写的函数来调用该函数,它将自动应用函数调用到numpy数组上的每个元素,并返回另一个numpy数组

>>> import numpy as np
>>> squarer = lambda t: t ** 2
>>> x = np.array([1, 2, 3, 4, 5])
>>> squarer(x)
array([ 1,  4,  9, 16, 25])