Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created June 9, 2011 02:11
Show Gist options
  • Save actsasgeek/1015900 to your computer and use it in GitHub Desktop.
Save actsasgeek/1015900 to your computer and use it in GitHub Desktop.
The NearestNeighbor#classify() method.
class NearestNeighbor( library: List[Instance]) {
def classify( query: Instance): Instance = {
val distanceMeasurements = library.map( example => (query.distanceTo( example), example))
val sortedDistanceMeasurements = distanceMeasurements.sortWith(( e1, e2) => ( e1._1 - e2._1) < 0)
val nearestExample = sortedDistanceMeasurements.head._2
query.assignClassLabel( nearestExample.classLabel)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment