Created
June 9, 2011 17:59
-
-
Save actsasgeek/1017313 to your computer and use it in GitHub Desktop.
Tests for nearest_neighbor.scala
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
import org.scalatest.Spec | |
import org.scalatest.matchers.ShouldMatchers | |
import scala.io.Source; | |
class InstanceSpec extends Spec with ShouldMatchers { | |
describe( "An Instance") { | |
val instance = new Instance( List( 0.0, 0.0)) | |
it( "can calculate the distance between itself and another instance.") { | |
val otherInstance = new Instance( List( 3.0, 4.0)) | |
instance.distanceTo( otherInstance) should be === 25.0 | |
} | |
} | |
} | |
class NearestNeighborSpec extends Spec with ShouldMatchers { | |
describe( "A Nearest Neighbor classifier") { | |
it( "can find the 1st closest example in the library to a query") { | |
val library = List( | |
new Instance( List( 3.0, 3.0), Some( "A")), | |
new Instance( List( 4.0, 2.0), Some( "A")), | |
new Instance( List( 2.0, 2.0), Some( "B")) | |
) | |
val nearestNeighbor = new NearestNeighbor( library) | |
val query = new Instance( List( 0.0, 0.0)) | |
val queryResult = nearestNeighbor.classify( query) | |
queryResult.classLabel should be === Some( "B") | |
} | |
} | |
it( "can create a library from a text representation of instances as CSVs") { | |
val text = """3.0,3.0,A | |
4.0,2.0,A | |
2.0,2.0,B""" | |
val library = NearestNeighbor.createLibraryFromCSVs( Source.fromString( text).getLines().toList) | |
library should be === List( | |
new Instance( List( 3.0, 3.0), Some( "A")), | |
new Instance( List( 4.0, 2.0), Some( "A")), | |
new Instance( List( 2.0, 2.0), Some( "B")) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment