Scala 为什么映射对会被删除?
我试图理解计算矩阵中向量对之间的Jaccard相似性的示例Scala 为什么映射对会被删除?,scala,scalding,Scala,Scalding,我试图理解计算矩阵中向量对之间的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
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 )
为什么最后一条评论提到非零值?据我所知,
\u 2
函数选择一对中独立于第一个元素的第二个元素。什么时候(0,x)
配对被删除了?是的,我对烫伤一无所知,但这似乎很奇怪。如果您查看zip
实现,它会执行外部联接以在任意一侧保留零。因此,该注释似乎不适用于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)中,我们找到行(i)
的元素之和加上行(j)
的元素之和,然后减去intersectMat
,以调整重复计数
编辑:谷歌搜索了一下这篇博文:。这些评论似乎与要比较的向量位于两个不一定大小相同的独立矩阵中的版本有关