Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created June 8, 2011 19:46
Show Gist options
  • Save actsasgeek/1015211 to your computer and use it in GitHub Desktop.
Save actsasgeek/1015211 to your computer and use it in GitHub Desktop.
Improved version of the Nearest Neighbor "walking skeleton"
class Instance( featureValues: List[Double], classLabel: Option[String] = None) {
def assignClassLabel( assignedClassLabel: Option[String]): Instance = {
new Instance( featureValues, assignedClassLabel)
}
override def toString(): String = {
"<'"+classLabel.getOrElse( "None")+"' is ["+featureValues.mkString( ", ")+"]>"
}
}
class NearestNeighbor( library: List[Instance]) {
def classify( query: Instance): Instance = {
query.assignClassLabel( Some( "Unknown"))
}
}
object NearestNeighbor {
def create( libraryFileName: String): NearestNeighbor = {
val library = new List[ Instance]( 10)
new NearestNeighbor( library)
}
}
val nearestNeighbor = NearestNeighbor.create( "library.data")
val query = new Instance( List( 0.0, 0.0, 0.0, 0.0))
val classifiedQuery = nearestNeighbor.classify( query)
println( classifiedQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment