This file contains hidden or 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, 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) |
This file contains hidden or 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)) | |
| var k = 3 | |
| if ( args.length == 3) { | |
| k = Integer.parseInt( args( 2)) | |
| } | |
| println( nearestNeighbor.classify( query, k)) |
This file contains hidden or 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 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 | |
| } |
This file contains hidden or 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
| 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 = { |
This file contains hidden or 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 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 { |
This file contains hidden or 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 Instance { | |
| def parseCSV( instanceAsCSVString: String): Instance = { | |
| def extractFeatureValues( tokenizedFeatures: List[ String]): List[ Option[Double]] = { | |
| tokenizedFeatures.map { token => | |
| try { | |
| Some( token.toDouble) | |
| } catch { | |
| case nfe: NumberFormatException => { | |
| if ( token == "?") { | |
| None |
This file contains hidden or 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
| (defn get-user-command [game-state] | |
| (let [ command (read-line) | |
| [function & args] (conj (into [] (.split command " ")) "game-state")] | |
| (do | |
| (println function args) | |
| (apply (symbol function) args)))) |
This file contains hidden or 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
| (ns banking) | |
| (defn zip [& rest] (apply map vector rest)) | |
| (defn rand-in-range [x y] | |
| "returns a random int in the range x (inclusive) and y (exclusive)" | |
| (+ x (rand-int (- y x)))) | |
| (defn rand-date [start-date end-date] | |
| "generates a random date between the start-date and the end-date. Each should be a string |
This file contains hidden or 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
| (ns codelesson.mars-rovers-threaded) | |
| ;; Mars Rovers | |
| ;; Steve Butcher (sgwbutcher@yahoo.com) | |
| ;; Code Lesson - Week 3 | |
| ;; | |
| ;; Utility functions | |
| ;; This function will take a map and switch the keys and values | |
| ;; The input map should have unique value as well as keys. |
This file contains hidden or 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
| (ns osx | |
| (:use seesaw.core) | |
| (:import | |
| [com.apple.eawt Application ApplicationListener] | |
| [java.awt.image BufferedImage])) | |
| ;; mostly for use with Seesaw | |
| ;; https://github.com/daveray/seesaw | |
| (defn event-not-handled [e] (.setHandled e false)) |