scala余弦距离

    import breeze.linalg.Vector

    val arr1 = Array(1,2,3,4,5)
    val arr2 = Array(2,3,4,5,6)
    val vec1 = breeze.linalg.Vector.apply(arr1)//spark线性代数库
    val vec2 = breeze.linalg.Vector.apply(arr2)
    
    //余弦距离 consΘ= ∑(Xi * Yi) / ( sqrt(∑Xi²) * sqrt(∑Yi²) )
   def cosineDis(vec1: Vector[Double], vec2: Vector[Double]): Double = {
        val cosineSim = vec1.toDenseVector.dot(vec2.toDenseVector) / (norm(vec1.toDenseVector) * norm(vec2.toDenseVector))
        cosineSim
    }
    
    
    val arr1 = Array(1,2,3,4,5)
    val arr2 = Array(2,3,4,5,6)
    val vec1 = arr1.toVector
    val vec2 = arr2.toVector
    
  //余弦距离 consΘ= ∑(Xi * Yi) / ( sqrt(∑Xi²) * sqrt(∑Yi²) )
  def cosineDis(vec1: scala.Vector[Double], vec2: scala.Vector[Double]): Double = {
    val numerator =vec1.zip(vec2).map(x=>x._1*x._2).reduce(_+_).toDouble  //分子 ∑(Xi * Yi)
    val temp1=math.sqrt(vec1.map(x=>math.pow(x,2)).reduce(_+_))//分母  sqrt(∑Xi²) * sqrt(∑Yi²)
    val temp2=math.sqrt(vec2.map(x=>math.pow(x,2)).reduce(_+_))
    val denominator=temp1*temp2
    val cosineSim=numerator/denominator 
    cosineSim
  }

 

posted @ 2020-09-07 17:38  等木鱼的猫  阅读(230)  评论(0编辑  收藏  举报