我具有以下功能,该功能将直方图交点内核应用于2个数组:
def histogram_intersection_kernel(X, Y):
k = np.array([])
for x_i,y_i in zip(X,Y):
k = np.append(k,np.minimum(x_i,y_i))
return np.sum(k)
现在,假设我具有以下矩阵“ mat”:
[[1,0,0,2,3],
[2,3,4,0,1],
[3,3,5,0,1]]
我想找到一种高效的方法来获取矩阵,该方法是将“ histogram_intersection_kernel”应用于mat中所有行组合的结果。在此示例中为:
[[6,2,2],
[6,10,10],
[2,10,12]]
答案 0 :(得分:0)
将维度扩展到3D
并利用broadcasting
-
np.minimum(a[:,None,:],a[None,:,:]).sum(axis=2)
或者简单地-
np.minimum(a[:,None],a).sum(2)
样品运行-
In [248]: a
Out[248]:
array([[1, 0, 0, 2, 3],
[2, 3, 4, 0, 1],
[3, 3, 5, 0, 1]])
In [249]: np.minimum(a[:,None],a).sum(2)
Out[249]:
array([[ 6, 2, 2],
[ 2, 10, 10],
[ 2, 10, 12]])