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 Instance( featureValues: List[Double], var classLabel: String) { | |
} | |
class NearestNeighbor( library: List[Instance]) { | |
def classify( query: Instance) { | |
query.classLabel = "unknown" | |
} | |
} | |
object NearestNeighbor { |
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 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( ", ")+"]>" | |
} | |
} |
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 Instance( val featureValues: List[Double], classLabel: Option[String] = None) { | |
def assignClassLabel( assignedClassLabel: Option[String]): Instance = { | |
new Instance( featureValues, assignedClassLabel) | |
} | |
def distanceTo( otherInstance: Instance): Double = { | |
euclideanDistance( featureValues, otherInstance.featureValues) | |
} | |
def euclideanDistance( thisVector: List[ Double], thatVector: List[ Double]): Double = { |
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) | |
} | |
} |
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 NearestNeighborSpec extends Spec with ShouldMatchers { | |
describe( "A Nearest Neighbor classifier") { | |
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) | |
it( "can find the 1st closest example in the library to a query") { | |
val query = new Instance( List( 0.0, 0.0)) |
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
[info] == NearestNeighborSpec == | |
[info] A Nearest Neighbor classifier | |
[info] - can find the 1st closest example in the library to a query | |
[info] == NearestNeighborSpec == | |
[info] | |
[info] == test-complete == | |
[info] == test-complete == |
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
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 | |
} |
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
val nearestNeighbor = NearestNeighbor.create( args( 0)) | |
val query = Instance.parseString( args( 1)) | |
println( nearestNeighbor.classify( query)) |
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 scala.io.Source; | |
import scala.collection.mutable.ListBuffer; | |
import java.io.File; | |
case class Instance( featureValues: List[Double], classLabel: Option[String] = None) { | |
def assignClassLabel( assignedClassLabel: Option[String]): Instance = { | |
new Instance( featureValues, assignedClassLabel) | |
} |
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)) |
OlderNewer