Numpy:基于关联对值进行分组/装箱
Numpy : Grouping/ binning values based on associations
Forgive me for a vague title. I honestly don't know which title will suit this question. If you have a better title, let's change it so that it will be apt for the problem at hand.
问题。
假设 result
是一个二维数组,values
是一个一维数组。 values
保存一些与 result
中每个元素关联的值。 values
中的元素到 result
的映射存储在 x_mapping
和 y_mapping
中。 result
中的位置可以与不同的值相关联。现在,我必须找到按关联分组的值的总和。
一个更好说明的例子。
result
数组:
[[0, 0],
[0, 0],
[0, 0],
[0, 0]]
values
数组:
[ 1., 2., 3., 4., 5., 6., 7., 8.]
注:此处result
和values
的元素个数相同。但事实可能并非如此。尺寸之间完全没有关系。
x_mapping
和 y_mapping
具有从一维 values
到二维 result
的映射。 x_mapping
、y_mapping
和 values
的大小将相同。
x_mapping
- [0, 1, 0, 0, 0, 0, 0, 0]
y_mapping
- [0, 3, 2, 2, 0, 3, 2, 1]
此处,第一个值 (values[0]
) 的 x 为 0,y 为 0(x_mapping[0]
和 y_mappping[0]
),因此与 result[0, 0]
相关联。如果我们计算关联的数量,那么 result[0,0]
处的元素值将为 2,因为第一个值和第五个值与 result[0, 0]
关联。如果我们要求和,result[0, 0] = value[0] + value[4]
就是 6.
当前解决方案
# Initialisation. No connection with the solution.
result = np.zeros([4,2], dtype=np.int16)
values = np.linspace(start=1, stop=8, num=8)
y_mapping = np.random.randint(low=0, high=values.shape[0], size=values.shape[0])
x_mapping = np.random.randint(low=0, high=values.shape[1], size=values.shape[0])
# Summing the values associated with x,y (current solution.)
for i in range(values.size):
x = x_mapping[i]
y = y_mapping[i]
result[-y, x] = result[-y, x] + values[i]
result
,
[[6, 0],
[ 6, 2],
[14, 0],
[ 8, 0]]
解决方案失败;但是为什么?
test_result = np.zeros_like(result)
test_result[-y_mapping, x_mapping] = test_result[-y_mapping, x_mapping] + values # solution
令我惊讶的是 test_result
中的元素被覆盖了。 test_result
、
处的值
[[5, 0],
[6, 2],
[7, 0],
[8, 0]]
问题
1。为什么在第二种解决方案中,每个元素都被覆盖了?
正如@Divakar 在他的回答中的评论中指出的那样 -
当索引在 test_result[-y_mapping, x_mapping] =
中重复时,NumPy 不会分配 accumulated/summed 值。它从其中一个实例中随机分配。
2。有没有任何 Numpy 方法可以做到这一点?那是没有循环?我正在寻找一些速度优化。
@Divakar 回答中的方法 #2 给了我很好的结果。对于 23315 个关联,for
循环耗时 50 毫秒,而方法 #1 耗时 1.85 毫秒。击败所有这些,方法 #2 花费了 668 微秒。
旁注
我在 i7 处理器上使用 Numpy 版本 1.14.3 和 Python 3.5.2。
我猜你会写
y_mapping = np.random.randint(low=0, high=result.shape[0], size=values.shape[0])
x_mapping = np.random.randint(low=0, high=result.shape[1], size=values.shape[0])
通过该更正,代码按预期工作。
方法 #1
对于那些重复的索引,最直观的方法是 np.add.at
-
np.add.at(result, [-y_mapping, x_mapping], values)
方法 #2
由于 x,y 索引可能重复的性质,我们需要执行分箱求和。因此,另一种方法可能是使用 NumPy 的分箱求和函数:np.bincount
并有一个像这样的实现 -
# Get linear index equivalents off the x and y indices into result array
m,n = result.shape
out_dtype = result.dtype
lidx = ((-y_mapping)%m)*n + x_mapping
# Get binned summations off values based on linear index as bins
binned_sums = np.bincount(lidx, values, minlength=m*n)
# Finally add into result array
result += binned_sums.astype(result.dtype).reshape(m,n)
如果您总是从 result
的零数组开始,最后一步可以使用 -
提高性能
result = binned_sums.astype(out_dtype).reshape(m,n)
Forgive me for a vague title. I honestly don't know which title will suit this question. If you have a better title, let's change it so that it will be apt for the problem at hand.
问题。
假设 result
是一个二维数组,values
是一个一维数组。 values
保存一些与 result
中每个元素关联的值。 values
中的元素到 result
的映射存储在 x_mapping
和 y_mapping
中。 result
中的位置可以与不同的值相关联。现在,我必须找到按关联分组的值的总和。
一个更好说明的例子。
result
数组:
[[0, 0],
[0, 0],
[0, 0],
[0, 0]]
values
数组:
[ 1., 2., 3., 4., 5., 6., 7., 8.]
注:此处result
和values
的元素个数相同。但事实可能并非如此。尺寸之间完全没有关系。
x_mapping
和 y_mapping
具有从一维 values
到二维 result
的映射。 x_mapping
、y_mapping
和 values
的大小将相同。
x_mapping
- [0, 1, 0, 0, 0, 0, 0, 0]
y_mapping
- [0, 3, 2, 2, 0, 3, 2, 1]
此处,第一个值 (values[0]
) 的 x 为 0,y 为 0(x_mapping[0]
和 y_mappping[0]
),因此与 result[0, 0]
相关联。如果我们计算关联的数量,那么 result[0,0]
处的元素值将为 2,因为第一个值和第五个值与 result[0, 0]
关联。如果我们要求和,result[0, 0] = value[0] + value[4]
就是 6.
当前解决方案
# Initialisation. No connection with the solution.
result = np.zeros([4,2], dtype=np.int16)
values = np.linspace(start=1, stop=8, num=8)
y_mapping = np.random.randint(low=0, high=values.shape[0], size=values.shape[0])
x_mapping = np.random.randint(low=0, high=values.shape[1], size=values.shape[0])
# Summing the values associated with x,y (current solution.)
for i in range(values.size):
x = x_mapping[i]
y = y_mapping[i]
result[-y, x] = result[-y, x] + values[i]
result
,
[[6, 0],
[ 6, 2],
[14, 0],
[ 8, 0]]
解决方案失败;但是为什么?
test_result = np.zeros_like(result)
test_result[-y_mapping, x_mapping] = test_result[-y_mapping, x_mapping] + values # solution
令我惊讶的是 test_result
中的元素被覆盖了。 test_result
、
[[5, 0],
[6, 2],
[7, 0],
[8, 0]]
问题
1。为什么在第二种解决方案中,每个元素都被覆盖了?
正如@Divakar 在他的回答中的评论中指出的那样 -
当索引在 test_result[-y_mapping, x_mapping] =
中重复时,NumPy 不会分配 accumulated/summed 值。它从其中一个实例中随机分配。
2。有没有任何 Numpy 方法可以做到这一点?那是没有循环?我正在寻找一些速度优化。
@Divakar 回答中的方法 #2 给了我很好的结果。对于 23315 个关联,for
循环耗时 50 毫秒,而方法 #1 耗时 1.85 毫秒。击败所有这些,方法 #2 花费了 668 微秒。
旁注
我在 i7 处理器上使用 Numpy 版本 1.14.3 和 Python 3.5.2。
我猜你会写
y_mapping = np.random.randint(low=0, high=result.shape[0], size=values.shape[0])
x_mapping = np.random.randint(low=0, high=result.shape[1], size=values.shape[0])
通过该更正,代码按预期工作。
方法 #1
对于那些重复的索引,最直观的方法是 np.add.at
-
np.add.at(result, [-y_mapping, x_mapping], values)
方法 #2
由于 x,y 索引可能重复的性质,我们需要执行分箱求和。因此,另一种方法可能是使用 NumPy 的分箱求和函数:np.bincount
并有一个像这样的实现 -
# Get linear index equivalents off the x and y indices into result array
m,n = result.shape
out_dtype = result.dtype
lidx = ((-y_mapping)%m)*n + x_mapping
# Get binned summations off values based on linear index as bins
binned_sums = np.bincount(lidx, values, minlength=m*n)
# Finally add into result array
result += binned_sums.astype(result.dtype).reshape(m,n)
如果您总是从 result
的零数组开始,最后一步可以使用 -
result = binned_sums.astype(out_dtype).reshape(m,n)