Skip to content

Instantly share code, notes, and snippets.

View actsasgeek's full-sized avatar

Stephyn Butcher actsasgeek

View GitHub Profile
@actsasgeek
actsasgeek / instance_v02.scala
Created July 8, 2011 17:16
Instance object that handles missing values.
object Instance {
def parseCSV( instanceAsCSVString: String): Instance = {
def extractFeatureValues( parsedInstance: Array[ String]): List[ Option[Double]] = {
val featuresStartIndex = 0
val featuresEndIndex = parsedInstance.length - 2
val featureBuffer = new ListBuffer[Option[Double]]()
for ( index <- featuresStartIndex to featuresEndIndex) {
val token = parsedInstance( index)
val value = try {
@actsasgeek
actsasgeek / instance_class_v01.scala
Created July 8, 2011 16:08
The current Instance class.
case class Instance( featureValues: List[Double], classLabel: Option[String] = None) {
def assignClassLabel( assignedClassLabel: Option[String]): Instance = {
new Instance( featureValues, assignedClassLabel)
}
def distanceTo( otherInstance: Instance): Double = {
squaredEuclideanDistance( featureValues, otherInstance.featureValues)
}
def squaredEuclideanDistance( thisVector: List[ Double], thatVector: List[ Double]): Double = {
@actsasgeek
actsasgeek / instance_v01.scala
Created July 8, 2011 16:01
the current Instance object.
object Instance {
def parseString( instanceAsCSVString: String): Instance = {
def extractFeatureValues( parsedInstance: Array[ String]): List[ Double] = {
val featuresStartIndex = 0
val featuresEndIndex = parsedInstance.length - 2
val featureBuffer = new ListBuffer[Double]()
for ( index <- featuresStartIndex to featuresEndIndex) {
featureBuffer += parsedInstance( index).toDouble
}
@actsasgeek
actsasgeek / classifer.scala
Created July 7, 2011 15:15
The script that runs a classification session.
val nearestNeighbor = NearestNeighbor.create( args( 0))
val query = Instance.parseString( args( 1))
var k = 3
if ( args.length == 3) {
k = Integer.parseInt( args( 2))
}
println( nearestNeighbor.classify( query, k))
@actsasgeek
actsasgeek / nearest_neighbor_v4.scala
Created July 7, 2011 15:08
Modification of 1-NN algorithm to handle kNN
class NearestNeighbor( library: List[Instance]) {
def classify( query: Instance, k: Int = 3): Instance = {
val distanceMeasurements = library.map( example => (query.distanceTo( example), example))
val sortedDistanceMeasurements = distanceMeasurements.sortWith(( e1, e2) => ( e1._1 - e2._1) < 0)
val kNearestExamples = sortedDistanceMeasurements.take( k)
val groupedExamples = kNearestExamples.groupBy( x => x._2.classLabel).toList
val sortedClassesByCounts = groupedExamples.map { kv => (kv._1, kv._2.length)}.sortWith( _._2 > _._2)
val classWithMostVotes = sortedClassesByCounts.head
query.assignClassLabel( classWithMostVotes._1)
@actsasgeek
actsasgeek / nearest_neighbor_spec_1.0.scala
Created June 9, 2011 17:59
Tests for nearest_neighbor.scala
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))
@actsasgeek
actsasgeek / nearest_neighbor_1.0.scala
Created June 9, 2011 17:58
Full source code for kNN with k = 1
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)
}
@actsasgeek
actsasgeek / classifier.scala
Created June 9, 2011 17:52
A script to run the Nearest Neighbor algorithm...
val nearestNeighbor = NearestNeighbor.create( args( 0))
val query = Instance.parseString( args( 1))
println( nearestNeighbor.classify( query))
@actsasgeek
actsasgeek / nearest_neighbor_v5.scala
Created June 9, 2011 17:18
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
}
@actsasgeek
actsasgeek / gist:1015928
Created June 9, 2011 02:31
Results of running the unit test: NearestNeighborSpec
[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 ==