本帖最后由 feilong 于 2018-6-29 11:16 编辑
问题导读
1.什么是文档相关性,如何计算?
2.什么是词文档相关性,如何计算?
3.词和文档是否要先转化成向量?
上一篇:Spark 高级分析:第六章第8,9节 低维度查询和评分以及词相关性
http://www.aboutyun.com/forum.php?mod=viewthread&tid=24699&extra=
同样的情况也适用于计算文档之间的相关性分数。为了找到两个文档之间的相似度,计算
和
之间的余弦相似度,ui是与i对应的U的行,为了找到一个文档和所有其他文档之间的相似性,计算标准化
。
在这种情况下,实现略有不同,因为U是由RDD支持的,而不是本地矩阵。
[mw_shl_code=scala,true]import org.apache.spark.mllib.linalg.Matrices
def topDocsForDoc(normalizedUS: RowMatrix, docId: Long)
: Seq[(Double, Long)] = {
val docRowArr = row(normalizedUS, docId)
val docRowVec = Matrices.dense(docRowArr.length, 1, docRowArr)
val docScores = normalizedUS.multiply(docRowVec)
val allDocWeights = docScores.rows.map(_.toArray(0)).
zipWithUniqueId
allDocWeights.filter(!_._1.isNaN).top(10)
}
val US = multiplyByDiagonalMatrix(svd.U, svd.s)
val normalizedUS = rowsNormalized(US)
def printRelevantDocs(doc: String) {
val id = idDocs(doc)
printIdWeights(topDocsForDoc(normalizedUS, id, docIds)
}
printRelevantDocs("Romania")
...
(Romania,0.9999999999999994), (Roma in Romania,0.9229332158078395), (Kingdom of Romania,0.9176138537751187), printRelevantDocs("Brad Pitt")
...
(Brad Pitt,0.9999999999999984), (Aaron Eckhart,0.8935447577397551), (Leonardo DiCaprio,0.8930359829082504), printRelevantDocs("Radiohead")
...
(Radiohead,1.0000000000000016), (Fightstar,0.9461712602479349), (R.E.M.,0.9456251852095919), (Incubus
[/mw_shl_code]
如何计算一个词语和一个文档之间的关联分数? 这相当于在term-document矩阵的降阶近似中找到与该词语和文档对应的元素。
它等于
,其中ud是对应于文档的U中的行,vt是对应于项的V中的行。一些简单的线性代数操作表明,计算一个词语和每个文档之间的相似性等同于
。结果向量中的每个元素将包含文档和查询词语之间的相似性。在另一个方向上,文档和每个词语之间的相似性来自于
。
[mw_shl_code=scala,true]def topDocsForTerm(US: RowMatrix, V: Matrix, termId: Int)
: Seq[(Double, Long)] = {
val rowArr = row(V, termId).toArray
val rowVec = Matrices.dense(termRowArr.length, 1, termRowArr)
val docScores = US.multiply(termRowVec)
val allDocWeights = docScores.rows.map(_.toArray(0)).
zipWithUniqueId
allDocWeights.top(10)
}
def printRelevantDocs(term: String) {
val id = idTerms(term)
printIdWeights(topDocsForTerm(normalizedUS, svd.V, id, docIds)
}
printRelevantDocs("fir")
...
(Silver tree,0.006292909647173194), (See the forest for the trees,0.004785047583508223), (Eucalyptus printRelevantDocs("graph")
...
(K-factor (graph theory),0.07074443599385992), (Mesh Graph,0.05843133228896666), (Mesh graph,0.05843133228896666
[/mw_shl_code]
|
|