Created
June 9, 2011 02:11
-
-
Save actsasgeek/1015900 to your computer and use it in GitHub Desktop.
The NearestNeighbor#classify() method.
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 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