【发布时间】:2018-08-12 13:00:00
【问题描述】:
在不对较小数组进行过采样的情况下,对不同大小的数组执行逐元素操作的最快和最 Python 的方法是什么?
例如: 我有一个大数组,A 1000x1000 和一个小数组 B 10x10 我希望 B 中的每个元素都响应数组 B 中的 100x100 元素。不需要任何插值,只需对 B 中的所有 10000 个操作使用相同的元素答:
我可以调整两个数组的大小,使 A 的形状是 B 的倍数。通常它们在所有维度上都是 1:10000 或 1:1000。这些数组表示具有不同分辨率但范围相同的数据样本。
我知道我可以对数组 B 进行过采样,例如通过使用 Kronecker 产品,但保持数组 B 较小会更好,特别是当我的一些数组变得非常大以处理和存储时。我正在使用 xarray 和 dask,但任何 numpy 操作也可以。
我希望这个 sn-p 解释了我想要做什么:
import numpy as np
A = np.random.rand(10,10)
B = np.random.rand(1000,1000)
res = np.shape(B)[0]//np.shape(A)[0]
#I want to add A and B so that each element in A is added to 100x100 elements in B.
#This doesn't work of obvious reasons:
#C = A+B
#This solution sacrifices the resolution of B:
C = A+B[::res,::res]
#These solutions creates an unnecessary large array for the operation(don't they?):
K = np.ones((res,res))
C = np.kron(A, K) + B
C = np.repeat(np.repeat(A,res, axis=0), res, axis=1)+B
我有一种感觉,这个问题一定是以前出现过的,但我找不到任何适用于这种特殊情况的答案。
【问题讨论】:
标签: python arrays numpy dask python-xarray