简体   繁体   English

计算余弦相似度火花Java

[英]Compute cosine similarity spark java

How to compute cosine similarity between 2 Spark Vector. 如何计算2 Spark Vector之间的余弦相似度。 I am using the new ml package. 我正在使用新的ml软件包。

Spark 2.1.1 火花2.1.1

EDIT: 编辑:

Spark provide RowMatrix which can be used to compute similarity but it accepts mllib.vector not an ml.vector . Spark提供了可用于计算相似度的RowMatrix,但它接受mllib.vector而不是ml.vector

Is there a way to convert Vectors from the different packages? 有没有一种方法可以转换来自不同包的Vector? Is there an implementation that uses ml.vector ? 是否有使用ml.vector的实现?

The easiest way to convert from an mllib vector to an ml vector is to use the Vectors.fromML method, see Vectors documentation . mllib矢量转换为ml矢量的最简单方法是使用Vectors.fromML方法,请参见Vectors文档 Example: 例:

val mlVector = org.apache.spark.ml.linalg.Vectors.dense((Array(1.0,2.0,3.0)))
println(mlVector.getClass())

val mllibVector = org.apache.spark.mllib.linalg.Vectors.fromML(mlVector)
println(mllibVector.getClass())

Gives an output: 给出输出:

class org.apache.spark.ml.linalg.DenseVector
class org.apache.spark.mllib.linalg.DenseVector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM