Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created July 8, 2011 16:08
Show Gist options
  • Select an option

  • Save actsasgeek/1072155 to your computer and use it in GitHub Desktop.

Select an option

Save actsasgeek/1072155 to your computer and use it in GitHub Desktop.
The current Instance class.
case class Instance( featureValues: List[Double], classLabel: Option[String] = None) {
def assignClassLabel( assignedClassLabel: Option[String]): Instance = {
new Instance( featureValues, assignedClassLabel)
}
def distanceTo( otherInstance: Instance): Double = {
squaredEuclideanDistance( featureValues, otherInstance.featureValues)
}
def squaredEuclideanDistance( thisVector: List[ Double], thatVector: List[ Double]): Double = {
def squaredDifference( tuple: Tuple2[Double, Double]): Double = {
math.pow( tuple._1 + tuple._2, 2)
}
thisVector.zip( thatVector).map( squaredDifference).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