gpu-jupyter/extra/Getting_Started/MultiInterpreterNotebook.ipynb

143 lines
76 KiB
Plaintext
Raw Permalink Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python and R in a single Notebook \n",
"\n",
"From the instructions of [all-spark-notebook](https://github.com/jupyter/docker-stacks/tree/master/all-spark-notebook) and [Stackoverflow](https://stackoverflow.com/questions/39008069/r-and-python-in-one-jupyter-notebook).\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Last run: 2019-12-19 09:09:24.610738 UTC\n"
]
}
],
"source": [
"import datetime\n",
"print('Last run:', datetime.datetime.utcnow(), 'UTC') # timezone can't be detected from browser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Processing in Python\n",
"* Create a dataframe\n",
"* load the rpy2 module"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"n = 100\n",
"df = pd.DataFrame({\n",
" 'cups_of_coffee': np.random.exponential(3, size=n),\n",
" 'productivity': np.random.normal(100, 15, n),\n",
" 'gender': np.random.choice([\"f\", \"m\"], n)\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"%load_ext rpy2.ipython"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Processing in R\n",
"\n",
"* Install and load ggplot2\n",
"* Receive the dataframe and set the figure size including resolution\n",
"* Plot the dataframe"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"%%R\n",
"#install.packages(\"ggplot2\", repos='http://cran.us.r-project.org', quiet=TRUE)\n",
"library(ggplot2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/conda/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order.\n",
" res = PandasDataFrame.from_items(items)\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxMAAAMTCAIAAABVON0HAAAACXBIWXMAAB7CAAAewgFu0HU+AAAgAElEQVR4nOzdeXxU1d0/8O+5984+WcmeQBICCMgmIirggoCKirJYC4pSH+2u1v6qtVVrFa219nGptk/bp621LvRRBNe64YKgLCKbAmEPIWTfk9nn3nt+fwwZQjKZmWT2mc/71dereOfM3G9yk8xnzjn3HMY5JwAAAAAIghDrAgAAAAASBpITAAAAQLCQnAAAAACCheQEAAAAECwkJwAAAIBgITkBAAAABAvJCQAAACBYSE4AAAAAwUJyAgAAAAgWkhMAAABAsJCcAAAAAIKF5AQAAAAQLCQnAAAAgGAhOQEAAAAES4p1Acmgo6NjyM/V6XRarZaIHA6H2+0OX1HxS5Ikg8FgtVpVVY11LdGQspfYZrMpihLrWqLBe4mdTqfL5Yp1OdEgiqLRaEydS6zVanU6HfVc4szMzFhXBLGE5BQGsiwP+bk6nU4URSLinIfyOglEEARRFFVVTZGv13uJKbQflQTiucSKoqTI16vValPtt5gxhksMKQujdQAAAADBQnICAAAACBaSEwAAAECwkJwAAAAAgoXkBAAAABAsJCcAAACAYCE5AQAAAAQLyQkAAAAgWEhOAAAAAMFCcgIAAAAIFpITAAAAQLCQnAAAAACCheQEAAAAECwkJwAAAIBgITkBAAAABAvJCQAAACBYSE4AAAAAwUJyAgAAAAgWkhMAAABAsJCcAAAAAIKF5AQAAAAQLCQnAAAAgGAhOQEAxJhNVR2cx7oKAAiKFOsCAABSVK3b/UxL+9udXc2yQkTDtZpFGek/HpaVLYmxLg0ABoQ+JwCAGFjXbZ116Nhzre2e2ERENS73M82tFxw+tsPmiG1tAOAHkhMAQLTttDtuPl5rUdX+DzXJ8rLjJ2rccvSrAoBgIDkBAETbPXWNTp8TmzgRUZus/Lq+McolAUC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5hWoyEw042Ulob5UO7o9mSQAAAAkKySn5idVVgtVC5Gucrofuq81RqwcAACBxITklP+l4VcA2Qt0J5nJFoRgAAICEhuSU/ASLJXAjzpk1iGYAAACpDckp+ak6bTDNuF4f6UoAAAASHZJT8lOLSgK3yczmBmMUigEAAEhoSE7JTx51Bjea/LdxTz4rOsUAAAAkNCSn5Mc1Wue8K4gNeGedkl/oPvvcaJYEAACQoJCcUoJ7zDjHpVeSKPZ/SC0qti9ZxkUp+lUBAAAkHLxfpgr3pKnK8FLt1k1S1WFm6eaiyAuKXBMmuydMIQEBGgAAIChITilEzRrmuHwBEZGi+Ox/AgAAAP+QnFJS6sUml8pXdXS+1dV9yOninMq1mqsz0m/MztAPPP0LAACgPyQnSH5HXa4bq2sPOk8tkt4oy1ts9r+2tr1YWjIuuPWuAAAACDPEIek1yfLiqhO9Y5NXtcu9uOp4jcsd/aoAACBBITlBklvZ2FLrHjAbtcjK/Q3N0awHAAASGpITJLMORVnb0eW/zXtd3XVuOTr1AABAokNygmS23e5wc+6/DSfaarNHpx4AAEh0SE6QzNplJZhmbTL6nAAAIChITpDMhgW3/kKuBjeZAgBAUJCcIJmdYzJoA63YJBCdbzRGpx4AAEh0SE6QzMyCcENWhv82izPTc6WUWxoUAACGBskJkty9+TkVA691WaLVrCzIi2Y9AACQ0JCcIMlliuLrZcOnGQ39H5qo171ZPhwdTgAAEDxMjIXkV6iR/lM+4j/dljc7uw46XZxolE57VZp5YUaaiH3rAABgMJCcICUIjBakmxekm2NdCAAAJDaM1gEAAAAEC31OKUE8dlQ6ekjoaOcaDc/Nd4+boGZkxrooAACAxIP
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%R -i df -w 10 -h 10 --units cm -r 200\n",
"ggplot(df, aes(x=cups_of_coffee, y=productivity, color=gender)) + geom_point() + \n",
"geom_smooth(method=\"lm\", se=TRUE) + ylab(\"Normalized Productivity\") + xlab(\"Cups of Coffee per day\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}