Numpy:实现成对的分钟运算和的有效方法 [英] Numpy: an efficient way to implement sum of pairwise mins operation

查看:50
本文介绍了Numpy:实现成对的分钟运算和的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在两个矩阵之间求和是很容易的,即:

It's easy to do sum of products between two matrices, i.e.:

A = np.random.randn(100, 64)
B = np.random.randn(64, 100)
G = np.dot(A, B)

是否存在类似的方法来对向量之间的成对分钟求和?

Is there a similar approach to do sum of pairwise mins between vectors ?

一种低效的方法是:

# For each row, col vector i,j in A and B respectively
for i in range(A.shape[0]):
    for j in range(B.shape[1]):
        G[i, j] = np.minimum(A[i], B[:,j]).sum()

所以我正在寻找类似的东西:

So I am looking for something like:

G = np.dot(A, B, operation=np.minimum)

目标是避免缓慢的for循环,并利用numpy的BLAS实现.

The goal is to avoid the slow for loops and to take advantage of numpy's BLAS implementation.

一个主要的用例是直方图内核的交集.这篇很棒的博客文章中描述了一个示例: http://blog.datadive.net/histogram-intersection-for-change -detection/

One major use case is the intersection of histograms kernel. An example is described in this great blog post: http://blog.datadive.net/histogram-intersection-for-change-detection/

推荐答案

一种简单的方法是将A扩展到3D并利用

One straight-forward way would be to extend A to 3D and leverage broadcasting -

np.minimum(A[...,None], B).sum(1)

这篇关于Numpy:实现成对的分钟运算和的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆