我有两个形状分别为(AxN)
和(BxN)
的矩阵,我想得到一个(AxB)
的新矩阵,其中每个元素是每两行之间的曼哈顿距离。你知道吗
我试过使用cdist,但事实上它非常慢,例如,如果我们讨论的是余弦距离,下面的代码对cdist
占用两分钟的相同数据需要4秒钟
dotted = matrix_a.dot(matrix_b.transpose())
matrix_a_norms = np.expand_dims(np.linalg.norm(matrix_a, ord=2, axis=1), axis=0)
matrix_b_norms = np.expand_dims(np.linalg.norm(matrix_b, ord=2, axis=1), axis=0)
norm_mat = matrix_a_norms.transpose().dot(matrix_b_norms)
cosine_distance = np.divide(dotted, norm_mat)
所以我想知道,有没有什么可以快速实现的方法,只与曼哈顿的距离相同?你知道吗
编辑:
我试过sklearn
的成对曼哈顿,它需要和cdist
相同的时间
编辑2:
确切的细节
A=14587
B=30228
N=300
dtype
是float32
使用sklearn.pairwise.manhattan_distances
/cdist(metric='cityblock')
平均需要135秒
目前没有回答
相关问题 更多 >
编程相关推荐