Created
June 8, 2011 21:29
-
-
Save actsasgeek/1015457 to your computer and use it in GitHub Desktop.
The distanceTo() method of Instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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