{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Polyglot Data Science with IPython & friends\n", "\n", "A demonstration of how to use Python, Julia, Fortran and R cooperatively to analyze data, in the same process. \n", "\n", "This is supported by the IPython kernel and a few extensions that take advantage of IPython's magic system to provide low-level integration between Python and other languages.\n", "\n", "See the [companion notebook](polyglot-ds-prep.ipynb) for data preparation and setup." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "plt.style.use(\"seaborn\")\n", "plt.rcParams[\"figure.figsize\"] = (10, 6)\n", "sns.set_context(\"talk\", font_scale=1.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's begin by reading our dataset and having a quick look:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(300, 2)\n" ] }, { "data": { "text/html": [ "
| \n", " | x | \n", "y | \n", "
|---|---|---|
| 0 | \n", "0.000000 | \n", "0.094287 | \n", "
| 1 | \n", "0.021014 | \n", "-0.216828 | \n", "
| 2 | \n", "0.042028 | \n", "0.329982 | \n", "
Generated by Cython 0.27.1
\n", "\n",
" Yellow lines hint at Python interaction.
\n",
" Click on a line that starts with a \"+\" to see the C code that Cython generated for it.\n",
"
+01: cdef double fcy(double x) except? -2:\n", "
static double __pyx_f_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_fcy(double __pyx_v_x) {\n",
" double __pyx_r;\n",
" __Pyx_RefNannyDeclarations\n",
" __Pyx_RefNannySetupContext(\"fcy\", 0);\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L0:;\n",
" __Pyx_RefNannyFinishContext();\n",
" return __pyx_r;\n",
"}\n",
"+02: return x**2-x\n", "
__pyx_r = (pow(__pyx_v_x, 2.0) - __pyx_v_x);\n",
" goto __pyx_L0;\n",
" 03: \n",
"+04: def integrate_fcy(double a, double b, int N):\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_1integrate_fcy(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_1integrate_fcy = {\"integrate_fcy\", (PyCFunction)__pyx_pw_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_1integrate_fcy, METH_VARARGS|METH_KEYWORDS, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_1integrate_fcy(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
" double __pyx_v_a;\n",
" double __pyx_v_b;\n",
" int __pyx_v_N;\n",
" PyObject *__pyx_r = 0;\n",
" __Pyx_RefNannyDeclarations\n",
" __Pyx_RefNannySetupContext(\"integrate_fcy (wrapper)\", 0);\n",
" {\n",
" static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_a,&__pyx_n_s_b,&__pyx_n_s_N,0};\n",
" PyObject* values[3] = {0,0,0};\n",
" if (unlikely(__pyx_kwds)) {\n",
" Py_ssize_t kw_args;\n",
" const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
" switch (pos_args) {\n",
" case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
" CYTHON_FALLTHROUGH;\n",
" case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
" CYTHON_FALLTHROUGH;\n",
" case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
" CYTHON_FALLTHROUGH;\n",
" case 0: break;\n",
" default: goto __pyx_L5_argtuple_error;\n",
" }\n",
" kw_args = PyDict_Size(__pyx_kwds);\n",
" switch (pos_args) {\n",
" case 0:\n",
" if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_a)) != 0)) kw_args--;\n",
" else goto __pyx_L5_argtuple_error;\n",
" CYTHON_FALLTHROUGH;\n",
" case 1:\n",
" if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_b)) != 0)) kw_args--;\n",
" else {\n",
" __Pyx_RaiseArgtupleInvalid(\"integrate_fcy\", 1, 3, 3, 1); __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" CYTHON_FALLTHROUGH;\n",
" case 2:\n",
" if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_N)) != 0)) kw_args--;\n",
" else {\n",
" __Pyx_RaiseArgtupleInvalid(\"integrate_fcy\", 1, 3, 3, 2); __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" }\n",
" if (unlikely(kw_args > 0)) {\n",
" if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"integrate_fcy\") < 0)) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n",
" goto __pyx_L5_argtuple_error;\n",
" } else {\n",
" values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
" values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
" values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
" }\n",
" __pyx_v_a = __pyx_PyFloat_AsDouble(values[0]); if (unlikely((__pyx_v_a == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" __pyx_v_b = __pyx_PyFloat_AsDouble(values[1]); if (unlikely((__pyx_v_b == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" __pyx_v_N = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_N == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" goto __pyx_L4_argument_unpacking_done;\n",
" __pyx_L5_argtuple_error:;\n",
" __Pyx_RaiseArgtupleInvalid(\"integrate_fcy\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 4, __pyx_L3_error)\n",
" __pyx_L3_error:;\n",
" __Pyx_AddTraceback(\"_cython_magic_8ba296f194edf8ff3bbb003f9da689f4.integrate_fcy\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __Pyx_RefNannyFinishContext();\n",
" return NULL;\n",
" __pyx_L4_argument_unpacking_done:;\n",
" __pyx_r = __pyx_pf_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_integrate_fcy(__pyx_self, __pyx_v_a, __pyx_v_b, __pyx_v_N);\n",
"\n",
" /* function exit code */\n",
" __Pyx_RefNannyFinishContext();\n",
" return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_integrate_fcy(CYTHON_UNUSED PyObject *__pyx_self, double __pyx_v_a, double __pyx_v_b, int __pyx_v_N) {\n",
" int __pyx_v_i;\n",
" double __pyx_v_s;\n",
" double __pyx_v_dx;\n",
" PyObject *__pyx_r = NULL;\n",
" __Pyx_RefNannyDeclarations\n",
" __Pyx_RefNannySetupContext(\"integrate_fcy\", 0);\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L1_error:;\n",
" __Pyx_XDECREF(__pyx_t_4);\n",
" __Pyx_AddTraceback(\"_cython_magic_8ba296f194edf8ff3bbb003f9da689f4.integrate_fcy\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __pyx_r = NULL;\n",
" __pyx_L0:;\n",
" __Pyx_XGIVEREF(__pyx_r);\n",
" __Pyx_RefNannyFinishContext();\n",
" return __pyx_r;\n",
"}\n",
"/* … */\n",
" __pyx_tuple_ = PyTuple_Pack(6, __pyx_n_s_a, __pyx_n_s_b, __pyx_n_s_N, __pyx_n_s_i, __pyx_n_s_s, __pyx_n_s_dx); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_tuple_);\n",
" __Pyx_GIVEREF(__pyx_tuple_);\n",
"/* … */\n",
" __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_1integrate_fcy, NULL, __pyx_n_s_cython_magic_8ba296f194edf8ff3b); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_1);\n",
" if (PyDict_SetItem(__pyx_d, __pyx_n_s_integrate_fcy, __pyx_t_1) < 0) __PYX_ERR(0, 4, __pyx_L1_error)\n",
" __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"05: cdef int i\n", "
06: cdef double s, dx\n", "
+07: s = 0; dx = (b-a)/N\n", "
__pyx_v_s = 0.0;\n",
" __pyx_t_1 = (__pyx_v_b - __pyx_v_a);\n",
" if (unlikely(__pyx_v_N == 0)) {\n",
" PyErr_SetString(PyExc_ZeroDivisionError, \"float division\");\n",
" __PYX_ERR(0, 7, __pyx_L1_error)\n",
" }\n",
" __pyx_v_dx = (__pyx_t_1 / ((double)__pyx_v_N));\n",
"+08: for i in range(N):\n", "
__pyx_t_2 = __pyx_v_N;\n",
" for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {\n",
" __pyx_v_i = __pyx_t_3;\n",
"+09: s += fcy(a+i*dx)\n", "
__pyx_t_1 = __pyx_f_46_cython_magic_8ba296f194edf8ff3bbb003f9da689f4_fcy((__pyx_v_a + (__pyx_v_i * __pyx_v_dx))); if (unlikely(__pyx_t_1 == ((double)-2.0) && PyErr_Occurred())) __PYX_ERR(0, 9, __pyx_L1_error)\n", " __pyx_v_s = (__pyx_v_s + __pyx_t_1);\n", " }\n", "
+10: return s * dx\n", "
__Pyx_XDECREF(__pyx_r);\n", " __pyx_t_4 = PyFloat_FromDouble((__pyx_v_s * __pyx_v_dx)); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 10, __pyx_L1_error)\n", " __Pyx_GOTREF(__pyx_t_4);\n", " __pyx_r = __pyx_t_4;\n", " __pyx_t_4 = 0;\n", " goto __pyx_L0;\n", "