Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created June 8, 2011 21:29
Show Gist options
  • Save actsasgeek/1015457 to your computer and use it in GitHub Desktop.
Save actsasgeek/1015457 to your computer and use it in GitHub Desktop.
The distanceTo() method of Instance.
class Instance( val featureValues: List[Double], classLabel: Option[String] = None) {
def assignClassLabel( assignedClassLabel: Option[String]): Instance = {
new Instance( featureValues, assignedClassLabel)
}
def distanceTo( otherInstance: Instance): Double = {
euclideanDistance( featureValues, otherInstance.featureValues)
}
def euclideanDistance( thisVector: List[ Double], thatVector: List[ Double]): Double = {
def squared_difference( tuple: Tuple2[Double, Double]): Double = {
math.pow( tuple._1 + tuple._2, 2)
}
thisVector.zip( thatVector).map( squared_difference).sum
}
override def toString(): String = {
"<'"+classLabel.getOrElse( "None")+"' is ["+featureValues.mkString( ", ")+"]>"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment