-
-
Save maxandersen/b3ad22cd39ff95e72a8c54f58580c14d to your computer and use it in GitHub Desktop.
notebook showing kotlin jupyter kernel in google colab. Use http://colab.research.google.com/gist/maxandersen/b3ad22cd39ff95e72a8c54f58580c14d/index.ipynb to open it.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/maxandersen/b3ad22cd39ff95e72a8c54f58580c14d/index.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "3AbFugV7bL4W" | |
}, | |
"source": [ | |
"# Kotlin for Google Colab" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZPJsQF2ObL4X" | |
}, | |
"source": [ | |
"This notebook will guide you on how one can use [Kotlin](https://kotlinlang.org/) with [Jupyter notebooks](https://jupyter.org/) on [Google Colab](https://colab.research.google.com)." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "a5hIDu36bL4Y" | |
}, | |
"source": [ | |
"### Installing kernel" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "GSMWlxUMbL4Y" | |
}, | |
"source": [ | |
"To install [Kotlin Jupyter kernel](https://github.com/erokhins/kotlin-jupyter) on Google Colab run the following using a Python kernel\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install jbang\n", | |
"import jbang\n", | |
"jbang.exec(\"trust add https://github.com/jupyter-java\")\n", | |
"jbang.exec(\"install-kernel@jupyter-java kotlin\")" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "r_KNlkERcTvp", | |
"outputId": "ccbf1f1a-07f8-4917-b8c8-8c969b3702ab" | |
}, | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Requirement already satisfied: jbang in /usr/local/lib/python3.10/dist-packages (0.4.0)\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"CompletedProcess(args='curl -Ls https://sh.jbang.dev | bash -s - install-kernel@jupyter-java kotlin', returncode=0, stdout='Kotlin - kotlin (JBang) kernel installed to /root/.local/share/jupyter/kernels/jbang-kotlin/kernel.json\\nKotlin - kotlin (JBang)-ipc kernel installed to /root/.local/share/jupyter/kernels/jbang-kotlin-ipc/kernel.json\\nAdditional file: /root/.local/share/jupyter/kernels/jbang-kotlin-ipc/ipc_proxy_kernel.py\\n\\nBrought to you by https://github.com/jupyter-java\\n', stderr='')" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"\n", | |
"When that completes switch to `Kotlin - jbang - ipc` kernel." | |
], | |
"metadata": { | |
"id": "dMisPmOkcXiA" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "-HXGoUZDbL4Y" | |
}, | |
"source": [ | |
"### Running cells" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZwzQQBjwbL4Z" | |
}, | |
"source": [ | |
"Here's a simple example with Kotlin code:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"id": "FkL_Y-L0bL4Z" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Greeter(val name: String) {\n", | |
" fun greet() {\n", | |
" println(\"Hello, $name!\")\n", | |
" }\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "pCgfaj-9bL4a", | |
"outputId": "723198fd-f224-4e85-8ca3-894273006915" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Hello, Jupyter!\n" | |
] | |
} | |
], | |
"source": [ | |
"Greeter(\"Jupyter\").greet() // Run me" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "gaE9Zrz4bL4a" | |
}, | |
"source": [ | |
"### Configuring Maven dependencies" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "0F6yXym4bL4b" | |
}, | |
"source": [ | |
"Here's another example, courtsey of [thomasnield/kotlin-statistics](https://github.com/thomasnield/kotlin-statistics), showcasing how to load additional dependencies to the notebook from Maven repos:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"id": "cmyE_OQ8bL4b" | |
}, | |
"outputs": [], | |
"source": [ | |
"@file:Repository(\"https://repo1.maven.org/maven2\")\n", | |
"@file:DependsOn(\"org.nield:kotlin-statistics:1.2.1\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"id": "0P8sxmOXbL4b" | |
}, | |
"outputs": [], | |
"source": [ | |
"import java.time.LocalDate\n", | |
"import java.time.temporal.ChronoUnit\n", | |
"import org.nield.kotlinstatistics.*\n", | |
"\n", | |
"data class Patient(val firstName: String,\n", | |
" val lastName: String,\n", | |
" val gender: Gender,\n", | |
" val birthday: LocalDate,\n", | |
" val whiteBloodCellCount: Int) {\n", | |
"\n", | |
" val age = ChronoUnit.YEARS.between(birthday, LocalDate.now())\n", | |
"}\n", | |
"\n", | |
"val patients = listOf(\n", | |
" Patient(\"John\", \"Simone\", Gender.MALE, LocalDate.of(1989, 1, 7), 4500),\n", | |
" Patient(\"Sarah\", \"Marley\", Gender.FEMALE, LocalDate.of(1970, 2, 5), 6700),\n", | |
" Patient(\"Jessica\", \"Arnold\", Gender.FEMALE, LocalDate.of(1980, 3, 9), 3400),\n", | |
" Patient(\"Sam\", \"Beasley\", Gender.MALE, LocalDate.of(1981, 4, 17), 8800),\n", | |
" Patient(\"Dan\", \"Forney\", Gender.MALE, LocalDate.of(1985, 9, 13), 5400),\n", | |
" Patient(\"Lauren\", \"Michaels\", Gender.FEMALE, LocalDate.of(1975, 8, 21), 5000),\n", | |
" Patient(\"Michael\", \"Erlich\", Gender.MALE, LocalDate.of(1985, 12, 17), 4100),\n", | |
" Patient(\"Jason\", \"Miles\", Gender.MALE, LocalDate.of(1991, 11, 1), 3900),\n", | |
" Patient(\"Rebekah\", \"Earley\", Gender.FEMALE, LocalDate.of(1985, 2, 18), 4600),\n", | |
" Patient(\"James\", \"Larson\", Gender.MALE, LocalDate.of(1974, 4, 10), 5100),\n", | |
" Patient(\"Dan\", \"Ulrech\", Gender.MALE, LocalDate.of(1991, 7, 11), 6000),\n", | |
" Patient(\"Heather\", \"Eisner\", Gender.FEMALE, LocalDate.of(1994, 3, 6), 6000),\n", | |
" Patient(\"Jasper\", \"Martin\", Gender.MALE, LocalDate.of(1971, 7, 1), 6000)\n", | |
")\n", | |
"\n", | |
"enum class Gender {\n", | |
" MALE,\n", | |
" FEMALE\n", | |
"}\n", | |
"\n", | |
"val clusters = patients.multiKMeansCluster(k = 3,\n", | |
" maxIterations = 10000,\n", | |
" trialCount = 50,\n", | |
" xSelector = { it.age.toDouble() },\n", | |
" ySelector = { it.whiteBloodCellCount.toDouble() }\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ST1Y9N_jbL4b", | |
"outputId": "3b5d43ca-e11f-495b-e272-d4ef4299187e" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"CENTROID: 0\n", | |
"\tPatient(firstName=Sarah, lastName=Marley, gender=FEMALE, birthday=1970-02-05, whiteBloodCellCount=6700)\n", | |
"\tPatient(firstName=Sam, lastName=Beasley, gender=MALE, birthday=1981-04-17, whiteBloodCellCount=8800)\n", | |
"CENTROID: 1\n", | |
"\tPatient(firstName=John, lastName=Simone, gender=MALE, birthday=1989-01-07, whiteBloodCellCount=4500)\n", | |
"\tPatient(firstName=Jessica, lastName=Arnold, gender=FEMALE, birthday=1980-03-09, whiteBloodCellCount=3400)\n", | |
"\tPatient(firstName=Michael, lastName=Erlich, gender=MALE, birthday=1985-12-17, whiteBloodCellCount=4100)\n", | |
"\tPatient(firstName=Jason, lastName=Miles, gender=MALE, birthday=1991-11-01, whiteBloodCellCount=3900)\n", | |
"\tPatient(firstName=Rebekah, lastName=Earley, gender=FEMALE, birthday=1985-02-18, whiteBloodCellCount=4600)\n", | |
"CENTROID: 2\n", | |
"\tPatient(firstName=Dan, lastName=Forney, gender=MALE, birthday=1985-09-13, whiteBloodCellCount=5400)\n", | |
"\tPatient(firstName=Lauren, lastName=Michaels, gender=FEMALE, birthday=1975-08-21, whiteBloodCellCount=5000)\n", | |
"\tPatient(firstName=James, lastName=Larson, gender=MALE, birthday=1974-04-10, whiteBloodCellCount=5100)\n", | |
"\tPatient(firstName=Dan, lastName=Ulrech, gender=MALE, birthday=1991-07-11, whiteBloodCellCount=6000)\n", | |
"\tPatient(firstName=Heather, lastName=Eisner, gender=FEMALE, birthday=1994-03-06, whiteBloodCellCount=6000)\n", | |
"\tPatient(firstName=Jasper, lastName=Martin, gender=MALE, birthday=1971-07-01, whiteBloodCellCount=6000)\n" | |
] | |
} | |
], | |
"source": [ | |
"clusters.forEachIndexed { index, item ->\n", | |
" println(\"CENTROID: $index\")\n", | |
" item.points.forEach {\n", | |
" println(\"\\t$it\")\n", | |
" }\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "YT1KZj71bL4b" | |
}, | |
"source": [ | |
"### Configuring the built-in via magics" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MdcsHQ_mbL4b" | |
}, | |
"source": [ | |
"For a more straightforward, the Kotlin kernel pre-configures certain libraries, and allows the notebook user to load them via special commands, also known as [magics](https://ipython.readthedocs.io/en/stable/interactive/magics.html). To pre-configure libraries for a notebook, one must comma-separate their names prepened with `%use`. Here's how it works:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "cpOpU_77bL4b", | |
"outputId": "a31c80b9-c58f-4f19-b642-e75f06e3968c" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"The problem is found in one of the loaded libraries: check library imports, dependencies and repositories\n", | |
"Error compiling code:\n", | |
"@file:DependsOn(\"com.github.thomasnield:kotlin-statistics:-SNAPSHOT\")\n", | |
"import org.nield.kotlinstatistics.*\n", | |
"\n", | |
"\n", | |
"Errors:\n", | |
"Failed to resolve [com.github.thomasnield:kotlin-statistics:-SNAPSHOT]:\n", | |
"File 'com.github.thomasnield:kotlin-statistics:-SNAPSHOT' not found\n", | |
"ArtifactResolutionException: Could not transfer artifact com.github.thomasnield:kotlin-statistics:pom:-SNAPSHOT from/to https___jitpack.io_ (https://jitpack.io/): authentication failed for https://jitpack.io/com/github/thomasnield/kotlin-statistics/-SNAPSHOT/kotlin-statistics--SNAPSHOT.pom, status: 401 Unauthorized\n", | |
"\n", | |
"org.jetbrains.kotlinx.jupyter.exceptions.ReplLibraryException: The problem is found in one of the loaded libraries: check library imports, dependencies and repositories\n", | |
"\tat org.jetbrains.kotlinx.jupyter.exceptions.ReplLibraryExceptionKt.rethrowAsLibraryException(ReplLibraryException.kt:32)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.doAddLibraries(CellExecutorImpl.kt:154)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.addLibraries(CellExecutorImpl.kt:189)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost$DefaultImpls.addLibrary(KotlinKernelHost.kt:42)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.addLibrary(CellExecutorImpl.kt:134)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute(CellExecutorImpl.kt:62)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.CellExecutor$DefaultImpls.execute$default(CellExecutor.kt:14)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl$evalEx$1.invoke(repl.kt:500)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl$evalEx$1.invoke(repl.kt:478)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl.withEvalContext(repl.kt:441)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl.evalEx(repl.kt:478)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.messaging.ProtocolKt$shellMessagesHandler$2$res$1.invoke(protocol.kt:320)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.messaging.ProtocolKt$shellMessagesHandler$2$res$1.invoke(protocol.kt:314)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.JupyterExecutorImpl$runExecution$execThread$1.invoke(execution.kt:38)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.JupyterExecutorImpl$runExecution$execThread$1.invoke(execution.kt:33)\n", | |
"\tat kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)\n", | |
"Caused by: org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException: Failed to resolve [com.github.thomasnield:kotlin-statistics:-SNAPSHOT]:\n", | |
"File 'com.github.thomasnield:kotlin-statistics:-SNAPSHOT' not found\n", | |
"ArtifactResolutionException: Could not transfer artifact com.github.thomasnield:kotlin-statistics:pom:-SNAPSHOT from/to https___jitpack.io_ (https://jitpack.io/): authentication failed for https://jitpack.io/com/github/thomasnield/kotlin-statistics/-SNAPSHOT/kotlin-statistics--SNAPSHOT.pom, status: 401 Unauthorized\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.JupyterCompilerImpl.compileSync(JupyterCompilerImpl.kt:182)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl.eval(InternalEvaluatorImpl.kt:102)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$execute$1$result$1.invoke(CellExecutorImpl.kt:75)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$execute$1$result$1.invoke(CellExecutorImpl.kt:73)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl.withHost(repl.kt:690)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute(CellExecutorImpl.kt:73)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.CellExecutor$DefaultImpls.execute$default(CellExecutor.kt:14)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.execute(CellExecutorImpl.kt:227)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.api.libraries.CodeExecutionCallback.invoke(CodeExecutionCallback.kt:8)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.api.libraries.CodeExecutionCallback.invoke(CodeExecutionCallback.kt:6)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.execute(CellExecutorImpl.kt:254)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.runChild(CellExecutorImpl.kt:147)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.runChild(CellExecutorImpl.kt:143)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext.access$runChild(CellExecutorImpl.kt:134)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext$doAddLibraries$1.invoke(CellExecutorImpl.kt:155)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$ExecutionContext$doAddLibraries$1.invoke(CellExecutorImpl.kt:154)\n", | |
"\tat org.jetbrains.kotlinx.jupyter.exceptions.ReplLibraryExceptionKt.rethrowAsLibraryException(ReplLibraryException.kt:30)\n", | |
"\t... 15 more\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"%use kotlin-statistics" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "T1Bw3GH0bL4b" | |
}, | |
"source": [ | |
"When such a cell is executed, the kernel, makes sure the corresponding Maven repo is configured, the library is loaded, necessary import statements are added (e.g. in that case `import org.nield.kotlinstatistics.*` won't be needed), and necessary renderers are configured. The supported magics now include: [`%%kotlin-statistics`](https://github.com/thomasnield/kotlin-statistics), [`klaxon`](https://github.com/cbeust/klaxon), [`krangl`](https://github.com/holgerbrandl/krangl), [`kravis`](https://github.com/holgerbrandl/kravis), and [`lets-plot`](https://github.com/jetbrains/datalore-plot)." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "PGqmZHGSbL4b" | |
}, | |
"source": [ | |
"Here's another example, showcasing [`krangl`](https://github.com/holgerbrandl/krangl), and [`lets-plot`](https://github.com/jetbrains/datalore-plot) libraries:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 17 | |
}, | |
"id": "PrDaMt4wbL4b", | |
"outputId": "228ee9e5-7a04-4f27-9989-de69479c5231" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/html": [ | |
" <div id=\"DeJ5Hw\"></div>\n", | |
" <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n", | |
" if(!window.letsPlotCallQueue) {\n", | |
" window.letsPlotCallQueue = [];\n", | |
" }; \n", | |
" window.letsPlotCall = function(f) {\n", | |
" window.letsPlotCallQueue.push(f);\n", | |
" };\n", | |
" (function() {\n", | |
" var script = document.createElement(\"script\");\n", | |
" script.type = \"text/javascript\";\n", | |
" script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.1.0/js-package/distr/lets-plot.min.js\";\n", | |
" script.onload = function() {\n", | |
" window.letsPlotCall = function(f) {f();};\n", | |
" window.letsPlotCallQueue.forEach(function(f) {f();});\n", | |
" window.letsPlotCallQueue = [];\n", | |
" \n", | |
" \n", | |
" };\n", | |
" script.onerror = function(event) {\n", | |
" window.letsPlotCall = function(f) {};\n", | |
" window.letsPlotCallQueue = [];\n", | |
" var div = document.createElement(\"div\");\n", | |
" div.style.color = 'darkred';\n", | |
" div.textContent = 'Error loading Lets-Plot JS';\n", | |
" document.getElementById(\"DeJ5Hw\").appendChild(div);\n", | |
" };\n", | |
" var e = document.getElementById(\"DeJ5Hw\");\n", | |
" e.appendChild(script);\n", | |
" })();\n", | |
" </script>" | |
] | |
}, | |
"metadata": {} | |
} | |
], | |
"source": [ | |
"%use lets-plot, krangl" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 177 | |
}, | |
"id": "jrW0xodEbL4b", | |
"outputId": "4ff4c727-5c14-4b24-8bfa-dedbd7fce7f3" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": [ | |
"<html><body><table><tr><th style=\"text-align:left\">sepal_length</th><th style=\"text-align:left\">sepal_width</th><th style=\"text-align:left\">petal_length</th><th style=\"text-align:left\">petal_width</th><th style=\"text-align:left\">species</th></tr><tr><td style=\"text-align:left\" title=\"5.1\">5.1</td><td style=\"text-align:left\" title=\"3.5\">3.5</td><td style=\"text-align:left\" title=\"1.4\">1.4</td><td style=\"text-align:left\" title=\"0.2\">0.2</td><td style=\"text-align:left\" title=\"Iris-setosa\">Iris-setosa</td></tr><tr><td style=\"text-align:left\" title=\"4.9\">4.9</td><td style=\"text-align:left\" title=\"3.0\">3.0</td><td style=\"text-align:left\" title=\"1.4\">1.4</td><td style=\"text-align:left\" title=\"0.2\">0.2</td><td style=\"text-align:left\" title=\"Iris-setosa\">Iris-setosa</td></tr><tr><td style=\"text-align:left\" title=\"4.7\">4.7</td><td style=\"text-align:left\" title=\"3.2\">3.2</td><td style=\"text-align:left\" title=\"1.3\">1.3</td><td style=\"text-align:left\" title=\"0.2\">0.2</td><td style=\"text-align:left\" title=\"Iris-setosa\">Iris-setosa</td></tr><tr><td style=\"text-align:left\" title=\"4.6\">4.6</td><td style=\"text-align:left\" title=\"3.1\">3.1</td><td style=\"text-align:left\" title=\"1.5\">1.5</td><td style=\"text-align:left\" title=\"0.2\">0.2</td><td style=\"text-align:left\" title=\"Iris-setosa\">Iris-setosa</td></tr><tr><td style=\"text-align:left\" title=\"5.0\">5.0</td><td style=\"text-align:left\" title=\"3.6\">3.6</td><td style=\"text-align:left\" title=\"1.4\">1.4</td><td style=\"text-align:left\" title=\"0.2\">0.2</td><td style=\"text-align:left\" title=\"Iris-setosa\">Iris-setosa</td></tr></table><p>Shape: 5 x 5. \n", | |
"</p></body></html>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 13 | |
} | |
], | |
"source": [ | |
"val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/cheptsov/kotlin-jupyter-demo/master/data/iris.csv\")\n", | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"scrolled": true, | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 134 | |
}, | |
"id": "LAOThax5bL4c", | |
"outputId": "c8e8884b-27a7-4738-f3c5-0fde8a6c62fd" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": [ | |
"<html><body><table><tr><th style=\"text-align:left\">species</th><th style=\"text-align:left\">n</th></tr><tr><td style=\"text-align:left\" title=\"Iris-setosa\">Iris-setosa</td><td style=\"text-align:left\" title=\"50\">50</td></tr><tr><td style=\"text-align:left\" title=\"Iris-versicolor\">Iris-versicolor</td><td style=\"text-align:left\" title=\"50\">50</td></tr><tr><td style=\"text-align:left\" title=\"Iris-virginica\">Iris-virginica</td><td style=\"text-align:left\" title=\"50\">50</td></tr></table><p>Shape: 3 x 2. \n", | |
"</p></body></html>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 14 | |
} | |
], | |
"source": [ | |
"df.groupBy(\"species\").count()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"scrolled": false, | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "9DykR4-5bL4c", | |
"outputId": "aa4e85a7-711a-453e-aa1f-013ca33f47a8" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"Line_23.jupyter.kts (1:14 - 24) Unresolved reference: geom_point\n", | |
"Line_23.jupyter.kts (3:9 - 55) Type mismatch: inferred type is ColumnFormula but Pair<TypeVariable(K), TypeVariable(V)> was expected\n", | |
"Line_23.jupyter.kts (3:35 - 44) Unresolved reference: asDoubles\n", | |
"Line_23.jupyter.kts (4:9 - 54) Type mismatch: inferred type is ColumnFormula but Pair<TypeVariable(K), TypeVariable(V)> was expected\n", | |
"Line_23.jupyter.kts (4:34 - 43) Unresolved reference: asDoubles\n", | |
"Line_23.jupyter.kts (5:9 - 54) Type mismatch: inferred type is ColumnFormula but Pair<TypeVariable(K), TypeVariable(V)> was expected\n", | |
"Line_23.jupyter.kts (5:34 - 43) Unresolved reference: asStrings\n", | |
"Line_23.jupyter.kts (9:5 - 6) Unresolved reference: x\n", | |
"Line_23.jupyter.kts (10:5 - 6) Unresolved reference: y\n", | |
"Line_23.jupyter.kts (11:5 - 10) Unresolved reference: color" | |
] | |
} | |
], | |
"source": [ | |
"val points = geom_point(\n", | |
" data = mapOf(\n", | |
" \"x\" to df[\"sepal_length\"].asDoubles().toList(),\n", | |
" \"y\" to df[\"sepal_width\"].asDoubles().toList(),\n", | |
" \"color\" to df[\"species\"].asStrings().toList()\n", | |
"\n", | |
" ), alpha=1.0)\n", | |
"{\n", | |
" x = \"x\"\n", | |
" y = \"y\"\n", | |
" color = \"color\"\n", | |
"}\n", | |
"\n", | |
"ggplot() + points" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "tnO2488VbL4c" | |
}, | |
"source": [ | |
"### Useful libraries" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "rXloBquabL4c" | |
}, | |
"source": [ | |
"* [kotlin-statistics](https://github.com/thomasnield/kotlin-statistics) is a library that provides a set of extension functions to perform exploratory and production statistics. It supports basic numeric list/sequence/array functions (from `sum` to `skewness`), slicing operators (e.g. `countBy`, `simpleRegressionBy`, etc), binning operations, discrete PDF sampling, naive bayes classifier, clustering, linear regression, and more.\n", | |
"* [kmath](https://github.com/mipt-npm/kmath) is a library inspired by `numpy`; this library supports algebraic structures and operations, array-like structures, math expressions, histograms, streaming operations, wrappers around [commons-math](http://commons.apache.org/proper/commons-math/) and [koma](https://github.com/kyonifer/koma), and more.\n", | |
"* [krangl](https://github.com/holgerbrandl/krangl) is a library inspired by R's `dplyr` and Python's `pandas`; this library provides functionality for data manipulation using a functional-style API; it allows to filter, transform, aggregate and reshape tabular data.\n", | |
"* [lets-plot](https://github.com/JetBrains/lets-plot) is a library for declaratively creating plots based tabular data; it is inspired by Python's `ggplot` and [The Grammar of Graphics](https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448/); this library is integrated tightly with the Kotlin kernel; the library is multi-platform and can be used not just with JVM but also from JS and Python.\n", | |
"* [kravis](https://github.com/holgerbrandl/kravis) is another library inspired by Python's `ggplot` for visualization of tabular data." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "00tvJcovbL4c" | |
}, | |
"source": [ | |
"### Documentation and contribution" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "R96670u5bL4c" | |
}, | |
"source": [ | |
"The kernel's source code along with documentation is available on [GitHub](https://github.com/erokhins/kotlin-jupyter)." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7TTydDODbL4c" | |
}, | |
"source": [ | |
"The community has already started adopting Kotlin for data science, and this adoption is only growing. It’s very much recommended to watch a [talk](https://www.youtube.com/watch?v=yjVW6uCmVBA) by Holger Brandl (the creator of [krangl](https://github.com/holgerbrandl/krangl), a Kotlin’s analog of Python’s pandas) or another [talk](https://www.youtube.com/watch?v=-zTqtEcnM7A&feature=youtu.be) by Thomas Nield (the creator of [kotlin-statistics](https://github.com/thomasnield/kotlin-statistics)), or read his [article](https://towardsdatascience.com/introduction-to-kotlin-statistics-cdad3be88b5)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "7Sdg2HYsbL4c" | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Kotlin - kotlin (JBang)-ipc", | |
"name": "jbang-kotlin-ipc" | |
}, | |
"colab": { | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment