├── README.md ├── examples ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src │ └── main │ └── kotlin │ ├── KotlinConfDemo.kt │ ├── Model.kt │ └── examples │ ├── Example1.kt │ ├── Example2.kt │ ├── Example3.kt │ └── Example4.kt ├── kotlin_for_data_science.pdf └── kotlin_for_data_science.pptx /README.md: -------------------------------------------------------------------------------- 1 | # KotlinConf Talk - Kotlin for Data Science 2 | 3 | ![](https://camo.githubusercontent.com/705d7144bcce5b0fcb68ea1bd563837bdf3398da/687474703a2f2f692e696d6775722e636f6d2f763346716945412e706e67) 4 | 5 | Slides, notes, and code for the _Kotlin for Data Science_ talk 6 | 7 | ## YouTube Video 8 | 9 | You can watch the session at KotlinConf on YouTube here: 10 | 11 | [![KotlinConf- Kotlin for Data Science](https://i.ytimg.com/vi/J8GYPG6pt5w/hqdefault.jpg)](https://www.youtube.com/watch?v=J8GYPG6pt5w) 12 | 13 | 14 | ## Libraries Used in Demo 15 | 16 | [Kotlin-Statistics](https://github.com/thomasnield/kotlin-statistics) 17 | 18 | [ojAlgo!](https://github.com/optimatika/ojAlgo/wiki/The-Diet-Problem) 19 | 20 | [TornadoFX](https://edvin.gitbooks.io/tornadofx-guide/content/) 21 | 22 | 23 | ## Resources 24 | 25 | Follow this Awesome-like curation of Kotlin Data Science resources here: 26 | 27 | https://github.com/thomasnield/kotlin-data-science-resources 28 | 29 | 30 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Kotlin template 3 | # Compiled class file 4 | *.class 5 | 6 | # Log file 7 | *.log 8 | 9 | # BlueJ files 10 | *.ctxt 11 | 12 | # Mobile Tools for Java (J2ME) 13 | .mtj.tmp/ 14 | /.gradle 15 | /.idea 16 | /build 17 | /out 18 | 19 | # Package Files # 20 | *.jar 21 | *.war 22 | *.ear 23 | *.zip 24 | *.tar.gz 25 | *.rar 26 | *.iml 27 | 28 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 29 | hs_err_pid* 30 | 31 | -------------------------------------------------------------------------------- /examples/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.51' 7 | } 8 | } 9 | 10 | apply plugin: "kotlin" 11 | 12 | compileKotlin { 13 | kotlinOptions.jvmTarget= "1.8" 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | jcenter() 19 | } 20 | 21 | dependencies { 22 | compile 'org.jetbrains.kotlin:kotlin-stdlib:1.1.51' 23 | compile 'org.ojalgo:ojalgo:44.0.0' 24 | compile 'org.nield:kotlin-statistics:1.0.0' 25 | compile 'no.tornado:tornadofx:1.7.12' 26 | testCompile 'junit:junit:4.12' 27 | } -------------------------------------------------------------------------------- /examples/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thomasnield/kotlinconf-datascience-talk/85fb471f875cd481a6720c1f984675b1965807db/examples/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /examples/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Nov 01 07:33:36 CDT 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /examples/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save ( ) { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /examples/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /examples/src/main/kotlin/KotlinConfDemo.kt: -------------------------------------------------------------------------------- 1 | 2 | fun main(args: Array) { 3 | 4 | Driver.addToModel() 5 | 6 | model.minimise().let(::println) 7 | 8 | // see variables for each driver 9 | drivers.forEach { 10 | println("Driver ${it.driverNumber}: ${it.shiftStart.value.toInt()}-${it.shiftEnd.value.toInt()}") 11 | } 12 | } 13 | 14 | // parameters 15 | val operatingDay = 6..22 16 | val allowableShiftSize = 4..6 17 | 18 | // declare drivers 19 | val drivers = listOf( 20 | Driver(driverNumber = 1, rate = 10.0), 21 | Driver(driverNumber = 2, rate = 12.0, availability = 6..11), 22 | Driver(driverNumber = 3, rate = 14.0) 23 | ) 24 | -------------------------------------------------------------------------------- /examples/src/main/kotlin/Model.kt: -------------------------------------------------------------------------------- 1 | import org.ojalgo.optimisation.ExpressionsBasedModel 2 | import org.ojalgo.optimisation.Variable 3 | import java.util.concurrent.atomic.AtomicInteger 4 | 5 | // declare ojAlgo Model 6 | val model = ExpressionsBasedModel() 7 | 8 | // custom DSL for model expression inputs, eliminate naming and adding 9 | val funcId = AtomicInteger(0) 10 | val variableId = AtomicInteger(0) 11 | fun variable() = Variable(variableId.incrementAndGet().toString().let { "Variable$it" }).apply(model::addVariable) 12 | fun ExpressionsBasedModel.addExpression() = funcId.incrementAndGet().let { "Func$it"}.let { addExpression(it) } 13 | 14 | // constants 15 | val operatingDayLength = operatingDay.endInclusive - operatingDay.start 16 | 17 | 18 | 19 | // Driver class will put itself into the Model when addToModel() is called 20 | data class Driver(val driverNumber: Int, 21 | val rate: Double, 22 | val availability: IntRange? = null) { 23 | 24 | val shiftStart = variable().lower(6).upper(22) 25 | val shiftEnd = variable().lower(6).upper(22) 26 | 27 | fun addToModel() { 28 | 29 | //constrain shift length 30 | model.addExpression() 31 | .lower(allowableShiftSize.start) 32 | .upper(allowableShiftSize.endInclusive) 33 | .set(shiftEnd, 1) 34 | .set(shiftStart, -1) 35 | 36 | //add specific driver availability 37 | availability?.let { 38 | model.addExpression() 39 | .lower(it.start) 40 | .upper(it.endInclusive) 41 | .set(shiftStart, 1) 42 | 43 | model.addExpression() 44 | .lower(it.start) 45 | .upper(it.endInclusive) 46 | .set(shiftEnd, 1) 47 | } 48 | 49 | //prevent shift overlap 50 | drivers.asSequence() 51 | .filter { it != this } 52 | .forEach { otherDriver -> 53 | 54 | val occupied = variable().lower(0).upper(1).integer(true) 55 | 56 | model.addExpression() 57 | .upper(0) 58 | .set(otherDriver.shiftEnd, 1) 59 | .set(occupied, operatingDayLength * - 1) 60 | .set(shiftStart, -1) 61 | 62 | model.addExpression() 63 | .upper(operatingDayLength) 64 | .set(shiftEnd, 1) 65 | .set(occupied, operatingDayLength) 66 | .set(otherDriver.shiftStart, -1) 67 | } 68 | } 69 | 70 | companion object { 71 | 72 | fun addToModel() { 73 | 74 | //ensure coverage of entire day 75 | model.addExpression() 76 | .level(operatingDayLength) 77 | .apply { 78 | drivers.forEach { 79 | set(it.shiftEnd, 1) 80 | set(it.shiftStart, -1) 81 | } 82 | } 83 | 84 | // set objective 85 | model.addExpression().weight(1).apply { 86 | drivers.forEach { 87 | set(it.shiftEnd, it.rate) 88 | set(it.shiftStart, -1 * it.rate) 89 | } 90 | } 91 | 92 | 93 | drivers.forEach { it.addToModel() } 94 | } 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /examples/src/main/kotlin/examples/Example1.kt: -------------------------------------------------------------------------------- 1 | package examples 2 | fun main(args: Array) { 3 | 4 | //Retrieve user "John" 5 | val user = users.first { it.firstName == "John" } 6 | 7 | //see recommended friends for John 8 | user.recommendedFriends() 9 | .forEach { println(it) } 10 | } 11 | 12 | data class SocialUser( 13 | val userId: Int, 14 | val firstName: String, 15 | val lastName: String 16 | ) { 17 | val friends get() = friendships.asSequence() 18 | .filter { userId == it.first || userId == it.second } 19 | .flatMap { sequenceOf(it.first, it.second) } 20 | .filter { it != userId } 21 | .map { friendId -> 22 | users.first { it.userId == friendId } 23 | }.toList() 24 | 25 | fun mutualFriendsOf(otherUser: SocialUser) = 26 | friends.asSequence().filter { it in otherUser.friends }.toList() 27 | 28 | fun recommendedFriends() = users.asSequence() 29 | .filter { it.userId != userId } // only look for other users 30 | .filter { it !in friends } // filter to people not already friends with 31 | .map { otherUser -> // package up mutual friends 32 | MutualFriendships(this, otherUser, mutualFriendsOf(otherUser).toList()) 33 | }.filter { it.mutualFriends.count() > 0 } // omit where no MutualFriendships exist 34 | .sortedByDescending { it.mutualFriends.count() } // sort greatest number of mutual friendships first 35 | .map { it.otherUser } 36 | } 37 | 38 | data class MutualFriendships(val user: SocialUser, val otherUser: SocialUser, val mutualFriends: List) 39 | 40 | val users = listOf( 41 | SocialUser(1, "John", "Scott"), 42 | SocialUser(2, "Hannah", "Earley"), 43 | SocialUser(3, "Timothy", "Tannen"), 44 | SocialUser(4, "Scott", "Marcum"), 45 | SocialUser(5, "Sid", "Maddow"), 46 | SocialUser(6, "Rachel", "Zimmerman"), 47 | SocialUser(7, "Heather", "Timmers"), 48 | SocialUser(8, "Casey", "Crane"), 49 | SocialUser(9, "Billy", "Awesome") 50 | ) 51 | 52 | val friendships = listOf( 53 | 1 to 6, 54 | 1 to 5, 55 | 1 to 8, 56 | 2 to 3, 57 | 2 to 9, 58 | 2 to 6, 59 | 3 to 8, 60 | 3 to 2, 61 | 3 to 7, 62 | 4 to 1, 63 | 4 to 2, 64 | 4 to 9, 65 | 5 to 7, 66 | 9 to 2, 67 | 8 to 4, 68 | 6 to 9 69 | ) 70 | -------------------------------------------------------------------------------- /examples/src/main/kotlin/examples/Example2.kt: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import org.nield.kotlinstatistics.averageBy 4 | import org.nield.kotlinstatistics.countBy 5 | 6 | //declare Product class 7 | class Product(val id: Int, 8 | val name: String, 9 | val category: String, 10 | val section: Int, 11 | val defectRate: Double) 12 | 13 | // Create list of Products 14 | val products = listOf(Product(1, "Rayzeon", "A", 3, 1.1), 15 | Product(2, "ZenFire", "B", 4, 0.7), 16 | Product(3, "HydroFlux", "A", 3, 1.9), 17 | Product(4, "IceFlyer", "C", 1, 2.4), 18 | Product(5, "FireCoyote", "B", 4, 3.2), 19 | Product(6, "LightFiber", "B", 2, 5.1), 20 | Product(7, "PyroKit", "A", 3, 1.4), 21 | Product(8, "BladeKit", "C", 1, 0.5), 22 | Product(9, "NightHawk", "C", 1, 3.5), 23 | Product(10, "NoctoSquirrel", "A", 2, 1.1), 24 | Product(11, "WolverinePack", "A", 3, 1.2) 25 | ) 26 | 27 | 28 | fun main(args: Array) { 29 | 30 | 31 | // Get Count by Category 32 | val countByCategory= 33 | products.countBy { it.category } 34 | 35 | println("Counts by Category") 36 | countByCategory.entries.forEach { println(it) } 37 | 38 | // Get Average Defect Rate by Category 39 | val averageDefectByCategory = 40 | products.averageBy( 41 | keySelector = { it.category }, 42 | doubleSelector = { it.defectRate } 43 | ) 44 | 45 | println("\nAverage Defect Rate by Category") 46 | averageDefectByCategory.entries.forEach { println(it) } 47 | } 48 | -------------------------------------------------------------------------------- /examples/src/main/kotlin/examples/Example3.kt: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import javafx.application.Application 4 | import javafx.geometry.Orientation 5 | import javafx.scene.chart.NumberAxis 6 | import org.nield.kotlinstatistics.multiKMeansCluster 7 | import tornadofx.* 8 | import java.time.LocalDate 9 | import java.time.temporal.ChronoUnit 10 | 11 | 12 | fun main(args: Array) = Application.launch(MyApp::class.java, *args) 13 | 14 | class MyApp: App(MyView::class) 15 | 16 | class MyView : View() { 17 | override val root = splitpane { 18 | orientation = Orientation.HORIZONTAL 19 | 20 | tableview { 21 | column("FIRST NAME", Patient::firstName) 22 | column("LAST NAME", Patient::lastName) 23 | column("GENDER", Patient::gender) 24 | column("BIRTHDAY", Patient::birthday) 25 | column("AGE", Patient::age) 26 | column("WBCC", Patient::whiteBloodCellCount) 27 | 28 | items.setAll(patients) 29 | } 30 | 31 | scatterchart("WBCC Clustering by Age", NumberAxis(), NumberAxis()) { 32 | 33 | patients.multiKMeansCluster(k = 3, 34 | maxIterations = 10000, 35 | trialCount = 50, 36 | xSelector = { it.age.toDouble() }, 37 | ySelector = { it.whiteBloodCellCount.toDouble() } 38 | ) 39 | .forEachIndexed { index, centroid -> 40 | series("Group ${index + 1}") { 41 | centroid.points.forEach { 42 | data(it.age, it.whiteBloodCellCount) 43 | } 44 | } 45 | } 46 | } 47 | } 48 | } 49 | 50 | 51 | data class Patient(val firstName: String, 52 | val lastName: String, 53 | val gender: Gender, 54 | val birthday: LocalDate, 55 | val whiteBloodCellCount: Int) { 56 | 57 | val age = ChronoUnit.YEARS.between(birthday, LocalDate.now()) 58 | } 59 | 60 | val patients = listOf( 61 | Patient("John", "Simone", Gender.MALE, LocalDate.of(1989, 1, 7), 4500), 62 | Patient("Sarah", "Marley", Gender.FEMALE, LocalDate.of(1970, 2, 5), 6700), 63 | Patient("Jessica", "Arnold", Gender.FEMALE, LocalDate.of(1980, 3, 9), 3400), 64 | Patient("Sam", "Beasley", Gender.MALE, LocalDate.of(1981, 4, 17), 8800), 65 | Patient("Dan", "Forney", Gender.MALE, LocalDate.of(1985, 9, 13), 5400), 66 | Patient("Lauren", "Michaels", Gender.FEMALE, LocalDate.of(1975, 8, 21), 5000), 67 | Patient("Michael", "Erlich", Gender.MALE, LocalDate.of(1985, 12, 17), 4100), 68 | Patient("Jason", "Miles", Gender.MALE, LocalDate.of(1991, 11, 1), 3900), 69 | Patient("Rebekah", "Earley", Gender.FEMALE, LocalDate.of(1985, 2, 18), 4600), 70 | Patient("James", "Larson", Gender.MALE, LocalDate.of(1974, 4, 10), 5100), 71 | Patient("Dan", "Ulrech", Gender.MALE, LocalDate.of(1991, 7, 11), 6000), 72 | Patient("Heather", "Eisner", Gender.FEMALE, LocalDate.of(1994, 3, 6), 6000), 73 | Patient("Jasper", "Martin", Gender.MALE, LocalDate.of(1971, 7, 1), 6000) 74 | ) 75 | 76 | enum class Gender { 77 | MALE, 78 | FEMALE 79 | } -------------------------------------------------------------------------------- /examples/src/main/kotlin/examples/Example4.kt: -------------------------------------------------------------------------------- 1 | package examples 2 | 3 | import org.nield.kotlinstatistics.binByComparable 4 | import java.time.LocalDate 5 | import java.time.Month 6 | 7 | fun main(args: Array) { 8 | 9 | data class Sale(val accountId: Int, val date: LocalDate, val amount: Double) 10 | 11 | val sales = listOf( 12 | Sale(1, LocalDate.of(2016,12,3), 180.0), 13 | Sale(2, LocalDate.of(2016, 7, 4), 140.2), 14 | Sale(3, LocalDate.of(2016, 6, 3), 111.4), 15 | Sale(4, LocalDate.of(2016, 1, 5), 192.7), 16 | Sale(5, LocalDate.of(2016, 5, 4), 137.9), 17 | Sale(6, LocalDate.of(2016, 3, 6), 125.6), 18 | Sale(7, LocalDate.of(2016, 12,4), 164.3), 19 | Sale(8, LocalDate.of(2016, 7,11), 144.2) 20 | ) 21 | 22 | //bin by quarter 23 | val binnedByQuarter = sales.binByComparable( 24 | valueSelector = { it.date.month }, 25 | binIncrements = 3, 26 | incrementer = { it.plus(1) } 27 | ) 28 | 29 | binnedByQuarter.forEach(::println) 30 | 31 | //look up bin for February 32 | println("\n\nLook up bin for FEBRUARY") 33 | println(binnedByQuarter[Month.FEBRUARY]) 34 | } -------------------------------------------------------------------------------- /kotlin_for_data_science.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thomasnield/kotlinconf-datascience-talk/85fb471f875cd481a6720c1f984675b1965807db/kotlin_for_data_science.pdf -------------------------------------------------------------------------------- /kotlin_for_data_science.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thomasnield/kotlinconf-datascience-talk/85fb471f875cd481a6720c1f984675b1965807db/kotlin_for_data_science.pptx --------------------------------------------------------------------------------