2013-07-02 110 views
0

我想了解計算矩陣中向量對之間的Jaccard相似度的示例here爲什麼映射對會被刪除?

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)對在什麼時候消失了?

回答

2

是的,我不知道什麼燙傷,但這似乎很奇怪。如果你看zip執行它mentions specifically,它做一個外部連接保留任何一方的零。因此,該評論似乎並不適用於matrix.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。看來評論與該版本相關,其中要比較的向量位於兩個不一定具有相同大小的單獨矩陣中。