【发布时间】:2018-10-29 22:03:35
【问题描述】:
我有一个 NumPy 数组:
arr = [[1, 2],
[3, 4]]
我想创建一个新数组,其中包含 arr 到 order 的幂:
# arr_new = [arr^0, arr^1, arr^2, arr^3,...arr^order]
arr_new = [[1, 1, 1, 2, 1, 4, 1, 8],
[1, 1, 3, 4, 9, 16, 27, 64]]
我目前的方法使用for 循环:
# Pre-allocate an array for powers
arr = np.array([[1, 2],[3,4]])
order = 3
rows, cols = arr.shape
arr_new = np.zeros((rows, (order+1) * cols))
# Iterate over each exponent
for i in range(order + 1):
arr_new[:, (i * cols) : (i + 1) * cols] = arr**i
print(arr_new)
是否有更快(即矢量化)的方法来创建数组的幂?
基准测试
感谢@hpaulj、@Divakar 和@Paul Panzer 的解答。我在以下测试数组上对基于循环和基于广播的操作进行了基准测试。
arr = np.array([[1, 2],
[3,4]])
order = 3
arrLarge = np.random.randint(0, 10, (100, 100)) # 100 x 100 array
orderLarge = 10
loop_based 函数是:
def loop_based(arr, order):
# pre-allocate an array for powers
rows, cols = arr.shape
arr_new = np.zeros((rows, (order+1) * cols))
# iterate over each exponent
for i in range(order + 1):
arr_new[:, (i * cols) : (i + 1) * cols] = arr**i
return arr_new
使用hstack 的broadcast_based 函数是:
def broadcast_based_hstack(arr, order):
# Create a 3D exponent array for a 2D input array to force broadcasting
powers = np.arange(order + 1)[:, None, None]
# Generate values (third axis contains array at various powers)
exponentiated = arr ** powers
# Reshape and return array
return np.hstack(exponentiated) # <== using hstack function
使用reshape 的broadcast_based 函数是:
def broadcast_based_reshape(arr, order):
# Create a 3D exponent array for a 2D input array to force broadcasting
powers = np.arange(order + 1)[:, None]
# Generate values (3-rd axis contains array at various powers)
exponentiated = arr[:, None] ** powers
# reshape and return array
return exponentiated.reshape(arr.shape[0], -1) # <== using reshape function
broadcast_based 函数使用累积积 cumprod 和 reshape:
def broadcast_cumprod_reshape(arr, order):
rows, cols = arr.shape
# Create 3D empty array where the middle dimension is
# the array at powers 0 through order
out = np.empty((rows, order + 1, cols), dtype=arr.dtype)
out[:, 0, :] = 1 # 0th power is always 1
a = np.broadcast_to(arr[:, None], (rows, order, cols))
# Cumulatively multiply arrays so each multiplication produces the next order
np.cumprod(a, axis=1, out=out[:,1:,:])
return out.reshape(rows, -1)
在 Jupyter notebook 上,我使用timeit 命令得到了这些结果:
小阵列 (2x2):
%timeit -n 100000 loop_based(arr, order)
7.41 µs ± 174 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit -n 100000 broadcast_based_hstack(arr, order)
10.1 µs ± 137 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit -n 100000 broadcast_based_reshape(arr, order)
3.31 µs ± 61.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit -n 100000 broadcast_cumprod_reshape(arr, order)
11 µs ± 102 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
大型阵列 (100x100):
%timeit -n 1000 loop_based(arrLarge, orderLarge)
261 µs ± 5.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit -n 1000 broadcast_based_hstack(arrLarge, orderLarge)
225 µs ± 4.15 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit -n 1000 broadcast_based_reshape(arrLarge, orderLarge)
223 µs ± 2.16 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit -n 1000 broadcast_cumprod_reshape(arrLarge, orderLarge)
157 µs ± 1.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
结论:
似乎使用reshape 的基于广播的方法对于较小的数组更快。但是,对于大型数组,cumprod 方法的扩展性更好,速度更快。
【问题讨论】:
-
broadcast_based_reshape看起来不对。扩展数组的顺序不同。请关注我的帖子以获取确切的代码。 -
我尝试了两种方法:
reshape- 扩展输入数组arr和不扩展输入数组。似乎它们的运行时间在彼此的误差范围内。我理解为什么要制作更高维的指数数组(np.arange(order+1)[:,None])来强制广播。不知道你为什么还用arr[:, None]。 -
需要扩展两个数组以便稍后使用 reshape,其想法是在输出中根据需要进行排序。我更多地谈论的是输出的正确性,而不是那里的性能。
-
谢谢 - 我意识到我在实施你的方法时犯了错误。固定(我认为)。
-
是的,现在看起来不错。
标签: python arrays numpy vectorization