我试图理解示例here,它计算矩阵中矢量对之间的Jaccard相似性。
val aBinary = adjacencyMatrix.binarizeAs[Double]
// intersectMat holds the size of the intersection of row(a)_i n row (b)_j
val intersectMat = aBinary * aBinary.transpose
val aSumVct = aBinary.sumColVectors
val bSumVct = aBinary.sumRowVectors
//Using zip to repeat the row and column vectors values on the right hand
//for all non-zeroes on the left hand matrix
val xMat = intersectMat.zip(aSumVct).mapValues( pair => pair._2 )
val yMat = intersectMat.zip(bSumVct).mapValues( pair => pair._2 )
为什么最后一条评论提到非零值?据我所知,._2
函数选择一对独立于第一个元素的第二个元素。在什么时候(0, x)
对被删除?
答案 0 :(得分:2)
是的,我对烫伤一无所知,但这看起来很奇怪。如果你看zip
实现它mentions specifically它会执行外连接以保留任意一侧的零。因此,评论似乎不适用于matrix.zip
中实际处理零的方式。
除了查看zip返回的维度之外,这行似乎只是复制了每列的aSumVct
列向量:
val xMat = intersectMat.zip(aSumVct).mapValues( pair => pair._2 )
此外,我发现val bSumVct = aBinary.sumRowVectors
可疑,因为它沿着错误的维度对矩阵求和。感觉这样的事情会更好:
val bSumVct = aBinary.tranpose.sumRowVectors
这在概念上与aSumVct.transpose
相同,因此在一天结束时,在xMat + yMat
的单元格(i,j)中,我们找到row(i)
的元素总和} 加上 row(j)
元素的总和,然后我们减去intersectMat
来调整重复计算。
编辑:在博客文章http://www.flavianv.me/post-15.htm中发掘了一些谷歌搜索。似乎评论与该版本相关,其中要比较的矢量位于两个不一定具有相同大小的单独矩阵中。