在scala中表示欧几里德距离的最简单方法

时间:2015-03-09 18:50:06

标签: arrays scala

我正在Scala中编写一个数据挖掘算法,我想为给定的测试和几个列车实例编写欧几里德距离函数。我有Array[Array[Double]]测试和训练实例。我有一个方法,它针对所有训练实例循环遍历每个测试实例,并计算两者之间的距离(每次迭代选择一个测试和训练实例)并返回Double

比如说,我有以下数据点:

testInstance = Array(Array(3.2, 2.1, 4.3, 2.8))
trainPoints = Array(Array(3.9, 4.1, 6.2, 7.3), Array(4.5, 6.1, 8.3, 3.8), Array(5.2, 4.6, 7.4, 9.8), Array(5.1, 7.1, 4.4, 6.9))

我有一个方法存根(突出显示距离函数),它返回给定测试实例周围的邻居:

def predictClass(testPoints: Array[Array[Double]], trainPoints: Array[Array[Double]], k: Int): Array[Double] = {

    for(testInstance <- testPoints)
    {
        for(trainInstance <- trainPoints) 
        {
            for(i <- 0 to k) 
            {
                distance = euclideanDistanceBetween(testInstance, trainInstance) //need help in defining this function
            }
        }
    }    
    return distance
}

我知道如何将通用欧几里德距离公式编写为:

math.sqrt(math.pow((x1 - y1), 2) + math.pow((x2 - y2), 2))

我有一些伪步骤,关于我希望该方法对函数的基本定义做什么:

def distanceBetween(testInstance: Array[Double], trainInstance: Array[Double]): Double = {
  // subtract each element of trainInstance with testInstance
  // for example, 
  // iteration 1 will do [Array(3.9, 4.1, 6.2, 7.3) - Array(3.2, 2.1, 4.3, 2.8)]
  // i.e. sqrt(3.9-3.2)^2+(4.1-2.1)^2+(6.2-4.3)^2+(7.3-2.8)^2
  // return result
  // iteration 2 will do [Array(4.5, 6.1, 8.3, 3.8) - Array(3.2, 2.1, 4.3, 2.8)]
  // i.e. sqrt(4.5-3.2)^2+(6.1-2.1)^2+(8.3-4.3)^2+(3.8-2.8)^2
  // return result, and so on......
  }

我如何在代码中写这个?

2 个答案:

答案 0 :(得分:8)

因此,您输入的公式仅适用于二维向量。你有四个维度,但你应该编写你的功能以便灵活处理这个问题。请查看this formula

所以你真正想说的是:

for each position i:
  subtract the ith element of Y from the ith element of X
  square it
add all of those up
square root the whole thing

为了使这种更具功能性的编程风格,它更像是:

square root the:
  sum of:
    zip X and Y into pairs
    for each pair, square the difference

所以看起来像:

import math._

def distance(xs: Array[Double], ys: Array[Double]) = {
  sqrt((xs zip ys).map { case (x,y) => pow(y - x, 2) }.sum)
}

val testInstances = Array(Array(5.0, 4.8, 7.5, 10.0), Array(3.2, 2.1, 4.3, 2.8))
val trainPoints = Array(Array(3.9, 4.1, 6.2, 7.3), Array(4.5, 6.1, 8.3, 3.8), Array(5.2, 4.6, 7.4, 9.8), Array(5.1, 7.1, 4.4, 6.9))

distance(testInstances.head, trainPoints.head)
// 3.2680269276736382

至于预测课程,你可以使它更具功能性,但不清楚你想要返回的是什么。您似乎想要预测每个测试实例的类?也许选择与最近的训练点相对应的班级c

def findNearestClasses(testPoints: Array[Array[Double]], trainPoints: Array[Array[Double]]): Array[Int] = {
  testPoints.map { testInstance =>
    trainPoints.zipWithIndex.map { case (trainInstance, c) =>
      c -> distance(testInstance, trainInstance)
    }.minBy(_._2)._1
  }
}    

findNearestClasses(testInstances, trainPoints)
// Array(2, 0)

或许你想要k - 最近的邻居:

def findKNearestClasses(testPoints: Array[Array[Double]], trainPoints: Array[Array[Double]], k: Int): Array[Int] = {
  testPoints.map { testInstance =>
    val distances = 
      trainPoints.zipWithIndex.map { case (trainInstance, c) =>
        c -> distance(testInstance, trainInstance)
      }
    val classes = distances.sortBy(_._2).take(k).map(_._1)
    val classCounts = classes.groupBy(identity).mapValues(_.size)
    classCounts.maxBy(_._2)._1
  }
}    

findKNearestClasses(testInstances, trainPoints)
// Array(2, 1)

答案 1 :(得分:0)

欧式距离的通用公式如下:

math.sqrt(math.pow((x1 - x2), 2) + math.pow((y1 - y2), 2))

您只能将x坐标与x进行比较,将y与y进行比较。