Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created June 9, 2011 17:18
Show Gist options
  • Save actsasgeek/1017210 to your computer and use it in GitHub Desktop.
Save actsasgeek/1017210 to your computer and use it in GitHub Desktop.
Changes to Nearest Neighbor object to support loading instances from a file.
object NearestNeighbor {
def create( libraryFileName: String): NearestNeighbor = {
val instances = getInstancesFromFile( libraryFileName)
val library = createLibraryFromCSVs( instances)
new NearestNeighbor( library)
}
def getInstancesFromFile( libraryFileName: String): List[ String] = {
Source.fromFile( new File( libraryFileName)).getLines().toList
}
def createLibraryFromCSVs( instances: List[ String]): List[Instance] = {
instances.map( Instance.parseString( _))
}
def main( args: Array[ String]) {
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